diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/MapBackedMemoryView.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/MapBackedMemoryView.java | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/MapBackedMemoryView.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/MapBackedMemoryView.java new file mode 100644 index 00000000..3be078bd --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/MapBackedMemoryView.java | |||
@@ -0,0 +1,102 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Gabor Bergmann, IncQuery Labs Ltd. | ||
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 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.util; | ||
10 | |||
11 | import java.util.Iterator; | ||
12 | import java.util.Map; | ||
13 | import java.util.Map.Entry; | ||
14 | import java.util.Set; | ||
15 | import java.util.function.BiConsumer; | ||
16 | |||
17 | /** | ||
18 | * Wraps a Map<T, Integer> (mapping elements to non-zero multiplicities) into an {@link IMemoryView}. | ||
19 | * | ||
20 | * @author Gabor Bergmann | ||
21 | * @since 2.0 | ||
22 | */ | ||
23 | public class MapBackedMemoryView<T> implements IMemoryView<T> { | ||
24 | |||
25 | private Map<T, Integer> wrapped; | ||
26 | |||
27 | /** | ||
28 | * @param wrapped an equivalent map from contained objects to multiplicities | ||
29 | */ | ||
30 | protected MapBackedMemoryView(Map<T, Integer> wrapped) { | ||
31 | super(); | ||
32 | this.wrapped = wrapped; | ||
33 | } | ||
34 | |||
35 | @Override | ||
36 | public Iterator<T> iterator() { | ||
37 | return wrapped.keySet().iterator(); | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public int getCount(T value) { | ||
42 | return getCountUnsafe(value); | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public int getCountUnsafe(Object value) { | ||
47 | Integer count = wrapped.get(value); | ||
48 | return count == null ? 0 : count; | ||
49 | } | ||
50 | |||
51 | @Override | ||
52 | public boolean containsNonZero(T value) { | ||
53 | return wrapped.containsKey(value); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public boolean containsNonZeroUnsafe(Object value) { | ||
58 | return wrapped.containsKey(value); | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public int size() { | ||
63 | return wrapped.size(); | ||
64 | } | ||
65 | |||
66 | @Override | ||
67 | public boolean isEmpty() { | ||
68 | return wrapped.isEmpty(); | ||
69 | } | ||
70 | |||
71 | @Override | ||
72 | public Set<T> distinctValues() { | ||
73 | return wrapped.keySet(); | ||
74 | } | ||
75 | |||
76 | |||
77 | @Override | ||
78 | public void forEachEntryWithMultiplicities(BiConsumer<T, Integer> entryConsumer) { | ||
79 | for (Entry<T, Integer> entry : wrapped.entrySet()) { | ||
80 | entryConsumer.accept(entry.getKey(), entry.getValue()); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | @Override | ||
85 | public Iterable<Entry<T, Integer>> entriesWithMultiplicities() { | ||
86 | return wrapped.entrySet(); | ||
87 | } | ||
88 | |||
89 | @Override | ||
90 | public int hashCode() { | ||
91 | return IMemoryView.hashCode(this); | ||
92 | } | ||
93 | @Override | ||
94 | public boolean equals(Object obj) { | ||
95 | return IMemoryView.equals(this, obj); | ||
96 | } | ||
97 | |||
98 | @Override | ||
99 | public String toString() { | ||
100 | return wrapped.toString(); | ||
101 | } | ||
102 | } | ||