KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > contexts > ContextDefinition


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.contexts;
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 final class ContextDefinition implements Comparable JavaDoc {
23     private final static int HASH_FACTOR = 89;
24     private final static int HASH_INITIAL =
25         ContextDefinition.class.getName().hashCode();
26
27     static Map JavaDoc contextDefinitionsById(
28         Collection JavaDoc contextDefinitions,
29         boolean allowNullIds) {
30         if (contextDefinitions == null)
31             throw new NullPointerException JavaDoc();
32
33         Map JavaDoc map = new HashMap JavaDoc();
34         Iterator JavaDoc iterator = contextDefinitions.iterator();
35
36         while (iterator.hasNext()) {
37             Object JavaDoc object = iterator.next();
38             Util.assertInstance(object, ContextDefinition.class);
39             ContextDefinition contextDefinition = (ContextDefinition) object;
40             String JavaDoc id = contextDefinition.getId();
41
42             if (allowNullIds || id != null)
43                 map.put(id, contextDefinition);
44         }
45
46         return map;
47     }
48
49     static Map JavaDoc contextDefinitionsByName(
50         Collection JavaDoc contextDefinitions,
51         boolean allowNullNames) {
52         if (contextDefinitions == null)
53             throw new NullPointerException JavaDoc();
54
55         Map JavaDoc map = new HashMap JavaDoc();
56         Iterator JavaDoc iterator = contextDefinitions.iterator();
57
58         while (iterator.hasNext()) {
59             Object JavaDoc object = iterator.next();
60             Util.assertInstance(object, ContextDefinition.class);
61             ContextDefinition contextDefinition = (ContextDefinition) object;
62             String JavaDoc name = contextDefinition.getName();
63
64             if (allowNullNames || name != null) {
65                 Collection JavaDoc contextDefinitions2 = (Collection JavaDoc) map.get(name);
66
67                 if (contextDefinitions2 == null) {
68                     contextDefinitions2 = new HashSet JavaDoc();
69                     map.put(name, contextDefinitions2);
70                 }
71
72                 contextDefinitions2.add(contextDefinition);
73             }
74         }
75
76         return map;
77     }
78
79     private transient int hashCode;
80     private transient boolean hashCodeComputed;
81     private String JavaDoc id;
82     private String JavaDoc name;
83     private String JavaDoc parentId;
84     private String JavaDoc sourceId;
85     private transient String JavaDoc string;
86
87     ContextDefinition(
88         String JavaDoc id,
89         String JavaDoc name,
90         String JavaDoc parentId,
91         String JavaDoc sourceId) {
92         this.id = id;
93         this.name = name;
94         this.parentId = parentId;
95         this.sourceId = sourceId;
96     }
97
98     public int compareTo(Object JavaDoc object) {
99         ContextDefinition castedObject = (ContextDefinition) object;
100         int compareTo = Util.compare(id, castedObject.id);
101
102         if (compareTo == 0) {
103             compareTo = Util.compare(name, castedObject.name);
104
105             if (compareTo == 0) {
106                 compareTo = Util.compare(parentId, castedObject.parentId);
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 ContextDefinition))
118             return false;
119
120         ContextDefinition castedObject = (ContextDefinition) object;
121         boolean equals = true;
122         equals &= Util.equals(id, castedObject.id);
123         equals &= Util.equals(name, castedObject.name);
124         equals &= Util.equals(parentId, castedObject.parentId);
125         equals &= Util.equals(sourceId, castedObject.sourceId);
126         return equals;
127     }
128
129     public String JavaDoc getId() {
130         return id;
131     }
132
133     public String JavaDoc getName() {
134         return name;
135     }
136
137     public String JavaDoc getParentId() {
138         return parentId;
139     }
140
141     public String JavaDoc getSourceId() {
142         return sourceId;
143     }
144
145     public int hashCode() {
146         if (!hashCodeComputed) {
147             hashCode = HASH_INITIAL;
148             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
149             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
150             hashCode = hashCode * HASH_FACTOR + Util.hashCode(parentId);
151             hashCode = hashCode * HASH_FACTOR + Util.hashCode(sourceId);
152             hashCodeComputed = true;
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(parentId);
167             stringBuffer.append(',');
168             stringBuffer.append(sourceId);
169             stringBuffer.append(']');
170             string = stringBuffer.toString();
171         }
172
173         return string;
174     }
175 }
176
Popular Tags