aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/EclipseCollectionsFactory.java
blob: 5a623c9b5e4fb115e9b0c5e48d259f7ab98bf8ae (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
 * 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 <K, V> Map<K, V> createMap() {
        return Maps.mutable.empty();
    }
    
    @Override
    public <K, V> Map<K, V> createMap(Map<K, V> initial) {
        MutableMap<K, V> result = Maps.mutable.ofInitialCapacity(initial.size());
        result.putAll(initial);
        return result;
    }
    
    @Override
    public <K, V> TreeMap<K, V> createTreeMap() {
        // eclipse collections is doing the same
        return new TreeMap<>();
    }

    @Override
    public <E> Set<E> createSet() {
        return Sets.mutable.empty();
    }

    @Override
    public <E> Set<E> createSet(Collection<E> initial) {
        return Sets.mutable.ofAll(initial);
    }
    
    @Override
    public <T> IMultiset<T> createMultiset() {
        return new EclipseCollectionsMultiset<T>();
    }
    
    @Override
    public <T> IDeltaBag<T> createDeltaBag() {
        return new EclipseCollectionsDeltaBag<T>();
    }
    
    @Override
    public <O> List<O> createObserverList() {
        return new ArrayList<O>(1); // keep concurrent modification exceptions for error detection
        // Lists.mutable.empty

    }
    
    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public <K, V> IMultiLookup<K, V> createMultiLookup(
            Class<? super K> fromKeys, 
            MemoryType toBuckets,
            Class<? super V> 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<K, V>) new EclipseCollectionsMultiLookup.FromLongs.ToMultisets.OfLongs();
                case SETS:
                    return (IMultiLookup<K, V>) 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 <T> IMemory<T> createMemory(Class<? super T> values, MemoryType memoryType) {
        if (Long.class.equals(values)) { // T == java.lang.Long
            switch(memoryType) {
            case MULTISETS:
                return (IMemory<T>) new EclipseCollectionsLongMultiset();
            case SETS:
                return (IMemory<T>) 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());
            }
        }
    }
    


}