Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dao; import javaapplication5.Registro; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import org.joda.time.LocalDate; import persist.ControlSerialize; /** * * @author S018248 */ public class RegistroDao { public static final String FILE_EXTENSION = "ser"; private List<Registro> list; private ControlSerialize controlSerialize; private Comparator<Registro> regComparator; public RegistroDao() { controlSerialize = new ControlSerialize(); list = new ArrayList<Registro>(); regComparator = new Comparator<Registro>() { @Override public int compare(Registro o1, Registro o2) { Date dt1 = o1.getDate(); Date dt2 = o2.getDate(); return dt1.compareTo(dt2); } }; } //private methods private String getFileName(Registro reg) { return getFileName(reg.getDate()); } private String getFileName(Date dt) { int month, year; Calendar calendar = Calendar.getInstance(); calendar.setTime(dt); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); String filename = "registros" + String.valueOf(year) + String.valueOf(month) + "." + FILE_EXTENSION; return filename; } //public methods public boolean add(Registro reg) { boolean result = true; try { list = getListByMonth(reg.getDate()); boolean isUnique = true; if (!list.isEmpty()) { Registro temp = list.get(list.size() - 1); if (temp != null) { if (temp.getDate().getDay() == reg.getDate().getDay()) { isUnique = false; } } } if (isUnique) { list.add(reg); } else { list.set(list.size() - 1, reg); } controlSerialize.write(list, getFileName(reg.getDate())); } catch (Exception e) { result = false; e.printStackTrace(); } return result; } public boolean edit(Registro reg) { boolean result = true; try { list = getListByMonth(reg.getDate()); Registro regTmp = getRegFromList(list, reg); if (regTmp.getId() >= 0) { list.set(regTmp.getId(), reg); controlSerialize.write(list, getFileName(reg.getDate())); } else { add(reg); } } catch (Exception e) { result = false; e.printStackTrace(); } return result; } public Registro getRegFromList(List<Registro> list, Registro reg) { Registro result = null; int i = Collections.binarySearch(list, reg, new Comparator<Registro>() { @Override public int compare(Registro o1, Registro o2) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(o1.getDate()); Calendar cal2 = Calendar.getInstance(); cal2.setTime(o2.getDate()); cal1.set(Calendar.HOUR_OF_DAY, 0); cal1.set(Calendar.MINUTE, 0); cal1.set(Calendar.SECOND, 0); cal1.set(Calendar.MILLISECOND, 0); cal2.set(Calendar.HOUR_OF_DAY, 0); cal2.set(Calendar.MINUTE, 0); cal2.set(Calendar.SECOND, 0); cal2.set(Calendar.MILLISECOND, 0); return cal1.compareTo(cal2); } }); if (i >= 0) { result = list.get(i); result.setId(i); } else { result = new Registro(); } return result; } public List<Registro> getListByMonth(Date dt) { List<Registro> result = null; try { String filename = getFileName(dt); result = (List<Registro>) controlSerialize.read(filename); if (result == null) { result = new ArrayList<Registro>(); } //ordena a lista Collections.sort(result, regComparator); } catch (Exception e) { e.printStackTrace(); } return result; } public List<Registro> getListBetween(Date dt1, Date dt2) { List<Registro> result = new ArrayList<Registro>(); try { LocalDate dtTmp = LocalDate.fromDateFields(dt1); while (dt2.compareTo(dtTmp.dayOfMonth().withMinimumValue().toDate()) >= 0) { List<Registro> listTmp = getListByMonth(dtTmp.toDate()); if (listTmp != null) { result.addAll(listTmp); //ordena a lista Collections.sort(result, regComparator); } dtTmp = dtTmp.plusMonths(1); } } catch (Exception e) { e.printStackTrace(); } return result; } public List<Registro> getList() { return getListByMonth(new Date()); } }