@Override public void remove(Entry entry) throws Exception { if (!entries.containsKey(entry.getId())) { throw new DBException("Entry not found"); } long logid = entry.getLog().getId(); Log log = logDB.get(logid); log.removeEntry(entry); logDB.update(log); entries.remove(entry.getId()); }
@Override public void update(Entry entry) throws Exception { if (entry == null) { throw new DBException("Invalid entry"); } entry = logDB.updateEntry(entry); entries.put(entry.getId(), entry); }
@Override public Entry add(Entry entry) throws Exception { if (entries.containsValue(entry)) { throw new DBException("Entry is already in the collection"); } entry.setId(idCounter); entries.put(idCounter, entry); idCounter++; Log log = entry.getLog(); log.addEntry(entry); logDB.update(log); return entry; }
/** @author yassinbarrani */ public class EntryDBCollection implements EntryDB { private static EntryDBCollection instance = null; protected EntryDBCollection() { // Exists only to defeat instantiation. } public static EntryDBCollection getInstance() { if (instance == null) { instance = new EntryDBCollection(); } return instance; } private static Map<Long, Entry> entries = new HashMap<Long, Entry>(); private static long idCounter = 1; private LogDBCollection logDB = LogDBCollection.getInstance(); @Override public Entry add(Entry entry) throws Exception { if (entries.containsValue(entry)) { throw new DBException("Entry is already in the collection"); } entry.setId(idCounter); entries.put(idCounter, entry); idCounter++; Log log = entry.getLog(); log.addEntry(entry); logDB.update(log); return entry; } @Override public void update(Entry entry) throws Exception { if (entry == null) { throw new DBException("Invalid entry"); } entry = logDB.updateEntry(entry); entries.put(entry.getId(), entry); } @Override public Entry get(long id) throws Exception { if (!entries.containsKey(id)) { throw new DBException("Entry not found"); } return entries.get(id); } @Override public void remove(Entry entry) throws Exception { if (!entries.containsKey(entry.getId())) { throw new DBException("Entry not found"); } long logid = entry.getLog().getId(); Log log = logDB.get(logid); log.removeEntry(entry); logDB.update(log); entries.remove(entry.getId()); } @Override public Collection<Entry> getEntries() { return entries.values(); } }