001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.activities;
011:
012: import java.util.Collection;
013: import java.util.HashMap;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.Map;
017:
018: import org.eclipse.ui.internal.util.Util;
019:
020: public final class ActivityDefinition implements Comparable {
021: private final static int HASH_FACTOR = 89;
022:
023: private final static int HASH_INITIAL = ActivityDefinition.class
024: .getName().hashCode();
025:
026: static Map activityDefinitionsById(Collection activityDefinitions,
027: boolean allowNullIds) {
028: if (activityDefinitions == null) {
029: throw new NullPointerException();
030: }
031:
032: Map map = new HashMap();
033: Iterator iterator = activityDefinitions.iterator();
034:
035: while (iterator.hasNext()) {
036: Object object = iterator.next();
037: Util.assertInstance(object, ActivityDefinition.class);
038: ActivityDefinition activityDefinition = (ActivityDefinition) object;
039: String id = activityDefinition.getId();
040:
041: if (allowNullIds || id != null) {
042: map.put(id, activityDefinition);
043: }
044: }
045:
046: return map;
047: }
048:
049: static Map activityDefinitionsByName(
050: Collection activityDefinitions, boolean allowNullNames) {
051: if (activityDefinitions == null) {
052: throw new NullPointerException();
053: }
054:
055: Map map = new HashMap();
056: Iterator iterator = activityDefinitions.iterator();
057:
058: while (iterator.hasNext()) {
059: Object object = iterator.next();
060: Util.assertInstance(object, ActivityDefinition.class);
061: ActivityDefinition activityDefinition = (ActivityDefinition) object;
062: String name = activityDefinition.getName();
063:
064: if (allowNullNames || name != null) {
065: Collection activityDefinitions2 = (Collection) map
066: .get(name);
067:
068: if (activityDefinitions2 == null) {
069: activityDefinitions2 = new HashSet();
070: map.put(name, activityDefinitions2);
071: }
072:
073: activityDefinitions2.add(activityDefinition);
074: }
075: }
076:
077: return map;
078: }
079:
080: private transient int hashCode = HASH_INITIAL;
081:
082: private String id;
083:
084: private String name;
085:
086: private String sourceId;
087:
088: private String description;
089:
090: private transient String string;
091:
092: public ActivityDefinition(String id, String name, String sourceId,
093: String description) {
094: this .id = id;
095: this .name = name;
096: this .sourceId = sourceId;
097: this .description = description;
098: }
099:
100: public int compareTo(Object object) {
101: ActivityDefinition castedObject = (ActivityDefinition) object;
102: int compareTo = Util.compare(id, castedObject.id);
103:
104: if (compareTo == 0) {
105: compareTo = Util.compare(name, castedObject.name);
106:
107: if (compareTo == 0) {
108: compareTo = Util.compare(sourceId,
109: castedObject.sourceId);
110: }
111: }
112:
113: return compareTo;
114: }
115:
116: public boolean equals(Object object) {
117: if (!(object instanceof ActivityDefinition)) {
118: return false;
119: }
120:
121: final ActivityDefinition castedObject = (ActivityDefinition) object;
122: if (!Util.equals(id, castedObject.id)) {
123: return false;
124: }
125:
126: if (!Util.equals(name, castedObject.name)) {
127: return false;
128: }
129:
130: return Util.equals(sourceId, castedObject.sourceId);
131: }
132:
133: public String getId() {
134: return id;
135: }
136:
137: public String getName() {
138: return name;
139: }
140:
141: public String getSourceId() {
142: return sourceId;
143: }
144:
145: public int hashCode() {
146: if (hashCode == HASH_INITIAL) {
147: hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
148: hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
149: hashCode = hashCode * HASH_FACTOR + Util.hashCode(sourceId);
150: if (hashCode == HASH_INITIAL) {
151: hashCode++;
152: }
153: }
154:
155: return hashCode;
156: }
157:
158: public String toString() {
159: if (string == null) {
160: final StringBuffer stringBuffer = new StringBuffer();
161: stringBuffer.append('[');
162: stringBuffer.append(id);
163: stringBuffer.append(',');
164: stringBuffer.append(name);
165: stringBuffer.append(',');
166: stringBuffer.append(sourceId);
167: stringBuffer.append(']');
168: string = stringBuffer.toString();
169: }
170:
171: return string;
172: }
173:
174: public String getDescription() {
175: return description;
176: }
177: }
|