Skip to main content
summaryrefslogtreecommitdiffstats
blob: a56159c1eec3a590ef8b223cd3903d304096fe52 (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
/*******************************************************************************
 * 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;
/*


 */

import java.beans.*;
import java.util.*;
/**
 * Equality tester for MethodDescriptors
 */
public class MethodDescriptorEquality extends FeatureDescriptorEquality {
	
	static void INIT() {
		try {
			MAP_EQUALITY.put(MethodDescriptor.class, (MethodDescriptorEquality.class).getConstructor(new Class[] {MethodDescriptor.class}));
		} catch (NoSuchMethodException e) {
		}
	}	
	
	private ArrayList fParameterDescriptors;	// Array of ParameterDescriptorEquality's.
	
	public MethodDescriptorEquality() {
	}
	
	public MethodDescriptorEquality(MethodDescriptor descr) {
		super(descr);
	}
	
	/**
	 * 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() {
		super.clearFeature();
		fParameterDescriptors = null;
	}	
	
	protected ArrayList parameterDescriptors() {
		if (fParameterDescriptors == null) {
			ParameterDescriptor[] pds = ((MethodDescriptor) fFeature).getParameterDescriptors();
			if (pds != null) {			
				fParameterDescriptors = new ArrayList(pds.length);
				for (int i=0; i<pds.length; i++)
					fParameterDescriptors.add(new ParameterDescriptorEquality(pds[i]));
			}
		}
		return fParameterDescriptors;
	}
	/**
	 * 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 = super.calculateHashCode();
		MethodDescriptor bd = (MethodDescriptor) fFeature;
		int hc = bd.getMethod().hashCode();
		if (parameterDescriptors() != null)
			hc += parameterDescriptors().hashCode(); 

		return hashcode*31 + hc;
	}

	public boolean equals(Object obj) {
		if (identityTest(obj))
			return true;
			
		if (!super.equals(obj))
			return false;

		MethodDescriptorEquality oem = (MethodDescriptorEquality) obj;
		MethodDescriptor om = (MethodDescriptor) oem.fFeature;
		MethodDescriptor fm = (MethodDescriptor) fFeature;
 		
		if (fm.getMethod() != om.getMethod())
			return false;
				
		if (fParameterDescriptors != null || oem.fParameterDescriptors != null) {
			// We are in a Map lookup situation, so one side has listener method equalities, so we will compare that way.
			if (parameterDescriptors() == null)
				if (((MethodDescriptorEquality) obj).parameterDescriptors() != null)
					return false;
				else ;
			else
				if (!parameterDescriptors().equals(((MethodDescriptorEquality) obj).parameterDescriptors()))
					return false;
		} else {
			// We are in the building the list phases, don't waste space building entire list.
			ParameterDescriptor[] ours = fm.getParameterDescriptors();
			ParameterDescriptor[] theirs = om.getParameterDescriptors();
			if (ours == theirs)
				return true;
			else if (ours == null)
				if (theirs != null)
					return false;
				else
					;
			else if (theirs == null)
				return false;
			else if (ours.length != theirs.length)
				return false;
			else if (ours.length > 0) {
				ParameterDescriptorEquality workingOurs = new ParameterDescriptorEquality();
				ParameterDescriptorEquality workingThiers = new ParameterDescriptorEquality();
				for (int i = 0; i < ours.length; i++) {
					workingOurs.setFeature(ours[i]);
					workingThiers.setFeature(theirs[i]);
					if (!workingOurs.equals(workingThiers))
						return false;
				}
			}			
		}
				
 			
		return true;
	}
}

Back to the top