01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package javax.management;
10:
11: import java.io.Serializable;
12:
13: /**
14: * @version $Revision: 1.8 $
15: */
16: public class Attribute implements Serializable {
17: private static final long serialVersionUID = 2484220110589082382L;
18:
19: /**
20: * @serial The attribute's name
21: */
22: private final String name;
23: /**
24: * @serial The attribute's value
25: */
26: private final Object value;
27:
28: private transient int hash;
29:
30: public Attribute(String name, Object value) {
31: if (name == null)
32: throw new RuntimeOperationsException(
33: new IllegalArgumentException(
34: "The name of an attribute cannot be null"));
35:
36: this .name = name;
37: this .value = value;
38: }
39:
40: public boolean equals(Object obj) {
41: if (obj == null)
42: return false;
43: if (obj == this )
44: return true;
45:
46: try {
47: Attribute other = (Attribute) obj;
48: boolean namesEqual = name.equals(other.name);
49: boolean valuesEqual = false;
50: if (value == null)
51: valuesEqual = other.value == null;
52: else
53: valuesEqual = value.equals(other.value);
54: return namesEqual && valuesEqual;
55: } catch (ClassCastException ignored) {
56: }
57: return false;
58: }
59:
60: public int hashCode() {
61: if (hash == 0)
62: hash = computeHash();
63: return hash;
64: }
65:
66: public String getName() {
67: return name;
68: }
69:
70: public Object getValue() {
71: return value;
72: }
73:
74: public String toString() {
75: return new StringBuffer("Attribute's name: ").append(getName())
76: .append(", value: ").append(getValue()).toString();
77: }
78:
79: private int computeHash() {
80: int hash = name.hashCode();
81: if (value != null)
82: hash ^= value.hashCode();
83: return hash;
84: }
85: }
|