summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/grow/provider/MapCollectionProvider.java
blob: 4c5cef6adad236806098cee6aadfc7e13c2f805c (plain)
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
70
71
72
73
74
/*
 * Copyright 2015 Jesse Morgan
 */

package com.p4square.grow.provider;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;

/**
 * In-memory CollectionProvider implementation, useful for tests.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class MapCollectionProvider<C, K, V> implements CollectionProvider<C, K, V> {
    private final Map<C, Map<K, V>> mMap;

    public MapCollectionProvider() {
        mMap = new HashMap<>();
    }

    @Override
    public synchronized V get(C collection, K key) throws IOException {
        Map<K, V> map = mMap.get(collection);
        if (map != null) {
            return map.get(key);
        }

        return null;
    }

    @Override
    public synchronized Map<K, V> query(C collection) throws IOException {
        Map<K, V> map = mMap.get(collection);
        if (map == null) {
            map = new HashMap<K, V>();
        }

        return map;
    }

    @Override
    public synchronized Map<K, V> query(C collection, int limit) throws IOException {
        Map<K, V> map = query(collection);

        if (map.size() > limit) {
            Map<K, V> smallMap = new HashMap<>();

            Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
            for (int i = 0; i < limit; i++) {
                Map.Entry<K, V> entry = iterator.next();
                smallMap.put(entry.getKey(), entry.getValue());
            }

            return smallMap;

        } else {
            return map;
        }
    }

    @Override
    public synchronized void put(C collection, K key, V obj) throws IOException {
        Map<K, V> map = mMap.get(collection);
        if (map == null) {
            map = new HashMap<K, V>();
            mMap.put(collection, map);
        }

        map.put(key, obj);
    }
}