001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.relation;
008:
009: import java.io.Serializable;
010: import java.util.ArrayList;
011: import java.util.Iterator;
012: import java.util.List;
013: import javax.management.ObjectName;
014:
015: /**
016: * @version $Revision: 1.11 $
017: */
018: public class Role implements Serializable {
019: private static final long serialVersionUID = -279985518429862552L;
020:
021: private String name;
022: private List objectNameList;
023:
024: public Role(String roleName, List roleValueList)
025: throws IllegalArgumentException {
026: setRoleName(roleName);
027: // loop and add the values to our global list
028: setRoleValue(roleValueList);
029: }
030:
031: public void setRoleName(String roleName)
032: throws IllegalArgumentException {
033: if (roleName == null)
034: throw new IllegalArgumentException(
035: "Cannot have a null role name");
036: this .name = roleName;
037: }
038:
039: public void setRoleValue(List roleValues)
040: throws IllegalArgumentException {
041: if (roleValues == null)
042: throw new IllegalArgumentException(
043: "List of role values cannot be null");
044: if (objectNameList == null) {
045: objectNameList = new ArrayList();
046: }
047: objectNameList.clear();
048: objectNameList.addAll(roleValues);
049: }
050:
051: public String getRoleName() {
052: return name;
053: }
054:
055: public List getRoleValue() {
056: return new ArrayList(objectNameList);
057: }
058:
059: public String toString() {
060: StringBuffer roleToString = new StringBuffer("roleName: ");
061: roleToString.append(name);
062: roleToString.append("\nroleValue: ");
063: String values = roleValueToString(objectNameList);
064: roleToString.append(values);
065: return roleToString.toString();
066: }
067:
068: public Object clone() {
069: try {
070: return new Role(name, objectNameList);
071: } catch (IllegalArgumentException ignored) {
072: // it never happens, no need to rethrow !
073: return null;
074: }
075: }
076:
077: public static String roleValueToString(List roleValues)
078: throws IllegalArgumentException {
079: StringBuffer valuesToString = new StringBuffer();
080: for (Iterator roleValuesIterator = roleValues.iterator(); roleValuesIterator
081: .hasNext();) {
082: ObjectName currentObjName = (ObjectName) roleValuesIterator
083: .next();
084: valuesToString.append(currentObjName.toString());
085:
086: if (roleValuesIterator.hasNext()) {
087: valuesToString.append("\n");
088: }
089: }
090: return valuesToString.toString();
091: }
092:
093: public boolean equals(Object o) {
094: if (this == o)
095: return true;
096: if (!(o instanceof Role))
097: return false;
098:
099: final Role role = (Role) o;
100:
101: if (name != null ? !name.equals(role.name) : role.name != null)
102: return false;
103: if (objectNameList != null ? !objectNameList
104: .equals(role.objectNameList)
105: : role.objectNameList != null)
106: return false;
107:
108: return true;
109: }
110:
111: public int hashCode() {
112: int result;
113: result = (name != null ? name.hashCode() : 0);
114: result = 29
115: * result
116: + (objectNameList != null ? objectNameList.hashCode()
117: : 0);
118: return result;
119: }
120: }
|