I might not be keeping with Single Responsibility with my DAO. I also implement a custom observable through update. It makes sense because when something changes in the data, then it makes sense for the observers to be updated. Is there way for me separate them but still able to update when I update the data structures? Also is there way to clean my code for readability? I'm trying to do defense coding on each method.
DAO
public class RAMTradeDAO
implements
GenericDAO<Trade, UUID, Boolean>,
Update<Observer<Set<UUID>, Map<UUID, Trade>, ControllerType>>
{
private static Map<UUID, Trade> tradeList;
private static Set<UUID> requests;
private static ArrayList<Observer> observers = new ArrayList<Observer>();
private String availability;
public RAMTradeDAO()
{
tradeList = new ConcurrentHashMap<UUID, Trade>();
requests = Collections.synchronizedSet(new TreeSet<UUID>());
}
@Override
public boolean update(Trade trade, Boolean active) throws NotFoundException
{
if (trade == null || trade.getTradeId() == null
|| !tradeList.containsKey(trade.getTradeId())) throw new NotFoundException(
"RAMTradeDAO update NULL");
tradeList.put(trade.getTradeId(), trade);
if (!active)
{
requests.add(trade.getTradeId());
}
return active;
}
@Override
public boolean delete(Trade trade, Boolean active) throws NotFoundException
{
if (trade == null || trade.getTradeId() == null
|| !tradeList.containsKey(trade.getTradeId())) throw new NotFoundException(
"RAMTradeDAO delete NULL");
if (!active)
{
if (requests.contains(trade.getTradeId()))
{
LOG.SYSTEM.info("Trade " + trade.getTradeId()
+ " was deleted from request");
requests.remove(trade.getTradeId());
}
else
{
throw new NotFoundException(
"RAMTradeDAO delete no request found");
}
}
else
{
LOG.SYSTEM.info("Trade " + trade.getTradeId()
+ " was deleted");
tradeList.remove(trade.getTradeId());
requests.remove(trade.getTradeId());
}
return true;
}
@Override
public Trade findByKey(UUID tradeID, Boolean active)
throws NotFoundException
{
if (tradeID == null || !tradeList.containsKey(tradeID)) throw new NotFoundException(
"RAMTradeDAO findByKey NULL");
if (!active)
{
if (!requests.contains(tradeID)) throw new NotFoundException(
"RAMTradeDAO findByKey request");
return tradeList.get(tradeID);
}
return tradeList.get(tradeID);
}
@Override
public Trade findByValue(Trade trade, Boolean active)
throws NotFoundException
{
if (trade == null || trade.getTradeId() == null
|| !tradeList.containsKey(trade.getTradeId())) throw new NotFoundException(
"RAMTradeDAO findByValue NULL");
if (!active)
{
if (!(requests.contains(trade.getTradeId()))) throw new NotFoundException(
"RAMTradeDAO findByValue request");
return tradeList.get(trade.getTradeId());
}
return tradeList.get(trade.getTradeId());
}
@Override
public boolean insert(Trade trade, Boolean active) throws NotFoundException
{
if (trade == null || trade.getTradeId() == null) throw new NotFoundException(
"RAMTradeDAO Insert");
tradeList.put(trade.getTradeId(), trade);
if (trade.getStatus().equals(TradeStatus.REQUEST))
{
requests.add(trade.getTradeId());
}
LOG.USER.info("Trade_ID: " + trade.getTradeId() + " Request_type: "
+ trade.getStatus());
notifyObservers();
return true;
}
@Override
public void notifyObservers() throws NotFoundException
{
if (observers == null || tradeList == null) throw new NotFoundException(
"notifyObservers NULL");
LOG.CONSOLE.debug("RAMTradeDAO notifyObservers");
for (Observer ob : observers)
{
LOG.CONSOLE
.debug("Notifying to all the subscribers that theres been update");
ob.update(requests, requests, tradeList);
}
}
@Override
public void registerObserver(Observer observer) throws NotFoundException
{
if (observers == null || observer == null) throw new NotFoundException(
"registerObserver NULL");
LOG.CONSOLE.debug("RegisterObserver");
observers.add(observer);
notifyObservers();
}
@Override
public void removeObserver(Observer observer) throws NotFoundException
{
if (observers == null || observer == null
|| !observers.contains(observer)) throw new NotFoundException(
"registerObserver NULL");
observers.remove(observer);
}
/**
* @return the tradeList
* @throws InsertionException
* @throws NotFoundException
*/
public static Map<UUID, Trade> getTradeList() throws NotFoundException
{
if (tradeList == null) throw new NotFoundException(
"RAMTradeDAO getTradeList");
return tradeList;
}
/**
* @return the buyRequests
* @throws InsertionException
* @throws NotFoundException
*/
public static Set<UUID> getRequests() throws NotFoundException
{
if (requests == null) throw new NotFoundException(
"RAMTradeDAO getRequests");
return requests;
}
}