aboutsummaryrefslogtreecommitdiffstats
path: root/cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository/CoSRespositoryInMem.java
blob: 2fa44c1f8b7aa16c2f5d3f5e7a8b8c05270616b2 (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
package com.cablelabs.vcpe.cos.cosbase.repository;

import com.cablelabs.vcpe.common.Dbg;
import com.cablelabs.vcpe.cos.cosbase.model.CoS;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by steve on 5/24/15.
 */


/*
    Singleton that contains a hashmap which holds instances of CoS objects, indexed by CiS ID
 */

public enum CoSRespositoryInMem implements CoSRespository {
    INSTANCE; // Singleton

    private Map< String, CoS> cosDB = new ConcurrentHashMap<String, CoS>();

    @Override
    public CoS add(CoS cos) {
        if ( this.get(cos.getId()) != null ) {
            return null;
        }
        cosDB.put(cos.getId(), cos );
        return cos;
    }

    @Override
    public CoS get(String cosId) {
        return cosDB.get(cosId);
    }

    @Override
    public CoS update(CoS cos) {
        // put returns null if cos did not exist, other returns cos as it stood prior to put
        return cosDB.put(cos.getId(), cos);
    }

    @Override
    public CoS delete(String cosId) {
        // remove returns null if cos did not exist, other returns cos as it stood prior to remove
        return cosDB.remove(cosId);
    }

    @Override
    public List<CoS> getAll() {
        List<CoS> cosList = new ArrayList<CoS>(cosDB.values());
        return cosList;
    }

    @Override
    public int count() {
        return cosDB.size();
    }

    @Override
    public void dump(int tab) {

        Dbg.p(tab, "CoS Repo: " + cosDB.size() + " entrie(s)");
        int numCos = 0;
        for (CoS curCos : cosDB.values()) {
            numCos++;
            Dbg.p(tab+1, "<Entry " + numCos+">");
            curCos.dump(tab+2);
        }
    }

}