aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java')
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java
new file mode 100644
index 00000000..49af05d1
--- /dev/null
+++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java
@@ -0,0 +1,63 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2015, Andras Szabolcs Nagy and Daniel Varro
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v. 2.0 which is available at
5 * http://www.eclipse.org/legal/epl-v20.html.
6 *
7 * SPDX-License-Identifier: EPL-2.0
8 *******************************************************************************/
9package org.eclipse.viatra.dse.util;
10
11import java.util.Comparator;
12import java.util.HashMap;
13import java.util.Map;
14import java.util.TreeMap;
15
16import org.eclipse.emf.ecore.EObject;
17
18import com.google.common.base.Functions;
19import com.google.common.collect.Ordering;
20
21/**
22 *
23 * This custom {@link TreeMap} implementation enables to store {@link EObject}-{@link String} pairs sorted by values
24 * (strings). It works as expected if the map is modified in any way, hence the map will still be sorted by values on
25 * the new set of entries.
26 *
27 * It is allowed to have two entries with the same EObject key (and also with same values).
28 *
29 * The short coming of the class is that EObjects are compared to each other by their
30 * {@link System#identityHashCode(Object)}, which may lead to unexpected errors.
31 *
32 * @author Andras Szabolcs Nagy
33 *
34 */
35public class ValueComparableEObjectStringMap extends TreeMap<EObject, String> {
36
37 private static final class EObjectComparator implements Comparator<EObject> {
38 @Override
39 public int compare(EObject o1, EObject o2) {
40 return Integer.valueOf(System.identityHashCode(o1)).compareTo(Integer.valueOf(System.identityHashCode(o2)));
41 }
42 }
43
44 private final Map<EObject, String> innerMap;
45
46 public ValueComparableEObjectStringMap() {
47 this(new HashMap<EObject, String>());
48 }
49
50 private ValueComparableEObjectStringMap(Map<EObject, String> innerMap) {
51 super(Ordering.natural().onResultOf(Functions.forMap(innerMap)).compound(new EObjectComparator()));
52 this.innerMap = innerMap;
53 }
54
55 @Override
56 public String put(EObject keyEObject, String stringValue) {
57 if (innerMap.containsKey(keyEObject)) {
58 remove(keyEObject);
59 }
60 innerMap.put(keyEObject, stringValue);
61 return super.put(keyEObject, stringValue);
62 }
63}