1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/*
* Copyright 2014 Jesse Morgan
*/
package com.p4square.grow.provider;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public abstract class DelegateCollectionProvider<C, DC, K, DK, V>
implements CollectionProvider<C, K, V> {
private CollectionProvider<DC, DK, V> mProvider;
public DelegateCollectionProvider(final CollectionProvider<DC, DK, V> provider) {
mProvider = provider;
}
public V get(C collection, K key) throws IOException {
return mProvider.get(makeCollectionKey(collection), makeKey(key));
}
public Map<K, V> query(C collection) throws IOException {
return query(collection, -1);
}
public Map<K, V> query(C collection, int limit) throws IOException {
Map<DK, V> delegateResult = mProvider.query(makeCollectionKey(collection), limit);
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<DK, V> entry : delegateResult.entrySet()) {
result.put(unmakeKey(entry.getKey()), entry.getValue());
}
return result;
}
public void put(C collection, K key, V obj) throws IOException {
mProvider.put(makeCollectionKey(collection), makeKey(key), obj);
}
/**
* Make a collection key for the delegated provider.
*
* @param input The pre-transform key.
* @return the post-transform key.
*/
protected abstract DC makeCollectionKey(final C input);
/**
* Make a key for the delegated provider.
*
* @param input The pre-transform key.
* @return the post-transform key.
*/
protected abstract DK makeKey(final K input);
/**
* Transform a key for the delegated provider to an input key.
*
* @param input The post-transform key.
* @return the pre-transform key.
*/
protected abstract K unmakeKey(final DK input);
}
|