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