001: /*
002: * $Id: EnumItem.java,v 1.21 2007/03/15 17:08:30 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.types;
008:
009: import org.xins.common.MandatoryArgumentChecker;
010:
011: /**
012: * Item in an enumeration type.
013: *
014: * @version $Revision: 1.21 $ $Date: 2007/03/15 17:08:30 $
015: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
016: *
017: * @since XINS 1.0.0
018: *
019: * @see EnumType
020: */
021: public class EnumItem {
022:
023: /**
024: * The symbolic (friendly) name for the enumeration value. Cannot be
025: * <code>null</code>.
026: */
027: private final String _name;
028:
029: /**
030: * The actual value of this enumeration item. Cannot <code>null</code>.
031: */
032: private final String _value;
033:
034: /**
035: * Creates a new <code>EnumItem</code>.
036: *
037: * @param name
038: * the symbolic (friendly) name for the enumeration value, not
039: * <code>null</code>.
040: *
041: * @param value
042: * the actual value of the enumeration item, not <code>null</code>.
043: *
044: * @throws IllegalArgumentException
045: * if <code>name == null || value == null</code>.
046: */
047: public EnumItem(String name, String value)
048: throws IllegalArgumentException {
049: MandatoryArgumentChecker.check("name", name, "value", value);
050:
051: _name = name;
052: _value = value;
053: }
054:
055: /**
056: * Gets the symbolic (friendly) name for the enumeration value.
057: *
058: * @return
059: * the symbolic name, not <code>null</code>.
060: */
061: public final String getName() {
062: return _name;
063: }
064:
065: /**
066: * Gets the value for this enumeration item.
067: *
068: * @return
069: * the actual value of this enumeration item, not <code>null</code>.
070: */
071: public String getValue() {
072: return _value;
073: }
074:
075: /**
076: * Returns the hash code for this object.
077: *
078: * @return
079: * the hash code.
080: */
081: public int hashCode() {
082: return (_name == null ? 0 : _name.hashCode())
083: ^ (_value == null ? 0 : _value.hashCode());
084: }
085:
086: /**
087: * Checks if this object is considered equal to the specified object. In
088: * order for it to qualify as equals, it needs to be an instance of the
089: * very same class as this object (not even a subclass), and it needs to
090: * have the same name and value.
091: *
092: * @param obj
093: * the object to compare, or <code>null</code>.
094: *
095: * @return
096: * <code>true</code> if <code>obj</code> is considered equal to this
097: * instance, or <code>false</code> otherwise.
098: */
099: public boolean equals(Object obj) {
100:
101: // Check null and the class
102: if (obj == null || !(obj.getClass().equals(getClass()))) {
103: return false;
104: }
105:
106: // Check name and value
107: EnumItem other = (EnumItem) obj;
108: return (_name == null ? other.getName() == null : _name
109: .equals(other.getName()))
110: && (_value == null ? other.getValue() == null : _value
111: .equals(other.getValue()));
112: }
113:
114: /**
115: * Returns a textual representation of this object.
116: *
117: * @return
118: * a textual representation of this object, never <code>null</code>.
119: */
120: public String toString() {
121: return _value;
122: }
123: }
|