Methods Summary |
---|
public synchronized void | addReport(Report report)Adds a copy of the report to the registry.
report.setId(currentReportId++);
reports.put(new Integer(report.getId()), new Report(report));
try {
save();
}
catch (IOException e) {
throw new RegistryException("Can't save ReportRegistry", e);
}
|
private void | checkExists(Report report)Tries to locate a report with the ID of the provided report,
and throws an IllegalStateException if there's no such report.
Integer id = new Integer(report.getId());
if (reports == null || reports.get(id) == null) {
throw new IllegalStateException("Report " + report.getId() +
" doesn't exist");
}
|
public synchronized Report | getReport(int id)Returns a copy of the report with the specified ID, or null if
there's no matching report in the registry.
return (Report) reports.get(new Integer(id));
|
public synchronized java.util.List | getReports(java.lang.String owner, java.util.Date fromDate, java.util.Date toDate, int[] status)Returns a List with copies of all reports matching the
search criteria.
List matches = new ArrayList();
Iterator i = reports.values().iterator();
while (i.hasNext()) {
Report report = (Report) i.next();
if (matchesCriteria(report, owner, fromDate, toDate, status)) {
matches.add(new Report(report));
}
}
return matches;
|
private java.io.File | getStore()Returns a File instance for a file named ".expense/store.ser"
in the home directory for the account running the JVM.
File store = null;
File homeDir = new File(System.getProperty("user.home"));
File persistenceDir = new File(homeDir, ".expense");
if (!persistenceDir.exists()) {
persistenceDir.mkdir();
}
return new File(persistenceDir, "store.ser");
|
private void | load()Loads the registry from the file returned by the getStore()
method.
File store = getStore();
try {
ObjectInputStream is =
new ObjectInputStream(new FileInputStream(store));
currentReportId = is.readInt();
reports = (Map) is.readObject();
}
catch (FileNotFoundException fnfe) {
// Ignore.
}
catch (ClassNotFoundException cnfe) {
// Shouldn't happen, but log it if it does
System.err.println("Error loading ReportRegistry: " +
cnfe.getMessage());
}
|
private boolean | matchesCriteria(Report report, java.lang.String owner, java.util.Date from, java.util.Date to, int[] status)Returns true if the report matches the non-null parameter
values.
boolean matches = false;
if ((owner == null || owner.equals(report.getOwner())) &&
(from == null || (report.getStartDate() != null &&
report.getStartDate().getTime() >= from.getTime())) &&
(to == null || (report.getStartDate() != null &&
report.getStartDate().getTime() <= to.getTime()))) {
if (status == null) {
matches = true;
}
else {
for (int i = 0; i < status.length; i++) {
if (report.getStatus() == status[i]) {
matches = true;
break;
}
}
}
}
return matches;
|
public synchronized void | removeReport(Report report)Removes an existing report in the registry with ID of the provided
report.
Throws an IllegalStateException if there's no report with the ID of
the provided report in the registry.
checkExists(report);
reports.remove(new Integer(report.getId()));
try {
save();
}
catch (IOException e) {
throw new RegistryException("Can't save ReportRegistry", e);
}
|
private void | save()Saves the registry to the file returned by the getStore()
method.
File store = getStore();
ObjectOutputStream os =
new ObjectOutputStream(new FileOutputStream(store));
os.writeInt(currentReportId);
os.writeObject(reports);
|
public synchronized void | updateReport(Report report)Replaces an existing report in the registry with a copy of the
provided report.
Throws an IllegalStateException if there's no report with the ID of
the provided report in the registry.
checkExists(report);
reports.put(new Integer(report.getId()), new Report(report));
try {
save();
}
catch (IOException e) {
throw new RegistryException("Can't save ReportRegistry", e);
}
|