KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > CategoryDefinition


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.activities;
13
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.ui.internal.util.Util;
21
22 public final class CategoryDefinition implements Comparable JavaDoc {
23     private final static int HASH_FACTOR = 89;
24
25     private final static int HASH_INITIAL = CategoryDefinition.class.getName()
26             .hashCode();
27
28     static Map JavaDoc categoryDefinitionsById(Collection JavaDoc categoryDefinitions,
29             boolean allowNullIds) {
30         if (categoryDefinitions == null) {
31             throw new NullPointerException JavaDoc();
32         }
33
34         Map JavaDoc map = new HashMap JavaDoc();
35         Iterator JavaDoc iterator = categoryDefinitions.iterator();
36
37         while (iterator.hasNext()) {
38             Object JavaDoc object = iterator.next();
39             Util.assertInstance(object, CategoryDefinition.class);
40             CategoryDefinition categoryDefinition = (CategoryDefinition) object;
41             String JavaDoc id = categoryDefinition.getId();
42
43             if (allowNullIds || id != null) {
44                 map.put(id, categoryDefinition);
45             }
46         }
47
48         return map;
49     }
50
51     static Map JavaDoc categoryDefinitionsByName(Collection JavaDoc categoryDefinitions,
52             boolean allowNullNames) {
53         if (categoryDefinitions == null) {
54             throw new NullPointerException JavaDoc();
55         }
56
57         Map JavaDoc map = new HashMap JavaDoc();
58         Iterator JavaDoc iterator = categoryDefinitions.iterator();
59
60         while (iterator.hasNext()) {
61             Object JavaDoc object = iterator.next();
62             Util.assertInstance(object, CategoryDefinition.class);
63             CategoryDefinition categoryDefinition = (CategoryDefinition) object;
64             String JavaDoc name = categoryDefinition.getName();
65
66             if (allowNullNames || name != null) {
67                 Collection JavaDoc categoryDefinitions2 = (Collection JavaDoc) map.get(name);
68
69                 if (categoryDefinitions2 == null) {
70                     categoryDefinitions2 = new HashSet JavaDoc();
71                     map.put(name, categoryDefinitions2);
72                 }
73
74                 categoryDefinitions2.add(categoryDefinition);
75             }
76         }
77
78         return map;
79     }
80
81     private transient int hashCode = HASH_INITIAL;
82
83     private String JavaDoc id;
84
85     private String JavaDoc name;
86
87     private String JavaDoc sourceId;
88
89     private transient String JavaDoc string;
90
91     private String JavaDoc description;
92
93     public CategoryDefinition(String JavaDoc id, String JavaDoc name, String JavaDoc sourceId,
94             String JavaDoc description) {
95         this.id = id;
96         this.name = name;
97         this.sourceId = sourceId;
98         this.description = description;
99     }
100
101     public int compareTo(Object JavaDoc object) {
102         CategoryDefinition castedObject = (CategoryDefinition) object;
103         int compareTo = Util.compare(id, castedObject.id);
104
105         if (compareTo == 0) {
106             compareTo = Util.compare(name, castedObject.name);
107
108             if (compareTo == 0) {
109                 compareTo = Util.compare(sourceId, castedObject.sourceId);
110             }
111         }
112
113         return compareTo;
114     }
115
116     public boolean equals(Object JavaDoc object) {
117         if (!(object instanceof CategoryDefinition)) {
118             return false;
119         }
120
121         final CategoryDefinition castedObject = (CategoryDefinition) 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 JavaDoc getId() {
134         return id;
135     }
136
137     public String JavaDoc getName() {
138         return name;
139     }
140
141     public String JavaDoc 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 JavaDoc toString() {
159         if (string == null) {
160             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
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 JavaDoc getDescription() {
175         return description;
176     }
177 }
178
Popular Tags