diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/GroupedSet.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/GroupedSet.java | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/GroupedSet.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/GroupedSet.java new file mode 100644 index 00000000..65561e53 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/GroupedSet.java | |||
@@ -0,0 +1,114 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2019, Tamas Szabo, itemis AG, 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.rete.aggregation; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Iterator; | ||
13 | import java.util.Set; | ||
14 | import java.util.function.BiFunction; | ||
15 | |||
16 | /** | ||
17 | * An optimized {@link Set} implementation where each contained value is produced by combining together a grouping value | ||
18 | * and some other (key) object. The way of combining together these two values is specified by the closure passed to the | ||
19 | * constructor. Only a select few {@link Set} operations are supported. This collection is unmodifiable. | ||
20 | * | ||
21 | * @author Tamas Szabo | ||
22 | * @since 2.4 | ||
23 | */ | ||
24 | public class GroupedSet<GroupingValueType, GroupedKeyType, WholeKeyType> implements Set<WholeKeyType> { | ||
25 | |||
26 | protected final GroupingValueType group; | ||
27 | protected final Collection<GroupedKeyType> values; | ||
28 | protected final BiFunction<GroupingValueType, GroupedKeyType, WholeKeyType> valueFunc; | ||
29 | |||
30 | public GroupedSet(final GroupingValueType group, final Collection<GroupedKeyType> values, | ||
31 | final BiFunction<GroupingValueType, GroupedKeyType, WholeKeyType> valueFunc) { | ||
32 | this.group = group; | ||
33 | this.values = values; | ||
34 | this.valueFunc = valueFunc; | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public int size() { | ||
39 | return this.values.size(); | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public boolean isEmpty() { | ||
44 | return this.values.isEmpty(); | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public boolean contains(final Object obj) { | ||
49 | throw new UnsupportedOperationException(); | ||
50 | } | ||
51 | |||
52 | @Override | ||
53 | public Iterator<WholeKeyType> iterator() { | ||
54 | final Iterator<GroupedKeyType> wrapped = this.values.iterator(); | ||
55 | return new Iterator<WholeKeyType>() { | ||
56 | @Override | ||
57 | public boolean hasNext() { | ||
58 | return wrapped.hasNext(); | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public WholeKeyType next() { | ||
63 | final GroupedKeyType value = wrapped.next(); | ||
64 | return valueFunc.apply(group, value); | ||
65 | } | ||
66 | }; | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public Object[] toArray() { | ||
71 | throw new UnsupportedOperationException(); | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public <T> T[] toArray(final T[] arr) { | ||
76 | throw new UnsupportedOperationException(); | ||
77 | } | ||
78 | |||
79 | @Override | ||
80 | public boolean add(final WholeKeyType tuple) { | ||
81 | throw new UnsupportedOperationException(); | ||
82 | } | ||
83 | |||
84 | @Override | ||
85 | public boolean remove(final Object obj) { | ||
86 | throw new UnsupportedOperationException(); | ||
87 | } | ||
88 | |||
89 | @Override | ||
90 | public boolean containsAll(final Collection<?> c) { | ||
91 | throw new UnsupportedOperationException(); | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public boolean addAll(final Collection<? extends WholeKeyType> coll) { | ||
96 | throw new UnsupportedOperationException(); | ||
97 | } | ||
98 | |||
99 | @Override | ||
100 | public boolean retainAll(final Collection<?> coll) { | ||
101 | throw new UnsupportedOperationException(); | ||
102 | } | ||
103 | |||
104 | @Override | ||
105 | public boolean removeAll(final Collection<?> coll) { | ||
106 | throw new UnsupportedOperationException(); | ||
107 | } | ||
108 | |||
109 | @Override | ||
110 | public void clear() { | ||
111 | throw new UnsupportedOperationException(); | ||
112 | } | ||
113 | |||
114 | } \ No newline at end of file | ||