Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 239f0dbacb708070a80b685c7683b31ee31fae0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.activities;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.ui.activities.IIdentifier;
import org.eclipse.ui.activities.IIdentifierListener;
import org.eclipse.ui.activities.IdentifierEvent;
import org.eclipse.ui.internal.util.Util;

final class Identifier implements IIdentifier {
    private final static int HASH_FACTOR = 89;

    private final static int HASH_INITIAL = Identifier.class.getName()
            .hashCode();

    private final static Set strongReferences = new HashSet();

    private Set activityIds = Collections.EMPTY_SET;

    private transient String[] activityIdsAsArray = {};

    private boolean enabled;

    private transient int hashCode = HASH_INITIAL;

    private String id;

	private ListenerList identifierListeners;

    private transient String string;

    Identifier(String id) {
        if (id == null) {
			throw new NullPointerException();
		}

        this.id = id;
    }

    public void addIdentifierListener(IIdentifierListener identifierListener) {
        if (identifierListener == null) {
			throw new NullPointerException();
		}

        if (identifierListeners == null) {
			identifierListeners = new ListenerList();
		}

		identifierListeners.add(identifierListener);
        strongReferences.add(this);
    }

    public int compareTo(Object object) {
        Identifier castedObject = (Identifier) object;
        int compareTo = Util.compare(activityIdsAsArray,
                castedObject.activityIdsAsArray);

        if (compareTo == 0) {
            compareTo = Util.compare(enabled, castedObject.enabled);

            if (compareTo == 0) {
				compareTo = Util.compare(id, castedObject.id);
			}
        }

        return compareTo;
    }

    public boolean equals(Object object) {
        if (!(object instanceof Identifier)) {
			return false;
		}

        final Identifier castedObject = (Identifier) object;
        if (!Util.equals(activityIds, castedObject.activityIds)) {
            return false;
        }
        
        if (!Util.equals(enabled, castedObject.enabled)) {
            return false;
        }
        
        return Util.equals(id, castedObject.id);
    }

    void fireIdentifierChanged(IdentifierEvent identifierEvent) {
        if (identifierEvent == null) {
			throw new NullPointerException();
		}

        if (identifierListeners != null) {
			Object[] listeners = identifierListeners.getListeners();
			for (int i = 0; i < listeners.length; i++) {
				Object listener = listeners[i];
				((IIdentifierListener) listener).identifierChanged(identifierEvent);
			}
		}
    }

    public Set getActivityIds() {
        return activityIds;
    }

    public String getId() {
        return id;
    }

    public int hashCode() {
        if (hashCode == HASH_INITIAL) {
            hashCode = hashCode * HASH_FACTOR + Util.hashCode(activityIds);
            hashCode = hashCode * HASH_FACTOR + Util.hashCode(enabled);
            hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
            if (hashCode == HASH_INITIAL) {
				hashCode++;
			}
        }

        return hashCode;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void removeIdentifierListener(IIdentifierListener identifierListener) {
        if (identifierListener == null) {
			throw new NullPointerException();
		}

        if (identifierListeners != null) {
			identifierListeners.remove(identifierListener);
			if (identifierListeners.isEmpty()) {
				strongReferences.remove(this);
			}
		}
    }

    boolean setActivityIds(Set activityIds) {
        activityIds = Util.safeCopy(activityIds, String.class);

        if (!Util.equals(activityIds, this.activityIds)) {
            this.activityIds = activityIds;
            this.activityIdsAsArray = (String[]) this.activityIds
                    .toArray(new String[this.activityIds.size()]);
            hashCode = HASH_INITIAL;
            string = null;
            return true;
        }

        return false;
    }

    boolean setEnabled(boolean enabled) {
        if (enabled != this.enabled) {
            this.enabled = enabled;
            hashCode = HASH_INITIAL;
            string = null;
            return true;
        }

        return false;
    }

    public String toString() {
        if (string == null) {
            final StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append('[');
            stringBuffer.append(activityIds);
            stringBuffer.append(',');
            stringBuffer.append(enabled);
            stringBuffer.append(',');
            stringBuffer.append(id);
            stringBuffer.append(']');
            string = stringBuffer.toString();
        }

        return string;
    }
}

Back to the top