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