Skip to content

Commit 6184b86

Browse files
committed
Merge branch 'develop'
2 parents 175a1a7 + e9bef67 commit 6184b86

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>jre-utils</artifactId>
13-
<version>0.3.10</version>
13+
<version>0.3.11</version>
1414
<name>JreUtils</name>
1515
<packaging>jar</packaging>
1616

src/main/java/info/unterrainer/commons/jreutils/DoubleBufferedFile.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.file.Files;
77
import java.nio.file.LinkOption;
88
import java.nio.file.Path;
9+
import java.nio.file.StandardOpenOption;
910
import java.nio.file.attribute.BasicFileAttributes;
1011
import java.time.LocalDateTime;
1112
import java.util.Objects;
@@ -116,7 +117,9 @@ DoubleBufferedFileData withCheckedRead() throws IOException {
116117
}
117118

118119
BufferedWriter getBufferedWriter() throws IOException {
119-
return Files.newBufferedWriter(path, Charset.forName("UTF-8"));
120+
Files.createDirectories(path.getParent());
121+
return Files.newBufferedWriter(path, Charset.forName("UTF-8"), StandardOpenOption.CREATE,
122+
StandardOpenOption.WRITE, StandardOpenOption.APPEND);
120123
}
121124
}
122125

src/main/java/info/unterrainer/commons/jreutils/collections/DataTable.java

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.util.Set;
1010
import java.util.function.Function;
1111

12+
import org.apache.commons.collections4.MultiValuedMap;
13+
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
14+
1215
import lombok.Getter;
1316
import lombok.experimental.Accessors;
1417

@@ -33,6 +36,10 @@ public class DataTable<T> {
3336
private HashMap<String, Function<T, Boolean>> filters = new HashMap<>();
3437
private HashMap<String, DataMap<Object, T>> maps = new HashMap<>();
3538

39+
private HashMap<String, Function<T, Object>> multiKeySuppliers = new HashMap<>();
40+
private HashMap<String, Function<T, Boolean>> multiFilters = new HashMap<>();
41+
private HashMap<String, MultiValuedMap<Object, T>> multiMaps = new HashMap<>();
42+
3643
public DataTable(final Class<T> clazz, final int maxEntries) {
3744
this.clazz = clazz;
3845
this.maxEntries = maxEntries;
@@ -43,13 +50,27 @@ public <K> DataTable<T> addIndex(final String name, final Function<T, K> keySupp
4350
return addIndex(name, keySupplier, null);
4451
}
4552

53+
public <K> DataTable<T> addMultiIndex(final String name, final Function<T, K> keySupplier) {
54+
return addMultiIndex(name, keySupplier, null);
55+
}
56+
4657
@SuppressWarnings("unchecked")
4758
public <K> DataTable<T> addIndex(final String name, final Function<T, K> keySupplier,
4859
final Function<T, Boolean> filter) {
4960
keySuppliers.put(name, (Function<T, Object>) keySupplier);
5061
if (filter != null)
5162
filters.put(name, filter);
52-
maps.put(name, new DataMap<Object, T>(maxEntries));
63+
maps.put(name, new DataMap<>(maxEntries));
64+
return this;
65+
}
66+
67+
@SuppressWarnings("unchecked")
68+
public <K> DataTable<T> addMultiIndex(final String name, final Function<T, K> keySupplier,
69+
final Function<T, Boolean> filter) {
70+
multiKeySuppliers.put(name, (Function<T, Object>) keySupplier);
71+
if (filter != null)
72+
multiFilters.put(name, filter);
73+
multiMaps.put(name, new ArrayListValuedHashMap<>(maxEntries));
5374
return this;
5475
}
5576

@@ -77,6 +98,11 @@ public synchronized T poll() {
7798
DataMap<Object, T> map = maps.get(name);
7899
map.remove(func.apply(e));
79100
}
101+
for (String name : multiKeySuppliers.keySet()) {
102+
Function<T, ?> func = multiKeySuppliers.get(name);
103+
MultiValuedMap<Object, T> map = multiMaps.get(name);
104+
map.removeMapping(func.apply(e), e);
105+
}
80106
return e;
81107
}
82108

@@ -92,6 +118,18 @@ public synchronized <K> T get(final String name, final K key) {
92118
return maps.get(name).get(key);
93119
}
94120

121+
/**
122+
* Gets elements by a specified multi-index.
123+
*
124+
* @param <K> the type of the key used by the given multi-index
125+
* @param name the name of the multi-index
126+
* @param key the key of the element to retrieve using the given multi-index
127+
* @return the collection of elements to retrieve, that may be empty
128+
*/
129+
public synchronized <K> Collection<T> multiGet(final String name, final K key) {
130+
return multiMaps.get(name).get(key);
131+
}
132+
95133
/**
96134
* Adds one or more elements to the DataTable.
97135
* <p>
@@ -113,6 +151,14 @@ public synchronized void add(final T... elements) {
113151
map.put(key, element);
114152
}
115153
}
154+
for (Entry<String, Function<T, Object>> entry : multiKeySuppliers.entrySet()) {
155+
Function<T, Boolean> filter = multiFilters.get(entry.getKey());
156+
MultiValuedMap<Object, T> map = multiMaps.get(entry.getKey());
157+
if (filter == null || filter.apply(element)) {
158+
Object key = entry.getValue().apply(element);
159+
map.put(key, element);
160+
}
161+
}
116162
}
117163
}
118164

@@ -128,6 +174,18 @@ public synchronized <K> Set<K> keySet(final String name) {
128174
return (Set<K>) maps.get(name).keySet();
129175
}
130176

177+
/**
178+
* Get a set of keys for a given multi-index.
179+
*
180+
* @param <K> the type of the key for the given multi-index
181+
* @param name the name of the multi-index to get the keys from
182+
* @return a set of keys
183+
*/
184+
@SuppressWarnings("unchecked")
185+
public synchronized <K> Set<K> multiKeySet(final String name) {
186+
return (Set<K>) multiMaps.get(name).keySet();
187+
}
188+
131189
/**
132190
* Get a list of keys for a given index.
133191
*
@@ -142,6 +200,20 @@ public synchronized <K> List<K> keyList(final String name) {
142200
return list;
143201
}
144202

203+
/**
204+
* Get a list of keys for a given multi-index.
205+
*
206+
* @param <K> the type of the key for the given multi-index
207+
* @param name the name of the multi-index to get the keys from
208+
* @return a list of keys
209+
*/
210+
@SuppressWarnings("unchecked")
211+
public synchronized <K> List<K> multiKeyList(final String name) {
212+
List<K> list = new ArrayList<>();
213+
list.addAll((Set<K>) multiMaps.get(name).keySet());
214+
return list;
215+
}
216+
145217
public synchronized Collection<T> values(final String name) {
146218
return maps.get(name).values();
147219
}
@@ -174,6 +246,14 @@ public synchronized DataTable<T> load(T[] backingArray) {
174246
if (filter == null || filter.apply(s))
175247
map.put(entry.getValue().apply(s), s);
176248
}
249+
for (Entry<String, Function<T, Object>> entry : multiKeySuppliers.entrySet()) {
250+
MultiValuedMap<Object, T> map = new ArrayListValuedHashMap<>(maxEntries);
251+
multiMaps.put(entry.getKey(), map);
252+
Function<T, Boolean> filter = multiFilters.get(entry.getKey());
253+
for (T s : backingArray)
254+
if (filter == null || filter.apply(s))
255+
map.put(entry.getValue().apply(s), s);
256+
}
177257
return this;
178258
}
179259

0 commit comments

Comments
 (0)