diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/EclipseCollectionsMultiset.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/EclipseCollectionsMultiset.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/EclipseCollectionsMultiset.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/EclipseCollectionsMultiset.java new file mode 100644 index 00000000..46977c8b --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/EclipseCollectionsMultiset.java | |||
@@ -0,0 +1,93 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2017, Gabor Bergmann, IncQueryLabs 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 | /** | ||
12 | * @author Gabor Bergmann | ||
13 | * @since 1.7 | ||
14 | */ | ||
15 | public class EclipseCollectionsMultiset<T> extends EclipseCollectionsBagMemory<T> implements IMultiset<T> { | ||
16 | |||
17 | @Override | ||
18 | public boolean addOne(T value) { | ||
19 | int oldCount = super.getIfAbsent(value, 0); | ||
20 | |||
21 | super.put(value, oldCount + 1); | ||
22 | |||
23 | return oldCount == 0; | ||
24 | } | ||
25 | |||
26 | @Override | ||
27 | public boolean addPositive(T value, int count) { | ||
28 | if (count < 0) { | ||
29 | throw new IllegalArgumentException("The count value must be positive!"); | ||
30 | } | ||
31 | |||
32 | int oldCount = super.getIfAbsent(value, 0); | ||
33 | |||
34 | super.put(value, oldCount + count); | ||
35 | |||
36 | return oldCount == 0; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public boolean addSigned(T value, int count) { | ||
41 | int oldCount = super.getIfAbsent(value, 0); | ||
42 | int newCount = oldCount + count; | ||
43 | |||
44 | boolean becomesZero = newCount == 0; | ||
45 | if (newCount < 0) | ||
46 | throw new IllegalStateException(String.format( | ||
47 | "Cannot remove %d occurrences of value '%s' as only %d would remain in %s", | ||
48 | count, value, newCount, this)); | ||
49 | else if (becomesZero) | ||
50 | super.removeKey(value); | ||
51 | else // (newCount > 0) | ||
52 | super.put(value, newCount); | ||
53 | |||
54 | return becomesZero || oldCount == 0; | ||
55 | } | ||
56 | |||
57 | |||
58 | @Override | ||
59 | public boolean removeOne(T value) { | ||
60 | return removeOneInternal(value, true); | ||
61 | } | ||
62 | |||
63 | @Override | ||
64 | public boolean removeOneOrNop(T value) { | ||
65 | return removeOneInternal(value, false); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * @since 2.3 | ||
70 | */ | ||
71 | protected boolean removeOneInternal(T value, boolean throwIfImpossible) { | ||
72 | int oldCount = super.getIfAbsent(value, 0); | ||
73 | if (oldCount == 0) { | ||
74 | if (throwIfImpossible) throw new IllegalStateException(String.format( | ||
75 | "Cannot remove value '%s' that is not contained in %s", | ||
76 | value, this)); | ||
77 | else return false; | ||
78 | } | ||
79 | |||
80 | int rest = oldCount - 1; | ||
81 | boolean empty = rest == 0; | ||
82 | |||
83 | if (!empty) { | ||
84 | super.put(value, rest); | ||
85 | } else { | ||
86 | super.remove(value); | ||
87 | } | ||
88 | |||
89 | return empty; | ||
90 | } | ||
91 | |||
92 | |||
93 | } | ||