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 ActivityRequirementBindingDefinition {
021: private final static int HASH_FACTOR = 89;
022:
023: private final static int HASH_INITIAL = ActivityRequirementBindingDefinition.class
024: .getName().hashCode();
025:
026: static Map activityRequirementBindingDefinitionsByActivityId(
027: Collection activityRequirementBindingDefinitions) {
028: if (activityRequirementBindingDefinitions == null) {
029: throw new NullPointerException();
030: }
031:
032: Map map = new HashMap();
033: Iterator iterator = activityRequirementBindingDefinitions
034: .iterator();
035:
036: while (iterator.hasNext()) {
037: Object object = iterator.next();
038: Util.assertInstance(object,
039: ActivityRequirementBindingDefinition.class);
040: ActivityRequirementBindingDefinition activityRequirementBindingDefinition = (ActivityRequirementBindingDefinition) object;
041: String parentActivityId = activityRequirementBindingDefinition
042: .getActivityId();
043:
044: if (parentActivityId != null) {
045: Collection activityRequirementBindingDefinitions2 = (Collection) map
046: .get(parentActivityId);
047:
048: if (activityRequirementBindingDefinitions2 == null) {
049: activityRequirementBindingDefinitions2 = new HashSet();
050: map.put(parentActivityId,
051: activityRequirementBindingDefinitions2);
052: }
053:
054: activityRequirementBindingDefinitions2
055: .add(activityRequirementBindingDefinition);
056: }
057: }
058:
059: return map;
060: }
061:
062: private String requiredActivityId;
063:
064: private transient int hashCode = HASH_INITIAL;
065:
066: private String activityId;
067:
068: private String sourceId;
069:
070: private transient String string;
071:
072: public ActivityRequirementBindingDefinition(
073: String requiredActivityId, String activityId,
074: String sourceId) {
075: this .requiredActivityId = requiredActivityId;
076: this .activityId = activityId;
077: this .sourceId = sourceId;
078: }
079:
080: public int compareTo(Object object) {
081: ActivityRequirementBindingDefinition castedObject = (ActivityRequirementBindingDefinition) object;
082: int compareTo = Util.compare(requiredActivityId,
083: castedObject.requiredActivityId);
084:
085: if (compareTo == 0) {
086: compareTo = Util.compare(activityId,
087: castedObject.activityId);
088:
089: if (compareTo == 0) {
090: compareTo = Util.compare(sourceId,
091: castedObject.sourceId);
092: }
093: }
094:
095: return compareTo;
096: }
097:
098: public boolean equals(Object object) {
099: if (!(object instanceof ActivityRequirementBindingDefinition)) {
100: return false;
101: }
102:
103: final ActivityRequirementBindingDefinition castedObject = (ActivityRequirementBindingDefinition) object;
104: if (!Util.equals(requiredActivityId,
105: castedObject.requiredActivityId)) {
106: return false;
107: }
108:
109: if (!Util.equals(activityId, castedObject.activityId)) {
110: return false;
111: }
112:
113: return Util.equals(sourceId, castedObject.sourceId);
114: }
115:
116: public String getRequiredActivityId() {
117: return requiredActivityId;
118: }
119:
120: public String getActivityId() {
121: return activityId;
122: }
123:
124: public String getSourceId() {
125: return sourceId;
126: }
127:
128: public int hashCode() {
129: if (hashCode == HASH_INITIAL) {
130: hashCode = hashCode * HASH_FACTOR
131: + Util.hashCode(requiredActivityId);
132: hashCode = hashCode * HASH_FACTOR
133: + Util.hashCode(activityId);
134: hashCode = hashCode * HASH_FACTOR + Util.hashCode(sourceId);
135: if (hashCode == HASH_INITIAL) {
136: hashCode++;
137: }
138: }
139:
140: return hashCode;
141: }
142:
143: public String toString() {
144: if (string == null) {
145: final StringBuffer stringBuffer = new StringBuffer();
146: stringBuffer.append('[');
147: stringBuffer.append(requiredActivityId);
148: stringBuffer.append(',');
149: stringBuffer.append(activityId);
150: stringBuffer.append(',');
151: stringBuffer.append(sourceId);
152: stringBuffer.append(']');
153: string = stringBuffer.toString();
154: }
155:
156: return string;
157: }
158: }
|