001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.activities;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.List;
015: import java.util.Set;
016:
017: import org.eclipse.ui.activities.IIdentifier;
018: import org.eclipse.ui.activities.IIdentifierListener;
019: import org.eclipse.ui.activities.IdentifierEvent;
020: import org.eclipse.ui.internal.util.Util;
021:
022: final class Identifier implements IIdentifier {
023: private final static int HASH_FACTOR = 89;
024:
025: private final static int HASH_INITIAL = Identifier.class.getName()
026: .hashCode();
027:
028: private final static Set strongReferences = new HashSet();
029:
030: private Set activityIds;
031:
032: private transient String[] activityIdsAsArray;
033:
034: private boolean enabled;
035:
036: private transient int hashCode = HASH_INITIAL;
037:
038: private String id;
039:
040: private List identifierListeners;
041:
042: private transient String string;
043:
044: Identifier(String id) {
045: if (id == null) {
046: throw new NullPointerException();
047: }
048:
049: this .id = id;
050: }
051:
052: public void addIdentifierListener(
053: IIdentifierListener identifierListener) {
054: if (identifierListener == null) {
055: throw new NullPointerException();
056: }
057:
058: if (identifierListeners == null) {
059: identifierListeners = new ArrayList();
060: }
061:
062: if (!identifierListeners.contains(identifierListener)) {
063: identifierListeners.add(identifierListener);
064: }
065:
066: strongReferences.add(this );
067: }
068:
069: public int compareTo(Object object) {
070: Identifier castedObject = (Identifier) object;
071: int compareTo = Util.compare(activityIdsAsArray,
072: castedObject.activityIdsAsArray);
073:
074: if (compareTo == 0) {
075: compareTo = Util.compare(enabled, castedObject.enabled);
076:
077: if (compareTo == 0) {
078: compareTo = Util.compare(id, castedObject.id);
079: }
080: }
081:
082: return compareTo;
083: }
084:
085: public boolean equals(Object object) {
086: if (!(object instanceof Identifier)) {
087: return false;
088: }
089:
090: final Identifier castedObject = (Identifier) object;
091: if (!Util.equals(activityIds, castedObject.activityIds)) {
092: return false;
093: }
094:
095: if (!Util.equals(enabled, castedObject.enabled)) {
096: return false;
097: }
098:
099: return Util.equals(id, castedObject.id);
100: }
101:
102: void fireIdentifierChanged(IdentifierEvent identifierEvent) {
103: if (identifierEvent == null) {
104: throw new NullPointerException();
105: }
106:
107: if (identifierListeners != null) {
108: for (int i = 0; i < identifierListeners.size(); i++) {
109: ((IIdentifierListener) identifierListeners.get(i))
110: .identifierChanged(identifierEvent);
111: }
112: }
113: }
114:
115: public Set getActivityIds() {
116: return activityIds;
117: }
118:
119: public String getId() {
120: return id;
121: }
122:
123: public int hashCode() {
124: if (hashCode == HASH_INITIAL) {
125: hashCode = hashCode * HASH_FACTOR
126: + 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(
142: IIdentifierListener identifierListener) {
143: if (identifierListener == null) {
144: throw new NullPointerException();
145: }
146:
147: if (identifierListeners != null) {
148: identifierListeners.remove(identifierListener);
149: }
150:
151: if (identifierListeners.isEmpty()) {
152: strongReferences.remove(this );
153: }
154: }
155:
156: boolean setActivityIds(Set activityIds) {
157: activityIds = Util.safeCopy(activityIds, String.class);
158:
159: if (!Util.equals(activityIds, this .activityIds)) {
160: this .activityIds = activityIds;
161: this .activityIdsAsArray = (String[]) this .activityIds
162: .toArray(new String[this .activityIds.size()]);
163: hashCode = HASH_INITIAL;
164: string = null;
165: return true;
166: }
167:
168: return false;
169: }
170:
171: boolean setEnabled(boolean enabled) {
172: if (enabled != this .enabled) {
173: this .enabled = enabled;
174: hashCode = HASH_INITIAL;
175: string = null;
176: return true;
177: }
178:
179: return false;
180: }
181:
182: public String toString() {
183: if (string == null) {
184: final StringBuffer stringBuffer = new StringBuffer();
185: stringBuffer.append('[');
186: stringBuffer.append(activityIds);
187: stringBuffer.append(',');
188: stringBuffer.append(enabled);
189: stringBuffer.append(',');
190: stringBuffer.append(id);
191: stringBuffer.append(']');
192: string = stringBuffer.toString();
193: }
194:
195: return string;
196: }
197: }
|