Skip to main content
summaryrefslogtreecommitdiffstats
blob: b663540ecf48b911799f684cae0f3265afe275b3 (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
/*******************************************************************************
 * Copyright (c) 2012, 2013 Obeo 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.match.eobject;

import java.util.Map;

import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.match.eobject.ProximityEObjectMatcher.DistanceFunction;
import org.eclipse.emf.compare.match.eobject.internal.AccessBasedLRUCache;
import org.eclipse.emf.ecore.EObject;

/**
 * This class wraps a DistanceFunction and cache its result. Any call to distance(a,b) will be cached and the
 * same value will be returned to distance(b,a).
 * 
 * @author <a href="mailto:cedric.brun@obeo.fr">Cedric Brun</a>
 */
public class CachingDistance implements DistanceFunction {

	/**
	 * The wrapped function.
	 */
	private DistanceFunction wrapped;

	/**
	 * The cache keeping the previous results.
	 */
	private Map<Pair, Double> distanceCache;

	/**
	 * Create a new caching distance.
	 * 
	 * @param wrapped
	 *            actual distance function to cache results from.
	 */
	public CachingDistance(DistanceFunction wrapped) {
		this.wrapped = wrapped;
		distanceCache = new AccessBasedLRUCache<Pair, Double>(10000, 1000, .75F);
	}

	/**
	 * {@inheritDoc}
	 */
	public double distance(Comparison inProgress, EObject a, EObject b) {
		Pair key = new Pair(a, b);
		Double previousResult = distanceCache.get(key);
		if (previousResult == null) {
			double dist = wrapped.distance(inProgress, a, b);
			distanceCache.put(key, dist);
			// cache it
			return dist;
		}
		return previousResult.doubleValue();
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean areIdentic(Comparison inProgress, EObject a, EObject b) {
		return wrapped.areIdentic(inProgress, a, b);
	}

	/**
	 * A class used as a key for two EObjects. Pair(a,b) and Pair(b,a) should be equals and have the same
	 * hashcodes
	 */
	class Pair {
		// CHECKSTYLE:OFF
		EObject a;

		EObject b;

		public Pair(EObject a, EObject b) {
			super();
			this.a = a;
			this.b = b;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			int first = a.hashCode();
			int second = b.hashCode();
			if (first > second) {
				int tmp = first;
				first = second;
				second = tmp;
			}
			result = prime * result + first;
			result = prime * result + second;
			return result;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (getClass() != obj.getClass()) {
				return false;
			}
			Pair other = (Pair)obj;
			if (!getOuterType().equals(other.getOuterType())) {
				return false;
			}
			return (a == other.a && b == other.b) || (b == other.a && a == other.b);

		}

		private CachingDistance getOuterType() {
			return CachingDistance.this;
		}

	}
	// CHECKSTYLE:ON

}

Back to the top