diff options
Diffstat (limited to 'cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository')
2 files changed, 96 insertions, 0 deletions
diff --git a/cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository/CoSRespository.java b/cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository/CoSRespository.java new file mode 100644 index 0000000..3a58000 --- /dev/null +++ b/cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository/CoSRespository.java @@ -0,0 +1,22 @@ +package com.cablelabs.vcpe.cos.cosbase.repository; + +import com.cablelabs.vcpe.cos.cosbase.model.CoS; + +import java.util.List; + +/** + * Created by steve on 5/25/15. + */ +public interface CoSRespository +{ + // TODO add exceptions + + CoS add(CoS cos); // returns null if already exists, otherwise returns stored cos + CoS get(String cosId); // returns null if not found, otherwise stored cos + CoS update(CoS cos); // returns null if did not exit, otherwise cos as it was previous to update (put in any case) + CoS delete(String cosId); // returns null if not found, otherwise cos as it was previous to delete + int count(); // number of CoS stored in the repo + List<CoS> getAll(); + + public void dump(int tab); // print out contents of the repo +} diff --git a/cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository/CoSRespositoryInMem.java b/cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository/CoSRespositoryInMem.java new file mode 100644 index 0000000..2fa44c1 --- /dev/null +++ b/cos/cosbase/src/main/java/com/cablelabs/vcpe/cos/cosbase/repository/CoSRespositoryInMem.java @@ -0,0 +1,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); + } + } + +} |