From e11bce7ad3e803e80883499fec0ad6e4540ffe43 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Tue, 30 Jun 2020 18:03:48 +0200 Subject: Add modified VIATRA-DSE version --- .../dse/util/ValueComparableEObjectStringMap.java | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/ValueComparableEObjectStringMap.java') 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 @@ +/******************************************************************************* + * Copyright (c) 2010-2015, Andras Szabolcs Nagy and Daniel Varro + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-v20.html. + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.viatra.dse.util; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +import org.eclipse.emf.ecore.EObject; + +import com.google.common.base.Functions; +import com.google.common.collect.Ordering; + +/** + * + * This custom {@link TreeMap} implementation enables to store {@link EObject}-{@link String} pairs sorted by values + * (strings). It works as expected if the map is modified in any way, hence the map will still be sorted by values on + * the new set of entries. + * + * It is allowed to have two entries with the same EObject key (and also with same values). + * + * The short coming of the class is that EObjects are compared to each other by their + * {@link System#identityHashCode(Object)}, which may lead to unexpected errors. + * + * @author Andras Szabolcs Nagy + * + */ +public class ValueComparableEObjectStringMap extends TreeMap { + + private static final class EObjectComparator implements Comparator { + @Override + public int compare(EObject o1, EObject o2) { + return Integer.valueOf(System.identityHashCode(o1)).compareTo(Integer.valueOf(System.identityHashCode(o2))); + } + } + + private final Map innerMap; + + public ValueComparableEObjectStringMap() { + this(new HashMap()); + } + + private ValueComparableEObjectStringMap(Map innerMap) { + super(Ordering.natural().onResultOf(Functions.forMap(innerMap)).compound(new EObjectComparator())); + this.innerMap = innerMap; + } + + @Override + public String put(EObject keyEObject, String stringValue) { + if (innerMap.containsKey(keyEObject)) { + remove(keyEObject); + } + innerMap.put(keyEObject, stringValue); + return super.put(keyEObject, stringValue); + } +} -- cgit v1.2.3-70-g09d2