KickJava   Java API By Example, From Geeks To Geeks.

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


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

11
12 package org.eclipse.ui.internal.commands;
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 import java.util.Set JavaDoc;
20
21 import org.eclipse.ui.internal.util.Util;
22
23 public final class CategoryDefinition implements Comparable JavaDoc {
24
25     private final static int HASH_FACTOR = 89;
26     private final static int HASH_INITIAL =
27         CategoryDefinition.class.getName().hashCode();
28
29     public static Map JavaDoc categoryDefinitionsById(
30         Collection JavaDoc categoryDefinitions,
31         boolean allowNullIds) {
32         if (categoryDefinitions == null)
33             throw new NullPointerException JavaDoc();
34
35         Map JavaDoc map = new HashMap JavaDoc();
36         Iterator JavaDoc iterator = categoryDefinitions.iterator();
37
38         while (iterator.hasNext()) {
39             Object JavaDoc object = iterator.next();
40             Util.assertInstance(object, CategoryDefinition.class);
41             CategoryDefinition categoryDefinition =
42                 (CategoryDefinition) object;
43             String JavaDoc id = categoryDefinition.getId();
44
45             if (allowNullIds || id != null)
46                 map.put(id, categoryDefinition);
47         }
48
49         return map;
50     }
51
52     public static Map JavaDoc categoryDefinitionsByName(
53         Collection JavaDoc categoryDefinitions,
54         boolean allowNullNames) {
55         if (categoryDefinitions == null)
56             throw new NullPointerException JavaDoc();
57
58         Map JavaDoc map = new HashMap JavaDoc();
59         Iterator JavaDoc iterator = categoryDefinitions.iterator();
60
61         while (iterator.hasNext()) {
62             Object JavaDoc object = iterator.next();
63             Util.assertInstance(object, CategoryDefinition.class);
64             CategoryDefinition categoryDefinition =
65                 (CategoryDefinition) object;
66             String JavaDoc name = categoryDefinition.getName();
67
68             if (allowNullNames || name != null) {
69                 Set JavaDoc categoryDefinitions2 = (Set JavaDoc) map.get(name);
70
71                 if (categoryDefinitions2 == null) {
72                     categoryDefinitions2 = new HashSet JavaDoc();
73                     map.put(name, categoryDefinitions2);
74                 }
75
76                 categoryDefinitions2.add(categoryDefinition);
77             }
78         }
79
80         return map;
81     }
82
83     private String JavaDoc description;
84
85     private transient int hashCode;
86     private transient boolean hashCodeComputed;
87     private String JavaDoc id;
88     private String JavaDoc name;
89     private String JavaDoc sourceId;
90     private transient String JavaDoc string;
91
92     public CategoryDefinition(
93         String JavaDoc description,
94         String JavaDoc id,
95         String JavaDoc name,
96         String JavaDoc sourceId) {
97         this.description = description;
98         this.id = id;
99         this.name = name;
100         this.sourceId = sourceId;
101     }
102
103     public int compareTo(Object JavaDoc object) {
104         CategoryDefinition castedObject = (CategoryDefinition) object;
105         int compareTo = Util.compare(description, castedObject.description);
106
107         if (compareTo == 0) {
108             compareTo = Util.compare(id, castedObject.id);
109
110             if (compareTo == 0) {
111                 compareTo = Util.compare(name, castedObject.name);
112
113                 if (compareTo == 0)
114                     compareTo = Util.compare(sourceId, castedObject.sourceId);
115             }
116         }
117
118         return compareTo;
119     }
120
121     public boolean equals(Object JavaDoc object) {
122         if (!(object instanceof CategoryDefinition))
123             return false;
124
125         CategoryDefinition castedObject = (CategoryDefinition) object;
126         boolean equals = true;
127         equals &= Util.equals(description, castedObject.description);
128         equals &= Util.equals(id, castedObject.id);
129         equals &= Util.equals(name, castedObject.name);
130         equals &= Util.equals(sourceId, castedObject.sourceId);
131         return equals;
132     }
133
134     public String JavaDoc getDescription() {
135         return description;
136     }
137
138     public String JavaDoc getId() {
139         return id;
140     }
141
142     public String JavaDoc getName() {
143         return name;
144     }
145
146     public String JavaDoc getSourceId() {
147         return sourceId;
148     }
149
150     public int hashCode() {
151         if (!hashCodeComputed) {
152             hashCode = HASH_INITIAL;
153             hashCode = hashCode * HASH_FACTOR + Util.hashCode(description);
154             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
155             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
156             hashCode = hashCode * HASH_FACTOR + Util.hashCode(sourceId);
157             hashCodeComputed = true;
158         }
159
160         return hashCode;
161     }
162
163     public String JavaDoc toString() {
164         if (string == null) {
165             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
166             stringBuffer.append('[');
167             stringBuffer.append(description);
168             stringBuffer.append(',');
169             stringBuffer.append(id);
170             stringBuffer.append(',');
171             stringBuffer.append(name);
172             stringBuffer.append(',');
173             stringBuffer.append(sourceId);
174             stringBuffer.append(']');
175             string = stringBuffer.toString();
176         }
177
178         return string;
179     }
180 }
181
Popular Tags