OpenMBeanAttributeInfoSupport.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* OpenMBeanAttributeInfoSupport.java -- Open typed info about an attribute.
  2. Copyright (C) 2006, 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.management.openmbean;
  32. import java.util.Collections;
  33. import java.util.HashSet;
  34. import java.util.Set;
  35. import javax.management.MBeanAttributeInfo;
  36. /**
  37. * Describes an attribute of an open management bean.
  38. *
  39. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  40. * @since 1.5
  41. */
  42. public class OpenMBeanAttributeInfoSupport
  43. extends MBeanAttributeInfo
  44. implements OpenMBeanAttributeInfo
  45. {
  46. /**
  47. * Compatible with JDK 1.5
  48. */
  49. private static final long serialVersionUID = -4867215622149721849L;
  50. /**
  51. * The open type of the attribute.
  52. */
  53. private OpenType<?> openType;
  54. /**
  55. * The default value of the attribute (may be <code>null</code>).
  56. */
  57. private Object defaultValue;
  58. /**
  59. * The possible legal values of the attribute (may be <code>null</code>).
  60. */
  61. private Set<?> legalValues;
  62. /**
  63. * The minimum value of the attribute (may be <code>null</code>).
  64. */
  65. private Comparable<?> minValue;
  66. /**
  67. * The maximum value of the attribute (may be <code>null</code>).
  68. */
  69. private Comparable<?> maxValue;
  70. /**
  71. * The hash code of this instance.
  72. */
  73. private transient Integer hashCode;
  74. /**
  75. * The <code>toString()</code> result of this instance.
  76. */
  77. private transient String string;
  78. /**
  79. * Constructs a new {@link OpenMBeanAttributeInfo} using the
  80. * specified name, description, open type and access properties.
  81. * The name, description and open type may not be <code>null</code>
  82. * and the name and description may not be equal to the empty
  83. * string.
  84. *
  85. * @param name the name of the attribute.
  86. * @param desc a description of the attribute.
  87. * @param type the open type of the attribute.
  88. * @param isReadable true if the attribute's value can be read.
  89. * @param isWritable true if the attribute's value can be changed.
  90. * @param isIs true if the attribute uses an accessor of the form isXXX.
  91. * @throws IllegalArgumentException if the name, description or
  92. * open type are <code>null</code>
  93. * or the name or description are
  94. * the empty string.
  95. */
  96. public OpenMBeanAttributeInfoSupport(String name, String desc, OpenType<?> type,
  97. boolean isReadable, boolean isWritable,
  98. boolean isIs)
  99. {
  100. super(name, type == null ? null : type.getClassName(), desc, isReadable,
  101. isWritable, isIs);
  102. if (name == null)
  103. throw new IllegalArgumentException("The name may not be null.");
  104. if (desc == null)
  105. throw new IllegalArgumentException("The description may not be null.");
  106. if (type == null)
  107. throw new IllegalArgumentException("The type may not be null.");
  108. if (name.length() == 0)
  109. throw new IllegalArgumentException("The name may not be the empty string.");
  110. if (desc.length() == 0)
  111. throw new IllegalArgumentException("The description may not be the " +
  112. "empty string.");
  113. }
  114. /**
  115. * Constructs a new {@link OpenMBeanAttributeInfo} using the
  116. * specified name, description, open type and default value. The
  117. * name, description and open type cannot be <code>null</code> and
  118. * the name and description may not be equal to the empty string.
  119. * The default value may be <code>null</code>. If non-null, it must
  120. * be a valid value of the given open type. Default values are not
  121. * applicable to the open types, {@link ArrayType} and {@link
  122. * TabularType}.
  123. *
  124. * @param name the name of the attribute.
  125. * @param desc a description of the attribute.
  126. * @param type the open type of the attribute.
  127. * @param isReadable true if the attribute's value can be read.
  128. * @param isWritable true if the attribute's value can be changed.
  129. * @param isIs true if the attribute uses an accessor of the form isXXX.
  130. * @param defaultValue the default value of the attribute.
  131. * @throws IllegalArgumentException if the name, description or
  132. * open type are <code>null</code>
  133. * or the name or description are
  134. * the empty string.
  135. * @throws OpenDataException if <code>defaultValue<code> is non-null
  136. * and is either not a value of the given
  137. * open type or the open type is an instance
  138. * of {@link ArrayType} or {@link TabularType}.
  139. */
  140. public <T> OpenMBeanAttributeInfoSupport(String name, String desc, OpenType<T> type,
  141. boolean isReadable, boolean isWritable,
  142. boolean isIs, T defaultValue)
  143. throws OpenDataException
  144. {
  145. this(name, desc, type, isReadable, isWritable, isIs, defaultValue, null);
  146. }
  147. /**
  148. * <p>
  149. * Constructs a new {@link OpenMBeanAttributeInfo} using the
  150. * specified name, description, open type, access properties,
  151. * default, maximum and minimum values. The name, description
  152. * and open type cannot be <code>null</code> and the name and
  153. * description may not be equal to the empty string. The
  154. * default, maximum and minimum values may be <code>null</code>.
  155. * The following conditions apply when the attributes mentioned
  156. * are non-null:
  157. * </p>
  158. * <ul>
  159. * <li>The values must be valid values for the given open type.</li>
  160. * <li>Default values are not applicable to the open types, {@link
  161. * ArrayType} and {@link TabularType}.</li>
  162. * <li>The minimum value must be smaller than or equal to the maximum value
  163. * (literally, <code>minValue.compareTo(maxValue) <= 0</code>.</li>
  164. * <li>The minimum value must be smaller than or equal to the default value
  165. * (literally, <code>minValue.compareTo(defaultValue) <= 0</code>.</li>
  166. * <li>The default value must be smaller than or equal to the maximum value
  167. * (literally, <code>defaultValue.compareTo(maxValue) <= 0</code>.</li>
  168. * </ul>
  169. *
  170. * @param name the name of the attribute.
  171. * @param desc a description of the attribute.
  172. * @param type the open type of the attribute.
  173. * @param isReadable true if the attribute's value can be read.
  174. * @param isWritable true if the attribute's value can be changed.
  175. * @param isIs true if the attribute uses an accessor of the form isXXX.
  176. * @param defaultValue the default value of the attribute, or <code>null</code>.
  177. * @param minimumValue the minimum value of the attribute, or <code>null</code>.
  178. * @param maximumValue the maximum value of the attribute, or <code>null</code>.
  179. * @throws IllegalArgumentException if the name, description or
  180. * open type are <code>null</code>
  181. * or the name or description are
  182. * the empty string.
  183. * @throws OpenDataException if any condition in the list above is broken.
  184. */
  185. @SuppressWarnings("unchecked")
  186. public <T> OpenMBeanAttributeInfoSupport(String name, String desc, OpenType<T> type,
  187. boolean isReadable, boolean isWritable,
  188. boolean isIs, T defaultValue,
  189. Comparable<T> minimumValue,
  190. Comparable<T> maximumValue)
  191. throws OpenDataException
  192. {
  193. this(name, desc, type, isReadable, isWritable, isIs);
  194. if (defaultValue != null && !(type.isValue(defaultValue)))
  195. throw new OpenDataException("The default value is not a member of the " +
  196. "open type given.");
  197. if (minimumValue != null && !(type.isValue(minimumValue)))
  198. throw new OpenDataException("The minimum value is not a member of the " +
  199. "open type given.");
  200. if (maximumValue != null && !(type.isValue(maximumValue)))
  201. throw new OpenDataException("The maximum value is not a member of the " +
  202. "open type given.");
  203. if (defaultValue != null && (type instanceof ArrayType ||
  204. type instanceof TabularType))
  205. throw new OpenDataException("Default values are not applicable for " +
  206. "array or tabular types.");
  207. if (minimumValue != null && maximumValue != null
  208. && minimumValue.compareTo((T) maximumValue) > 0)
  209. throw new OpenDataException("The minimum value is greater than the " +
  210. "maximum.");
  211. if (minimumValue != null && defaultValue != null
  212. && minimumValue.compareTo(defaultValue) > 0)
  213. throw new OpenDataException("The minimum value is greater than the " +
  214. "default.");
  215. if (defaultValue != null && maximumValue != null
  216. && maximumValue.compareTo(defaultValue) < 0)
  217. throw new OpenDataException("The default value is greater than the " +
  218. "maximum.");
  219. openType = type;
  220. this.defaultValue = defaultValue;
  221. minValue = minimumValue;
  222. maxValue = maximumValue;
  223. }
  224. /**
  225. * <p>
  226. * Constructs a new {@link OpenMBeanAttributeInfo} using the
  227. * specified name, description, open type, access properties, default
  228. * value and set of legal values. The name, description and open type
  229. * cannot be <code>null</code> and the name and description may not be
  230. * equal to the empty string. The default, maximum and minimum values
  231. * may be <code>null</code>. The following conditions apply when the
  232. * attributes mentioned are non-null:
  233. * </p>
  234. * <ul>
  235. * <li>The default value and each of the legal values must be a valid
  236. * value for the given open type.</li>
  237. * <li>Default and legal values are not applicable to the open types, {@link
  238. * ArrayType} and {@link TabularType}.</li>
  239. * <li>The default value is not in the set of legal values.</li>
  240. * </ul>
  241. * <p>
  242. * The legal values are copied from the array into a unmodifiable set,
  243. * so future modifications to the array have no effect.
  244. * </p>
  245. *
  246. * @param name the name of the attribute.
  247. * @param desc a description of the attribute.
  248. * @param type the open type of the attribute.
  249. * @param isReadable true if the attribute's value can be read.
  250. * @param isWritable true if the attribute's value can be changed.
  251. * @param isIs true if the attribute uses an accessor of the form isXXX.
  252. * @param defaultValue the default value of the attribute, or <code>null</code>.
  253. * @param legalValues the legal values of the attribute. May be
  254. * <code>null</code> or an empty array.
  255. * @throws IllegalArgumentException if the name, description or
  256. * open type are <code>null</code>
  257. * or the name or description are
  258. * the empty string.
  259. * @throws OpenDataException if any condition in the list above is broken.
  260. */
  261. public <T> OpenMBeanAttributeInfoSupport(String name, String desc, OpenType<T> type,
  262. boolean isReadable, boolean isWritable,
  263. boolean isIs, T defaultValue,
  264. T[] legalValues)
  265. throws OpenDataException
  266. {
  267. this(name, desc, type, isReadable, isWritable, isIs);
  268. if (defaultValue != null && !(type.isValue(defaultValue)))
  269. throw new OpenDataException("The default value is not a member of the " +
  270. "open type given.");
  271. if (defaultValue != null && (type instanceof ArrayType ||
  272. type instanceof TabularType))
  273. throw new OpenDataException("Default values are not applicable for " +
  274. "array or tabular types.");
  275. if (legalValues != null && (type instanceof ArrayType ||
  276. type instanceof TabularType))
  277. throw new OpenDataException("Legal values are not applicable for " +
  278. "array or tabular types.");
  279. if (legalValues != null && legalValues.length > 0)
  280. {
  281. Set<T> lv = new HashSet<T>(legalValues.length);
  282. for (int a = 0; a < legalValues.length; ++a)
  283. {
  284. if (legalValues[a] != null &&
  285. !(type.isValue(legalValues[a])))
  286. throw new OpenDataException("The legal value, "
  287. + legalValues[a] +
  288. "is not a member of the " +
  289. "open type given.");
  290. lv.add(legalValues[a]);
  291. }
  292. if (defaultValue != null && !(lv.contains(defaultValue)))
  293. throw new OpenDataException("The default value is not in the set " +
  294. "of legal values.");
  295. this.legalValues = Collections.unmodifiableSet(lv);
  296. }
  297. openType = type;
  298. this.defaultValue = defaultValue;
  299. }
  300. /**
  301. * Compares this attribute with the supplied object. This returns
  302. * true iff the object is an instance of {@link OpenMBeanAttributeInfo}
  303. * with an equal name and open type and the same default, minimum,
  304. * maximum and legal values and the same access properties.
  305. *
  306. * @param obj the object to compare.
  307. * @return true if the object is a {@link OpenMBeanAttributeInfo}
  308. * instance,
  309. * <code>name.equals(object.getName())</code>,
  310. * <code>openType.equals(object.getOpenType())</code>,
  311. * <code>isRead == object.isReadable()</code>,
  312. * <code>isWrite == object.isWritable()</code>,
  313. * <code>isIs == object.isIs()</code>,
  314. * <code>defaultValue.equals(object.getDefaultValue())</code>,
  315. * <code>minValue.equals(object.getMinValue())</code>,
  316. * <code>maxValue.equals(object.getMaxValue())</code>,
  317. * and <code>legalValues.equals(object.getLegalValues())</code>.
  318. */
  319. public boolean equals(Object obj)
  320. {
  321. if (!(obj instanceof OpenMBeanAttributeInfo))
  322. return false;
  323. OpenMBeanAttributeInfo o = (OpenMBeanAttributeInfo) obj;
  324. return getName().equals(o.getName()) &&
  325. openType.equals(o.getOpenType()) &&
  326. isReadable() == o.isReadable() &&
  327. isWritable() == o.isWritable() &&
  328. isIs() == o.isIs() &&
  329. (defaultValue == null ? o.getDefaultValue() == null :
  330. defaultValue.equals(o.getDefaultValue())) &&
  331. (minValue == null ? o.getMinValue() == null :
  332. minValue.equals(o.getMinValue())) &&
  333. (maxValue == null ? o.getMaxValue() == null :
  334. maxValue.equals(o.getMaxValue())) &&
  335. (legalValues == null ? o.getLegalValues() == null :
  336. legalValues.equals(o.getLegalValues()));
  337. }
  338. /**
  339. * Returns the default value of this attribute, or <code>null</code>
  340. * if there is no default value.
  341. *
  342. * @return the default value of the attribute, or <code>null</code>
  343. * if there is no default.
  344. */
  345. public Object getDefaultValue()
  346. {
  347. return defaultValue;
  348. }
  349. /**
  350. * Returns a {@link java.util.Set} enumerating the legal values
  351. * of this attribute, or <code>null</code> if no such limited
  352. * set exists for this attribute.
  353. *
  354. * @return a set of legal values, or <code>null</code> if no such
  355. * set exists.
  356. */
  357. public Set<?> getLegalValues()
  358. {
  359. return legalValues;
  360. }
  361. /**
  362. * Returns the maximum value of this attribute, or <code>null</code>
  363. * if there is no maximum.
  364. *
  365. * @return the maximum value, or <code>null</code> if none exists.
  366. */
  367. public Comparable<?> getMaxValue()
  368. {
  369. return maxValue;
  370. }
  371. /**
  372. * Returns the minimum value of this attribute, or <code>null</code>
  373. * if there is no minimum.
  374. *
  375. * @return the minimum value, or <code>null</code> if none exists.
  376. */
  377. public Comparable<?> getMinValue()
  378. {
  379. return minValue;
  380. }
  381. /**
  382. * Returns the open type instance which represents the type of this
  383. * attribute.
  384. *
  385. * @return the open type of this attribute.
  386. */
  387. public OpenType<?> getOpenType()
  388. {
  389. return openType;
  390. }
  391. /**
  392. * Returns true if this attribute has a default value
  393. * (i.e. the value is non-null).
  394. *
  395. * @return true if this attribute has a default.
  396. */
  397. public boolean hasDefaultValue()
  398. {
  399. return defaultValue != null;
  400. }
  401. /**
  402. * <p>
  403. * Returns the hashcode of the attribute information as the sum of
  404. * the hashcodes of the name, open type, default value, maximum
  405. * value, minimum value and the set of legal values.
  406. * </p>
  407. * <p>
  408. * As instances of this class are immutable, the hash code
  409. * is computed just once for each instance and reused
  410. * throughout its life.
  411. * </p>
  412. *
  413. * @return the hashcode of the attribute information.
  414. */
  415. public int hashCode()
  416. {
  417. if (hashCode == null)
  418. hashCode = Integer.valueOf(getName().hashCode() +
  419. openType.hashCode() +
  420. Boolean.valueOf(isReadable()).hashCode() +
  421. (2 *
  422. Boolean.valueOf(isWritable()).hashCode()) +
  423. (4 * Boolean.valueOf(isIs()).hashCode()) +
  424. (defaultValue == null ? 0 :
  425. defaultValue.hashCode()) +
  426. (minValue == null ? 0 :
  427. minValue.hashCode()) +
  428. (maxValue == null ? 0 :
  429. maxValue.hashCode()) +
  430. (legalValues == null ? 0 :
  431. legalValues.hashCode()));
  432. return hashCode.intValue();
  433. }
  434. /**
  435. * Returns true if there is a set of legal values for this
  436. * attribute (i.e. the value is non-null).
  437. *
  438. * @return true if a set of legal values exists for this
  439. * attribute.
  440. */
  441. public boolean hasLegalValues()
  442. {
  443. return legalValues != null;
  444. }
  445. /**
  446. * Returns true if there is a maximum value for this attribute
  447. * (i.e. the value is non-null).
  448. *
  449. * @return true if a maximum value exists for this attribute.
  450. */
  451. public boolean hasMaxValue()
  452. {
  453. return maxValue != null;
  454. }
  455. /**
  456. * Returns true if there is a minimum value for this attribute.
  457. * (i.e. the value is non-null).
  458. *
  459. * @return true if a minimum value exists for this attribute.
  460. */
  461. public boolean hasMinValue()
  462. {
  463. return minValue != null;
  464. }
  465. /**
  466. * Returns true if the specified object is a valid value for
  467. * this attribute.
  468. *
  469. * @param obj the object to test.
  470. * @return true if <code>obj</code> is a valid value for this
  471. * attribute.
  472. */
  473. public boolean isValue(Object obj)
  474. {
  475. return openType.isValue(obj);
  476. }
  477. /**
  478. * <p>
  479. * Returns a textual representation of this instance. This
  480. * is constructed using the class name
  481. * (<code>javax.management.openmbean.OpenMBeanAttributeInfo</code>)
  482. * along with the name, open type, access properties, default,
  483. * minimum, maximum and legal values of the attribute.
  484. * </p>
  485. * <p>
  486. * As instances of this class are immutable, the return value
  487. * is computed just once for each instance and reused
  488. * throughout its life.
  489. * </p>
  490. *
  491. * @return a @link{java.lang.String} instance representing
  492. * the instance in textual form.
  493. */
  494. public String toString()
  495. {
  496. if (string == null)
  497. string = getClass().getName()
  498. + "[name=" + getName()
  499. + ",openType=" + openType
  500. + ",isReadable=" + isReadable()
  501. + ",isWritable=" + isWritable()
  502. + ",isIs=" + isIs()
  503. + ",defaultValue=" + defaultValue
  504. + ",minValue=" + minValue
  505. + ",maxValue=" + maxValue
  506. + ",legalValues=" + legalValues
  507. + "]";
  508. return string;
  509. }
  510. }