KickJava   Java API By Example, From Geeks To Geeks.

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


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