Object-Relational Mapping experiment
Box<Transaction> transactionsBox = new Box<Transaction>(new ORM<Transaction>()
{
public Map write(Transaction data)
{
Map map = new HashMap();
map.put("id", data.getId());
map.put("count", data.getCount());
map.put("date", data.getDate());
return map;
}
public Transaction read(Map map)
{
return new Transaction(((Long)map.get("id")).intValue(),
((Long)map.get("count")).intValue(),
(Long)map.get("date"));
}
}, "/home/user/stan/boxes/transactionsbox");
transactionsBox.add(new Transaction(1, 100, System.currentTimeMillis()),
new Transaction(2, 58, System.currentTimeMillis()),
new Transaction(3, -45, System.currentTimeMillis()),
new Transaction(4, 23, System.currentTimeMillis()),
new Transaction(5, -78, System.currentTimeMillis()));
List<Transaction> transactions = transactionsBox.getAll();
List<Transaction> transactions = transactionsBox.get(new Query<Transaction>()
{
public boolean query(Transaction transaction)
{
return transaction.getCount() < 50;
}
});
transactionsBox.replace(new Query<Transaction>()
{
public boolean query(Transaction transaction)
{
return transaction.getId() == 3;
}
}, new Transaction(3, -25, System.currentTimeMillis()));
transactionsBox.removeFirst(new Query<Transaction>()
{
public boolean query(Transaction transaction)
{
return transaction.getId() == 1748145049;
}
});
Case<Settings> settingsCase = new Case<Settings>(new Settings(0, null, 0), new ORM<Settings>()
{
public Map write(Settings data)
{
Map map = new HashMap();
map.put("time", data.getTime());
map.put("name", data.getName());
map.put("color", data.getColor());
return map;
}
public Settings read(Map map)
{
return new Settings((Long)map.get("time"),
(String)map.get("name"),
Long.valueOf((Long)map.get("color")).intValue());
}
}, "/home/user/stan/boxes/settingscase");
Settings settings = settingsCase.get();
settingsCase.save(new Settings(System.currentTimeMillis(), "sometext", 1234));