Skip to main content
summaryrefslogtreecommitdiffstats
blob: e06cc023abef2f654bd8fe415fe9ae06c4504666 (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
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2001, 2005 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.jem.internal.beaninfo.vm;
/*
 *  $RCSfile: FeatureDescriptorEquality.java,v $
 *  $Revision: 1.5 $  $Date: 2005/08/24 20:31:29 $ 
 */

import java.beans.*;
import java.util.*;
import java.lang.reflect.*;
/**
 * This object is used to test for semantic equality (equals())
 * between feature objects. It is needed because Feature's don't
 * provide a semantic equality, only an identity equality. Need
 * semantic equality so that keys in map can be found equal
 * semantically.
 */

public class FeatureDescriptorEquality {
	protected FeatureDescriptor fFeature;
	
	private HashMap fValues;	// The key/values for this feature. We grab them out
								// so that we don't have to keep re-getting them for equality
								// compares. This is done the first time needed in the equals
								// statement.
								
	private int fHashCode = 0;	// Hashcode of this equality object. The hashcode for the feature is expensive
					// so we will calculate it once (the assumption is that
					// features once created aren't changed, which for our
					// purposes here in beaninfo land is good).
		
	protected static HashMap MAP_EQUALITY = new HashMap(10);	// Map from descriptor to equality type.
	static {
		try {
			MAP_EQUALITY.put(FeatureDescriptor.class, (FeatureDescriptorEquality.class).getConstructor(new Class[] {FeatureDescriptor.class}));
		} catch (NoSuchMethodException e) {
		}
		// Need to set up the others.
		BeanDescriptorEquality.INIT();
		EventSetDescriptorEquality.INIT();		
		IndexedPropertyDescriptorEquality.INIT();
		MethodDescriptorEquality.INIT();		
		ParameterDescriptorEquality.INIT();
	}
			
	/**
	 * Create the appropriate descriptor equality for this object.
	 */
	public static FeatureDescriptorEquality createEquality(FeatureDescriptor descr) {
		try {
			return (FeatureDescriptorEquality) ((Constructor) MAP_EQUALITY.get(descr.getClass())).newInstance(new Object[] {descr});
		} catch (IllegalAccessException e) {
		} catch (InstantiationException e) {
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
		
	public FeatureDescriptorEquality() {
	}
	
	/**
	 * NOTE: Every subclass needs to implement an override for the methods:
	 *       calculateHashCode
	 *       equals
	 *       clearFeature - if it has any cache values
	 */
					
	public FeatureDescriptorEquality(FeatureDescriptor feature) {
		setFeature(feature);
	}
	
	public final void setFeature(FeatureDescriptor feature) {
		clearFeature();
		fFeature = feature;
	}
	
	/**
	 * A new feature is being set into this object,
	 * clear any cache members so that they can be reconstructed.
	 *
	 * NOTE: Subclasses - remember to call super.clearFeature();
	 */
	protected void clearFeature() {
		fValues = null;
		fHashCode = 0;
	}
	
	public final int hashCode() {
		if (fHashCode == 0)
			fHashCode = calculateHashCode();
		return fHashCode;
	}

	protected final HashMap values() {
		if (fValues == null) {
			fValues = new HashMap(5);
			
			Enumeration keys = fFeature.attributeNames();
			while (keys.hasMoreElements()) {
				String key = (String) keys.nextElement();
				fValues.put(key, fFeature.getValue(key));
			}
		}
		return fValues;
	}
	
	/**
	 * Calculate the hashcode for the current feature, add this
	 * to the hashcode received from super.calculateHashCode
	 * and return the new value.
	 *
	 * NOTE: for subclasses, it is MANDITORY that the first line be:
	 *         int hashcode = super.calculateHashCode();
	 *       and the last line be:
	 *         return hashcode*31 + (your calculated hashcode for just this subclass);
	 */
	protected int calculateHashCode() {
		int hashcode = 0;
		if (fFeature.getName() != null)
			hashcode += fFeature.getName().hashCode();
			
		if (fFeature.getDisplayName() != fFeature.getName())
			hashcode += fFeature.getDisplayName().hashCode();
		if (fFeature.getShortDescription() != fFeature.getDisplayName())
			hashcode += fFeature.getShortDescription().hashCode();			
			
		hashcode += (fFeature.isExpert() ? Boolean.TRUE : Boolean.FALSE).hashCode();
		hashcode += (fFeature.isHidden() ? Boolean.TRUE : Boolean.FALSE).hashCode();		
		hashcode += (fFeature.isPreferred() ? Boolean.TRUE : Boolean.FALSE).hashCode();
		
		hashcode += values().hashCode();
		return hashcode;
	}
	
	/**
	 * equals: See if this is equal semantically.
	 *
	 * NOTE: Every subclass needs to implement this and
	 *       the first few lines should be:
	 *         if (identityTest())
	 *           return true;
	 *         if (!super.equals(obj))
	 *           return false;
	 */
	 
	 public boolean equals(Object obj) {
		if (identityTest(obj))
			return true;
		if (!(obj instanceof FeatureDescriptorEquality))
			return false;
			
		FeatureDescriptorEquality ofe = (FeatureDescriptorEquality) obj;	 		
	 	FeatureDescriptor of = ofe.fFeature;
	 		
	 	if (fFeature.getClass() != of.getClass())
	 		return false;
	 		
	 	if (!fFeature.getName().equals(of.getName()))
	 		return false;
	 	if (!fFeature.getDisplayName().equals(of.getDisplayName()))
	 		return false;
	 	if (!fFeature.getShortDescription().equals(of.getShortDescription()))
	 		return false;	 
	 	if (fFeature.isExpert() != of.isExpert() ||
		 		fFeature.isHidden() != of.isHidden() ||
		 		fFeature.isPreferred() != of.isPreferred())
	 		return false;
	 	
		if (!values().equals(ofe.values()))
			return false;
		return true;
	 }
	 
	 /*
	  * Identity test: Tests for quick identity of
	  * descriptors. If this returns true, then 
	  * no other tests required.
	  */
	 protected boolean identityTest(Object obj) {
	 	if (!(obj instanceof FeatureDescriptorEquality))
	 		return false;
	 	if (this == obj)
	 		return true;

	 	if (((FeatureDescriptorEquality) obj).fFeature == fFeature)
	 		return true;
	 		
	 	return false;	 	
	 }	
}

Back to the top