aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/MemoryViewBackedMapView.java
blob: 49711a89a65dcdb7e3c0ed564bd17b0332bc4f42 (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
/*******************************************************************************
 * Copyright (c) 2010-2018, Gabor Bergmann, IncQuery Labs 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.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * A partial and read-only Map implementation, mapping elements to multiplicities backed by an {@link IMemoryView}.
 * 
 * <p> Not implemented: write methods.
 * 
 * <p> Inefficiently implemented: {@link #containsValue(Object)}, {@link #values()}, {@link #entrySet()}.
 * 
 * @author Gabor Bergmann
 * @since 2.0
 */
public class MemoryViewBackedMapView<T> implements Map<T, Integer> {
    
    private static final String READ_ONLY = "Read only";
    private final IMemoryView<T> wrapped;
    
    /**
     * @param wrapped a memory view whose contents are to be exposed as an element-to-integer map.
     */
    protected MemoryViewBackedMapView(IMemoryView<T> wrapped) {
        super();
        this.wrapped = wrapped;
    }

    @Override
    public int size() {
        return wrapped.size();
    }

    @Override
    public boolean isEmpty() {
        return wrapped.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return wrapped.containsNonZeroUnsafe(key);
    }

    @Override
    public boolean containsValue(Object value) {
        if (value instanceof Integer) {
            for (Entry<T, Integer> entry : wrapped.entriesWithMultiplicities()) {
                if (entry.getValue().equals(value)) return true;
            }
        }
        return false;
    }

    @Override
    public Integer put(T key, Integer value) {
        throw new UnsupportedOperationException(READ_ONLY);
    }

    @Override
    public Integer get(Object key) {
        int count = wrapped.getCountUnsafe(key);
        if (count == 0) return null; else return count;
    }

    @Override
    public Integer remove(Object key) {
        throw new UnsupportedOperationException(READ_ONLY);
    }

    @Override
    public void putAll(Map<? extends T, ? extends Integer> m) {
        throw new UnsupportedOperationException(READ_ONLY);
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException(READ_ONLY);
    }

    @Override
    public Set<T> keySet() {
        return wrapped.distinctValues();
    }

    @Override
    public Collection<Integer> values() {
        Collection<Integer> result = new ArrayList<>();
        wrapped.forEachEntryWithMultiplicities((value, count) -> result.add(count));
        return result;
    }

    @Override
    public Set<Entry<T, Integer>> entrySet() {
        Set<Entry<T, Integer>> result = new HashSet<>();
        for (Entry<T, Integer> entry : wrapped.entriesWithMultiplicities()) {
            result.add(entry);
        }
        return result;
    }


    @Override
    public String toString() {
        return wrapped.toString();
    }
}