KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.ui.activities.IIdentifier;
20 import org.eclipse.ui.activities.IIdentifierListener;
21 import org.eclipse.ui.activities.IdentifierEvent;
22 import org.eclipse.ui.internal.util.Util;
23
24 final class Identifier implements IIdentifier {
25     private final static int HASH_FACTOR = 89;
26
27     private final static int HASH_INITIAL = Identifier.class.getName()
28             .hashCode();
29
30     private final static Set JavaDoc strongReferences = new HashSet JavaDoc();
31
32     private Set JavaDoc activityIds;
33
34     private transient String JavaDoc[] activityIdsAsArray;
35
36     private boolean enabled;
37
38     private transient int hashCode = HASH_INITIAL;
39
40     private String JavaDoc id;
41
42     private List JavaDoc identifierListeners;
43
44     private transient String JavaDoc string;
45
46     Identifier(String JavaDoc id) {
47         if (id == null) {
48             throw new NullPointerException JavaDoc();
49         }
50
51         this.id = id;
52     }
53
54     public void addIdentifierListener(IIdentifierListener identifierListener) {
55         if (identifierListener == null) {
56             throw new NullPointerException JavaDoc();
57         }
58
59         if (identifierListeners == null) {
60             identifierListeners = new ArrayList JavaDoc();
61         }
62
63         if (!identifierListeners.contains(identifierListener)) {
64             identifierListeners.add(identifierListener);
65         }
66
67         strongReferences.add(this);
68     }
69
70     public int compareTo(Object JavaDoc object) {
71         Identifier castedObject = (Identifier) object;
72         int compareTo = Util.compare(activityIdsAsArray,
73                 castedObject.activityIdsAsArray);
74
75         if (compareTo == 0) {
76             compareTo = Util.compare(enabled, castedObject.enabled);
77
78             if (compareTo == 0) {
79                 compareTo = Util.compare(id, castedObject.id);
80             }
81         }
82
83         return compareTo;
84     }
85
86     public boolean equals(Object JavaDoc object) {
87         if (!(object instanceof Identifier)) {
88             return false;
89         }
90
91         final Identifier castedObject = (Identifier) object;
92         if (!Util.equals(activityIds, castedObject.activityIds)) {
93             return false;
94         }
95         
96         if (!Util.equals(enabled, castedObject.enabled)) {
97             return false;
98         }
99         
100         return Util.equals(id, castedObject.id);
101     }
102
103     void fireIdentifierChanged(IdentifierEvent identifierEvent) {
104         if (identifierEvent == null) {
105             throw new NullPointerException JavaDoc();
106         }
107
108         if (identifierListeners != null) {
109             for (int i = 0; i < identifierListeners.size(); i++) {
110                 ((IIdentifierListener) identifierListeners.get(i))
111                         .identifierChanged(identifierEvent);
112             }
113         }
114     }
115
116     public Set JavaDoc getActivityIds() {
117         return activityIds;
118     }
119
120     public String JavaDoc getId() {
121         return id;
122     }
123
124     public int hashCode() {
125         if (hashCode == HASH_INITIAL) {
126             hashCode = hashCode * HASH_FACTOR + Util.hashCode(activityIds);
127             hashCode = hashCode * HASH_FACTOR + Util.hashCode(enabled);
128             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
129             if (hashCode == HASH_INITIAL) {
130                 hashCode++;
131             }
132         }
133
134         return hashCode;
135     }
136
137     public boolean isEnabled() {
138         return enabled;
139     }
140
141     public void removeIdentifierListener(IIdentifierListener identifierListener) {
142         if (identifierListener == null) {
143             throw new NullPointerException JavaDoc();
144         }
145
146         if (identifierListeners != null) {
147             identifierListeners.remove(identifierListener);
148         }
149
150         if (identifierListeners.isEmpty()) {
151             strongReferences.remove(this);
152         }
153     }
154
155     boolean setActivityIds(Set JavaDoc activityIds) {
156         activityIds = Util.safeCopy(activityIds, String JavaDoc.class);
157
158         if (!Util.equals(activityIds, this.activityIds)) {
159             this.activityIds = activityIds;
160             this.activityIdsAsArray = (String JavaDoc[]) this.activityIds
161                     .toArray(new String JavaDoc[this.activityIds.size()]);
162             hashCode = HASH_INITIAL;
163             string = null;
164             return true;
165         }
166
167         return false;
168     }
169
170     boolean setEnabled(boolean enabled) {
171         if (enabled != this.enabled) {
172             this.enabled = enabled;
173             hashCode = HASH_INITIAL;
174             string = null;
175             return true;
176         }
177
178         return false;
179     }
180
181     public String JavaDoc toString() {
182         if (string == null) {
183             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
184             stringBuffer.append('[');
185             stringBuffer.append(activityIds);
186             stringBuffer.append(',');
187             stringBuffer.append(enabled);
188             stringBuffer.append(',');
189             stringBuffer.append(id);
190             stringBuffer.append(']');
191             string = stringBuffer.toString();
192         }
193
194         return string;
195     }
196 }
197
Popular Tags