/******************************************************************************* * Copyright (c) 2010-2017, Gabor Bergmann, IncQueryLabs Ltd. * 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 tools.refinery.viatra.runtime.matchers.util; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.eclipse.collections.api.map.MutableMap; import org.eclipse.collections.impl.factory.Maps; import org.eclipse.collections.impl.factory.Sets; import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory.ICollectionsFramework; import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory.MemoryType; /** * @author Gabor Bergmann * @since 1.7 * @noreference This class is not intended to be referenced by clients. */ public class EclipseCollectionsFactory implements ICollectionsFramework { @Override public Map createMap() { return Maps.mutable.empty(); } @Override public Map createMap(Map initial) { MutableMap result = Maps.mutable.ofInitialCapacity(initial.size()); result.putAll(initial); return result; } @Override public TreeMap createTreeMap() { // eclipse collections is doing the same return new TreeMap<>(); } @Override public Set createSet() { return Sets.mutable.empty(); } @Override public Set createSet(Collection initial) { return Sets.mutable.ofAll(initial); } @Override public IMultiset createMultiset() { return new EclipseCollectionsMultiset(); } @Override public IDeltaBag createDeltaBag() { return new EclipseCollectionsDeltaBag(); } @Override public List createObserverList() { return new ArrayList(1); // keep concurrent modification exceptions for error detection // Lists.mutable.empty } @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public IMultiLookup createMultiLookup( Class fromKeys, MemoryType toBuckets, Class ofValues) { boolean longKeys = Long.class.equals(fromKeys); boolean objectKeys = Object.class.equals(fromKeys); if (! (longKeys || objectKeys)) throw new IllegalArgumentException(fromKeys.getName()); boolean longValues = Long.class.equals(ofValues); boolean objectValues = Object.class.equals(ofValues); if (! (longValues || objectValues)) throw new IllegalArgumentException(ofValues.getName()); if (longKeys) { // K == java.lang.Long if (longValues) { // V == java.lang.Long switch(toBuckets) { case MULTISETS: return (IMultiLookup) new EclipseCollectionsMultiLookup.FromLongs.ToMultisets.OfLongs(); case SETS: return (IMultiLookup) new EclipseCollectionsMultiLookup.FromLongs.ToSets.OfLongs(); default: throw new IllegalArgumentException(toBuckets.toString()); } } else { // objectValues switch(toBuckets) { case MULTISETS: return new EclipseCollectionsMultiLookup.FromLongs.ToMultisets.OfObjects(); case SETS: return new EclipseCollectionsMultiLookup.FromLongs.ToSets.OfObjects(); default: throw new IllegalArgumentException(toBuckets.toString()); } } } else { // objectKeys if (longValues) { // V == java.lang.Long switch(toBuckets) { case MULTISETS: return new EclipseCollectionsMultiLookup.FromObjects.ToMultisets.OfLongs(); case SETS: return new EclipseCollectionsMultiLookup.FromObjects.ToSets.OfLongs(); default: throw new IllegalArgumentException(toBuckets.toString()); } } else { // objectValues switch(toBuckets) { case MULTISETS: return new EclipseCollectionsMultiLookup.FromObjects.ToMultisets.OfObjects(); case SETS: return new EclipseCollectionsMultiLookup.FromObjects.ToSets.OfObjects(); default: throw new IllegalArgumentException(toBuckets.toString()); } } } } @Override @SuppressWarnings("unchecked") public IMemory createMemory(Class values, MemoryType memoryType) { if (Long.class.equals(values)) { // T == java.lang.Long switch(memoryType) { case MULTISETS: return (IMemory) new EclipseCollectionsLongMultiset(); case SETS: return (IMemory) new EclipseCollectionsLongSetMemory(); default: throw new IllegalArgumentException(memoryType.toString()); } } else { // objectValues switch(memoryType) { case MULTISETS: return new EclipseCollectionsMultiset<>(); case SETS: return new EclipseCollectionsSetMemory<>(); default: throw new IllegalArgumentException(memoryType.toString()); } } } }