KickJava   Java API By Example, From Geeks To Geeks.

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


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