• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 228

29 Nov 2023 05:00PM UTC coverage: 62.48% (-0.09%) from 62.574%
228

push

circleci

web-flow
Show dialog for choosing game path, cleanup (#1083)

* show dialog for choosing game path

* cleanup code

* remove logs and refactor

* remove confusing mpq error, make some mpq loads readonly

17295 of 27681 relevant lines covered (62.48%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.08
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/utils/MapWithIndexes.java
1
package de.peeeq.wurstscript.utils;
2

3
import com.google.common.base.Preconditions;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.LinkedHashMultimap;
6
import com.google.common.collect.Multimap;
7

8
import java.util.*;
9
import java.util.function.BiConsumer;
10
import java.util.function.Function;
11
import java.util.function.Predicate;
12

13
/**
14
 * A map from keys of type K to values of type V
15
 * with the additional feature of creating indexes for values.
16
 * Indexes are automatically kept up to date.
17
 *
18
 * Assumption: index functions do not change.
19
 */
20
public class MapWithIndexes<K, V> {
1✔
21
    private final Map<K, V> base = new LinkedHashMap<>();
1✔
22
    private final List<BiConsumer<K, V>> onInserts = new ArrayList<>();
1✔
23
    private final List<BiConsumer<K, V>> onDelete = new ArrayList<>();
1✔
24

25

26
    public <I> Index<I, K> createIndex(Function<V, I> indexedValue) {
27
        Multimap<I, K> index = LinkedHashMultimap.create();
×
28
        onInserts.add((k, v) -> index.put(indexedValue.apply(v), k));
×
29
        onDelete.add((k, v) -> index.remove(indexedValue.apply(v), k));
×
30
        return key -> ImmutableList.copyOf(index.get(key));
×
31
    }
32

33
    public PredIndex<K> createPredicateIndex(Predicate<V> indexedValue) {
34
        Set<K> index = new LinkedHashSet<>();
1✔
35
        onInserts.add((k, v) -> {
1✔
36
            if (indexedValue.test(v)) {
1✔
37
                index.add(k);
1✔
38
            }
39
        });
1✔
40
        onDelete.add((k, v) -> {
1✔
41
            index.remove(k);
1✔
42
        });
1✔
43
        return () -> ImmutableList.copyOf(index);
1✔
44
    }
45

46
    public <I> Index<I, K> createMultiIndex(Function<V, ? extends Collection<I>> indexedValue) {
47
        Multimap<I, K> index = LinkedHashMultimap.create();
1✔
48
        onInserts.add((k, v) -> {
1✔
49
            Collection<I> is = indexedValue.apply(v);
1✔
50
            for (I i : is) {
1✔
51
                index.put(i, k);
1✔
52
            }
1✔
53
        });
1✔
54
        onDelete.add((k, v) -> {
1✔
55
            Collection<I> is = indexedValue.apply(v);
1✔
56
            for (I i : is) {
1✔
57
                index.remove(i, k);
1✔
58
            }
1✔
59
        });
1✔
60
        return key -> ImmutableList.copyOf(index.get(key));
1✔
61
    }
62

63

64
    public boolean isEmpty() {
65
        return base.isEmpty();
1✔
66
    }
67

68
    public boolean containsKey(K key) {
69
        return base.containsKey(key);
×
70
    }
71

72
    public V get(K key) {
73
        return base.get(key);
×
74
    }
75

76
    public V put(K key, V value) {
77
        Preconditions.checkNotNull(key, "key must not be null");
1✔
78
        Preconditions.checkNotNull(value, "value must not be null");
1✔
79
        V oldV = base.put(key, value);
1✔
80
        if (oldV != null) {
1✔
81
            for (BiConsumer<K, V> f : onDelete) {
×
82
                f.accept(key, value);
×
83
            }
×
84
        }
85
        for (BiConsumer<K, V> f : onInserts) {
1✔
86
            f.accept(key, value);
1✔
87
        }
1✔
88
        return oldV;
1✔
89
    }
90

91
    public V remove(K key) {
92
        V value = base.remove(key);
1✔
93
        if (value != null) {
1✔
94
            for (BiConsumer<K, V> f : onDelete) {
1✔
95
                f.accept(key, value);
1✔
96
            }
1✔
97
        }
98
        return value;
1✔
99
    }
100

101
    public void clear() {
102
        for (Map.Entry<K, V> e : base.entrySet()) {
1✔
103
            for (BiConsumer<K, V> f : onDelete) {
1✔
104
                f.accept(e.getKey(), e.getValue());
1✔
105
            }
1✔
106
        }
1✔
107
        base.clear();
1✔
108
    }
1✔
109

110
    public Set<K> keySet() {
111
        return base.keySet();
×
112
    }
113

114
    public Collection<V> values() {
115
        return base.values();
×
116
    }
117

118
    public Set<Map.Entry<K, V>> entrySet() {
119
        return base.entrySet();
1✔
120
    }
121

122
    public void removeAll(Collection<K> ks) {
123
        for (K k : ks) {
1✔
124
            remove(k);
1✔
125
        }
1✔
126
    }
1✔
127

128
    public interface Index<I, K> {
129
        Collection<K> lookup(I i);
130
    }
131

132
    public interface PredIndex<K> {
133
        Collection<K> lookup();
134
    }
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc