001/*
002 * Copyright 2008-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.unboundidds.monitors;
022
023
024
025import java.io.Serializable;
026import java.util.Arrays;
027import java.util.Collections;
028import java.util.Date;
029import java.util.List;
030
031import com.unboundid.util.NotMutable;
032import com.unboundid.util.ThreadSafety;
033import com.unboundid.util.ThreadSafetyLevel;
034
035import static com.unboundid.util.Validator.*;
036
037
038
039/**
040 * This class provides a data structure for providing information about the data
041 * presented in an attribute in a Directory Server monitor entry.  It includes
042 * a human-readable display name, a human-readable description, a class that
043 * represents the data type for the values, and the set of values.
044 * <BR>
045 * <BLOCKQUOTE>
046 *   <B>NOTE:</B>  This class, and other classes within the
047 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
048 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
049 *   server products.  These classes provide support for proprietary
050 *   functionality or for external specifications that are not considered stable
051 *   or mature enough to be guaranteed to work in an interoperable way with
052 *   other types of LDAP servers.
053 * </BLOCKQUOTE>
054 */
055@NotMutable()
056@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
057public final class MonitorAttribute
058       implements Serializable
059{
060  /**
061   * The serial version UID for this serializable class.
062   */
063  private static final long serialVersionUID = 7931725606171964572L;
064
065
066
067  // The data type for the values of this monitor attribute.
068  private final Class<?> dataType;
069
070  // The set of values for this monitor attribute.
071  private final Object[] values;
072
073  // The description for this monitor attribute.
074  private final String description;
075
076  // The display name for this monitor attribute.
077  private final String displayName;
078
079  // The name used to identify this monitor attribute.
080  private final String name;
081
082
083
084  /**
085   * Creates a new monitor attribute with the provided information.  It will
086   * have a single Boolean value.
087   *
088   * @param  name         The name used to identify this monitor attribute.  It
089   *                      must not be {@code null}.
090   * @param  displayName  The human-readable display name for this monitor
091   *                      attribute.  It must not be {@code null}.
092   * @param  description  A human-readable description for this monitor
093   *                      attribute.  It may be {@code null} if no description
094   *                      is available.
095   * @param  value        The {@code Boolean} value for this monitor attribute.
096   *                      It must not be {@code null}.
097   */
098  public MonitorAttribute(final String name, final String displayName,
099                          final String description, final Boolean value)
100  {
101    this(name, displayName, description, Boolean.class, new Object[] { value });
102
103    ensureNotNull(value);
104  }
105
106
107
108  /**
109   * Creates a new monitor attribute with the provided information.  It will
110   * have a single Date value.
111   *
112   * @param  name         The name used to identify this monitor attribute.  It
113   *                      must not be {@code null}.
114   * @param  displayName  The human-readable display name for this monitor
115   *                      attribute.  It must not be {@code null}.
116   * @param  description  A human-readable description for this monitor
117   *                      attribute.  It may be {@code null} if no description
118   *                      is available.
119   * @param  value        The {@code Date} value for this monitor attribute.  It
120   *                      must not be {@code null}.
121   */
122  public MonitorAttribute(final String name, final String displayName,
123                          final String description, final Date value)
124  {
125    this(name, displayName, description, Date.class, new Object[] { value });
126
127    ensureNotNull(value);
128  }
129
130
131
132  /**
133   * Creates a new monitor attribute with the provided information.  It will
134   * have one or more Date values.
135   *
136   * @param  name         The name used to identify this monitor attribute.  It
137   *                      must not be {@code null}.
138   * @param  displayName  The human-readable display name for this monitor
139   *                      attribute.  It must not be {@code null}.
140   * @param  description  A human-readable description for this monitor
141   *                      attribute.  It may be {@code null} if no description
142   *                      is available.
143   * @param  values       The set of {@code Date} values for this monitor
144   *                      attribute.  It must not be {@code null} or empty.
145   */
146  public MonitorAttribute(final String name, final String displayName,
147                          final String description, final Date[] values)
148  {
149    this(name, displayName, description, Date.class, values);
150  }
151
152
153
154  /**
155   * Creates a new monitor attribute with the provided information.  It will
156   * have a single Double value.
157   *
158   * @param  name         The name used to identify this monitor attribute.  It
159   *                      must not be {@code null}.
160   * @param  displayName  The human-readable display name for this monitor
161   *                      attribute.  It must not be {@code null}.
162   * @param  description  A human-readable description for this monitor
163   *                      attribute.  It may be {@code null} if no description
164   *                      is available.
165   * @param  value        The {@code Double} value for this monitor attribute.
166   *                      It must not be {@code null}.
167   */
168  public MonitorAttribute(final String name, final String displayName,
169                          final String description, final Double value)
170  {
171    this(name, displayName, description, Double.class, new Object[] { value });
172
173    ensureNotNull(value);
174  }
175
176
177
178  /**
179   * Creates a new monitor attribute with the provided information.  It will
180   * have one or more Double values.
181   *
182   * @param  name         The name used to identify this monitor attribute.  It
183   *                      must not be {@code null}.
184   * @param  displayName  The human-readable display name for this monitor
185   *                      attribute.  It must not be {@code null}.
186   * @param  description  A human-readable description for this monitor
187   *                      attribute.  It may be {@code null} if no description
188   *                      is available.
189   * @param  values       The set of {@code Double} values for this monitor
190   *                      attribute.  It must not be {@code null} or empty.
191   */
192  public MonitorAttribute(final String name, final String displayName,
193                          final String description, final Double[] values)
194  {
195    this(name, displayName, description, Double.class, values);
196  }
197
198
199
200  /**
201   * Creates a new monitor attribute with the provided information.  It will
202   * have a single Long value.
203   *
204   * @param  name         The name used to identify this monitor attribute.  It
205   *                      must not be {@code null}.
206   * @param  displayName  The human-readable display name for this monitor
207   *                      attribute.  It must not be {@code null}.
208   * @param  description  A human-readable description for this monitor
209   *                      attribute.  It may be {@code null} if no description
210   *                      is available.
211   * @param  value        The {@code Integer} value for this monitor attribute.
212   *                      It must not be {@code null}.
213   */
214  public MonitorAttribute(final String name, final String displayName,
215                          final String description, final Integer value)
216  {
217    this(name, displayName, description, Integer.class, new Object[] { value });
218
219    ensureNotNull(value);
220  }
221
222
223
224  /**
225   * Creates a new monitor attribute with the provided information.  It will
226   * have a single Long value.
227   *
228   * @param  name         The name used to identify this monitor attribute.  It
229   *                      must not be {@code null}.
230   * @param  displayName  The human-readable display name for this monitor
231   *                      attribute.  It must not be {@code null}.
232   * @param  description  A human-readable description for this monitor
233   *                      attribute.  It may be {@code null} if no description
234   *                      is available.
235   * @param  values       The set of {@code Integer} values for this monitor
236   *                      attribute.  It must not be {@code null} or empty.
237   */
238  public MonitorAttribute(final String name, final String displayName,
239                          final String description, final Integer[] values)
240  {
241    this(name, displayName, description, Integer.class, values);
242  }
243
244
245
246  /**
247   * Creates a new monitor attribute with the provided information.  It will
248   * have a single Long value.
249   *
250   * @param  name         The name used to identify this monitor attribute.  It
251   *                      must not be {@code null}.
252   * @param  displayName  The human-readable display name for this monitor
253   *                      attribute.  It must not be {@code null}.
254   * @param  description  A human-readable description for this monitor
255   *                      attribute.  It may be {@code null} if no description
256   *                      is available.
257   * @param  value        The {@code Long} value for this monitor attribute.  It
258   *                      must not be {@code null}.
259   */
260  public MonitorAttribute(final String name, final String displayName,
261                          final String description, final Long value)
262  {
263    this(name, displayName, description, Long.class, new Object[] { value });
264
265    ensureNotNull(value);
266  }
267
268
269
270  /**
271   * Creates a new monitor attribute with the provided information.  It will
272   * have one or more Long values.
273   *
274   * @param  name         The name used to identify this monitor attribute.  It
275   *                      must not be {@code null}.
276   * @param  displayName  The human-readable display name for this monitor
277   *                      attribute.  It must not be {@code null}.
278   * @param  description  A human-readable description for this monitor
279   *                      attribute.  It may be {@code null} if no description
280   *                      is available.
281   * @param  values       The set of {@code Long} values for this monitor
282   *                      attribute.  It must not be {@code null} or empty.
283   */
284  public MonitorAttribute(final String name, final String displayName,
285                          final String description, final Long[] values)
286  {
287    this(name, displayName, description, Long.class, values);
288  }
289
290
291
292  /**
293   * Creates a new monitor attribute with the provided information.  It will
294   * have a single String value.
295   *
296   * @param  name         The name used to identify this monitor attribute.  It
297   *                      must not be {@code null}.
298   * @param  displayName  The human-readable display name for this monitor
299   *                      attribute.  It must not be {@code null}.
300   * @param  description  A human-readable description for this monitor
301   *                      attribute.  It may be {@code null} if no description
302   *                      is available.
303   * @param  value        The {@code String} value for this monitor attribute.
304   *                      It must not be {@code null}.
305   */
306  public MonitorAttribute(final String name, final String displayName,
307                          final String description, final String value)
308  {
309    this(name, displayName, description, String.class, new Object[] { value });
310
311    ensureNotNull(value);
312  }
313
314
315
316  /**
317   * Creates a new monitor attribute with the provided information.  It will
318   * have one or more String values.
319   *
320   * @param  name         The name used to identify this monitor attribute.  It
321   *                      must not be {@code null}.
322   * @param  displayName  The human-readable display name for this monitor
323   *                      attribute.  It must not be {@code null}.
324   * @param  description  A human-readable description for this monitor
325   *                      attribute.  It may be {@code null} if no description
326   *                      is available.
327   * @param  values       The set of {@code String} values for this monitor
328   *                      attribute.  It must not be {@code null} or empty.
329   */
330  public MonitorAttribute(final String name, final String displayName,
331                          final String description, final String[] values)
332  {
333    this(name, displayName, description, String.class, values);
334  }
335
336
337
338  /**
339   * Creates a new monitor attribute with the provided information.
340   *
341   * @param  name         The name used to identify this monitor attribute.  It
342   *                      must not be {@code null}.
343   * @param  displayName  The human-readable display name for this monitor
344   *                      attribute.  It must not be {@code null}.
345   * @param  description  A human-readable description for this monitor
346   *                      attribute.  It may be {@code null} if no description
347   *                      is available.
348   * @param  dataType     The data type for this monitor attribute.  It may be
349   *                      one of the following classes:  Boolean, Date, Double,
350   *                      Long, and String.  It must not be {@code null}.
351   * @param  values       The set of values for this monitor attribute.  The
352   *                      data type for the values must correspond to the value
353   *                      of the {@code dataType} attribute.  It must not be
354   *                      {@code null} or empty.
355   */
356  private MonitorAttribute(final String name, final String displayName,
357                           final String description, final Class<?> dataType,
358                           final Object[] values)
359  {
360    ensureNotNull(name, displayName, dataType, values);
361    ensureFalse(values.length == 0,
362                "MonitorAttribute.values must not be empty.");
363
364    this.name        = name;
365    this.displayName = displayName;
366    this.description = description;
367    this.dataType    = dataType;
368    this.values      = values;
369  }
370
371
372
373  /**
374   * Retrieves the name used to identify this monitor attribute.  It is not
375   * necessarily human-readable, but it should be used as the key for this
376   * monitor attribute in the map returned by the
377   * {@code MonitorEntry.getMonitorAttributes} method.
378   *
379   * @return  The name used to identify this monitor attribute.
380   */
381  public String getName()
382  {
383    return name;
384  }
385
386
387
388  /**
389   * Retrieves the human-readable display name for this monitor attribute.
390   *
391   * @return  The human-readable display name for this monitor attribute.
392   */
393  public String getDisplayName()
394  {
395    return displayName;
396  }
397
398
399
400  /**
401   * Retrieves the human-readable description for this monitor attribute, if
402   * available.
403   *
404   * @return  The human-readable description for this monitor attribute, or
405   *          {@code null} if none is available.
406   */
407  public String getDescription()
408  {
409    return description;
410  }
411
412
413
414  /**
415   * Retrieves the class representing the data type for this monitor attribute.
416   * It will be one of the following class types:  Boolean, Date, Double, Long,
417   * or String.
418   *
419   * @return  The class representing the data type for this monitor attribute.
420   */
421  public Class<?> getDataType()
422  {
423    return dataType;
424  }
425
426
427
428  /**
429   * Indicates whether this monitor attribute has multiple values.
430   *
431   * @return  {@code true} if this monitor attribute has more than one value, or
432   *          {@code false} if not.
433   */
434  public boolean hasMultipleValues()
435  {
436    return (values.length > 1);
437  }
438
439
440
441  /**
442   * Retrieves the value for this monitor attribute as an {@code Object}.  If it
443   * has multiple values, then the first will be returned.
444   *
445   * @return  The value for this monitor attribute as an {@code Object}.
446   */
447  public Object getValue()
448  {
449    return values[0];
450  }
451
452
453
454  /**
455   * Retrieves the set of values for this monitor attribute as a list of
456   * {@code Object}s.
457   *
458   * @return  The set of values for this monitor attribute as a list of
459   *          {@code Object}s.
460   */
461  public List<Object> getValues()
462  {
463    return Collections.unmodifiableList(Arrays.asList(values));
464  }
465
466
467
468  /**
469   * Retrieves the value for this monitor attribute as a {@code Boolean} object.
470   *
471   * @return  The value for this monitor attribute as a {@code Boolean} object.
472   *
473   * @throws  ClassCastException  If the data type for this monitor attribute is
474   *                              not {@code Boolean}.
475   */
476  public Boolean getBooleanValue()
477         throws ClassCastException
478  {
479    return (Boolean) values[0];
480  }
481
482
483
484  /**
485   * Retrieves the value for this monitor attribute as a {@code Date} object.
486   *
487   * @return  The value for this monitor attribute as a {@code Date} object.
488   *
489   * @throws  ClassCastException  If the data type for this monitor attribute is
490   *                              not {@code Date}.
491   */
492  public Date getDateValue()
493         throws ClassCastException
494  {
495    return (Date) values[0];
496  }
497
498
499
500  /**
501   * Retrieves the values for this monitor attribute as a list of {@code Date}
502   * objects.
503   *
504   * @return  The values for this monitor attribute as a list of {@code Date}
505   *          objects.
506   *
507   * @throws  ClassCastException  If the data type for this monitor attribute is
508   *                              not {@code Date}.
509   */
510  public List<Date> getDateValues()
511         throws ClassCastException
512  {
513    return Collections.unmodifiableList(Arrays.asList((Date[]) values));
514  }
515
516
517
518  /**
519   * Retrieves the value for this monitor attribute as a {@code Double} object.
520   *
521   * @return  The value for this monitor attribute as a {@code Double} object.
522   *
523   * @throws  ClassCastException  If the data type for this monitor attribute is
524   *                              not {@code Double}.
525   */
526  public Double getDoubleValue()
527         throws ClassCastException
528  {
529    return (Double) values[0];
530  }
531
532
533
534  /**
535   * Retrieves the values for this monitor attribute as a list of {@code Double}
536   * objects.
537   *
538   * @return  The values for this monitor attribute as a list of {@code Double}
539   *          objects.
540   *
541   * @throws  ClassCastException  If the data type for this monitor attribute is
542   *                              not {@code Double}.
543   */
544  public List<Double> getDoubleValues()
545         throws ClassCastException
546  {
547    return Collections.unmodifiableList(Arrays.asList((Double[]) values));
548  }
549
550
551
552  /**
553   * Retrieves the value for this monitor attribute as an {@code Integer}
554   * object.
555   *
556   * @return  The value for this monitor attribute as an {@code Integer} object.
557   *
558   * @throws  ClassCastException  If the data type for this monitor attribute is
559   *                              not {@code Integer}.
560   */
561  public Integer getIntegerValue()
562         throws ClassCastException
563  {
564    return (Integer) values[0];
565  }
566
567
568
569  /**
570   * Retrieves the values for this monitor attribute as a list of
571   * {@code Integer} objects.
572   *
573   * @return  The values for this monitor attribute as a list of {@code Integer}
574   *          objects.
575   *
576   * @throws  ClassCastException  If the data type for this monitor attribute is
577   *                              not {@code Integer}.
578   */
579  public List<Integer> getIntegerValues()
580         throws ClassCastException
581  {
582    return Collections.unmodifiableList(Arrays.asList((Integer[]) values));
583  }
584
585
586
587  /**
588   * Retrieves the value for this monitor attribute as a {@code Long} object.
589   *
590   * @return  The value for this monitor attribute as a {@code Long} object.
591   *
592   * @throws  ClassCastException  If the data type for this monitor attribute is
593   *                              not {@code Long}.
594   */
595  public Long getLongValue()
596         throws ClassCastException
597  {
598    return (Long) values[0];
599  }
600
601
602
603  /**
604   * Retrieves the values for this monitor attribute as a list of {@code Long}
605   * objects.
606   *
607   * @return  The values for this monitor attribute as a list of {@code Long}
608   *          objects.
609   *
610   * @throws  ClassCastException  If the data type for this monitor attribute is
611   *                              not {@code Long}.
612   */
613  public List<Long> getLongValues()
614         throws ClassCastException
615  {
616    return Collections.unmodifiableList(Arrays.asList((Long[]) values));
617  }
618
619
620
621  /**
622   * Retrieves the value for this monitor attribute as a {@code String} object.
623   *
624   * @return  The value for this monitor attribute as a {@code String} object.
625   *
626   * @throws  ClassCastException  If the data type for this monitor attribute is
627   *                              not {@code String}.
628   */
629  public String getStringValue()
630         throws ClassCastException
631  {
632    return (String) values[0];
633  }
634
635
636
637  /**
638   * Retrieves the values for this monitor attribute as a list of {@code String}
639   * objects.
640   *
641   * @return  The values for this monitor attribute as a list of {@code String}
642   *          objects.
643   *
644   * @throws  ClassCastException  If the data type for this monitor attribute is
645   *                              not {@code String}.
646   */
647  public List<String> getStringValues()
648         throws ClassCastException
649  {
650    return Collections.unmodifiableList(Arrays.asList((String[]) values));
651  }
652
653
654
655  /**
656   * Retrieves a string representation of this monitor attribute.
657   *
658   * @return  A string representation of this monitor attribute.
659   */
660  @Override()
661  public String toString()
662  {
663    final StringBuilder buffer = new StringBuilder();
664    toString(buffer);
665    return buffer.toString();
666  }
667
668
669
670  /**
671   * Appends a string representation of this monitor attribute to the provided
672   * buffer.
673   *
674   * @param  buffer  The buffer to which the string representation should be
675   *                 appended.
676   */
677  public void toString(final StringBuilder buffer)
678  {
679    buffer.append("MonitorAttribute(name='");
680    buffer.append(name);
681    buffer.append("', values={");
682
683    for (int i=0; i < values.length; i++)
684    {
685      if (i > 0)
686      {
687        buffer.append(", ");
688      }
689
690      buffer.append('\'');
691      buffer.append(String.valueOf(values[i]));
692      buffer.append('\'');
693    }
694
695    buffer.append("})");
696  }
697}