1. /*
  2. * @(#)NamedObject.java 1.31 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.jmx.mbeanserver;
  8. import javax.management.* ;
  9. /**
  10. * This class is used for storing a pair (name, object) where name is
  11. * an object name and object is a reference to the object.
  12. *
  13. * @since 1.5
  14. * @since.unbundled JMX RI 1.2
  15. */
  16. public class NamedObject {
  17. /**
  18. * Object name.
  19. */
  20. private ObjectName name;
  21. /**
  22. * Object reference.
  23. */
  24. private Object object= null;
  25. /**
  26. * Allows a named object to be created.
  27. *
  28. *@param objectName The object name of the object.
  29. *@param object A reference to the object.
  30. */
  31. public NamedObject(ObjectName objectName, Object object) {
  32. if (objectName.isPattern()) {
  33. throw new RuntimeOperationsException(new IllegalArgumentException("Invalid name->"+ objectName.toString()));
  34. }
  35. this.name= objectName;
  36. this.object= object;
  37. }
  38. /**
  39. * Allows a named object to be created.
  40. *
  41. *@param objectName The string representation of the object name of the object.
  42. *@param object A reference to the object.
  43. *
  44. *@exception MalformedObjectNameException The string passed does not have the format of a valid ObjectName
  45. */
  46. public NamedObject(String objectName, Object object) throws MalformedObjectNameException{
  47. ObjectName objName= new ObjectName(objectName);
  48. if (objName.isPattern()) {
  49. throw new RuntimeOperationsException(new IllegalArgumentException("Invalid name->"+ objName.toString()));
  50. }
  51. this.name= objName;
  52. this.object= object;
  53. }
  54. /**
  55. * Compares the current object name with another object name.
  56. *
  57. * @param object The Named Object that the current object name is to be
  58. * compared with.
  59. *
  60. * @return True if the two named objects are equal, otherwise false.
  61. */
  62. public boolean equals(Object object) {
  63. if (this == object) return true;
  64. if (object == null) return false;
  65. if (!(object instanceof NamedObject)) return false;
  66. NamedObject no = (NamedObject) object;
  67. return name.equals(no.getName());
  68. }
  69. /**
  70. * Returns a hash code for this named object.
  71. *
  72. */
  73. public int hashCode() {
  74. return name.hashCode();
  75. }
  76. /**
  77. * Get the object name.
  78. */
  79. public ObjectName getName() {
  80. return name;
  81. }
  82. /**
  83. * Get the object
  84. */
  85. public Object getObject() {
  86. return object;
  87. }
  88. }