当前位置: 首页 > 工具软件 > Floggy > 使用案例 >

floggy

阎元徽
2023-12-01
As J2ME developers we couldn't handle anymore the idea of writing so many code lines to persist so few attributes. For each class we want to persist we had to generate the source code to transform an object into a byte array and vice-versa. We have tried to find free persistence frameworks for this technology on the internet, but we couldn't find one that fitted our needs. So,floggy is your selection.
How to use it ? it is very easy.see below,

public static void saveObject() {
Person person = new Person();
person.setName("LaoLi");
person.setBirthday("1988/8/8");
person.setGender("man");
person.setPhones("13169563289");
try {
int id = PersistableManager.getInstance().save(person);
// Editing and saving a previously saved object
PersistableManager.getInstance().load(person, id);
person.setName("LiLao");
id = PersistableManager.getInstance().save(person);
// Deleting a previously saved object
PersistableManager.getInstance().load(person, id);
PersistableManager.getInstance().delete(person);
}
catch (FloggyException e) {
System.out.println("throw a exception");
e.printStackTrace();
}
// Listing all objects
ObjectSet persons;
try {
persons = PersistableManager.getInstance().find(Person.class, null, null);
for ( int i = 0 ; i < persons.size() ; i++) {
Person person2 = (Person) persons.get(i);
System.out.println("name: " + person2.getName());
}
}
catch (FloggyException e) {
e.printStackTrace();
}
// Using a filter to find specific objects
try {
persons = PersistableManager.getInstance().find(Person.class, new MacFilter(), null);
for ( int i = 0; i< persons.size(); i++) {
Person pers = (Person) persons.get(i);
}
}
catch (FloggyException e) {
e.printStackTrace();
}

// Sorting persons by their age (younger first)
try {
persons = PersistableManager.getInstance().find(Person.class, null, new AgeCompator());
}
catch (FloggyException e) {
e.printStackTrace();
}
}

}

class MacFilter implements Filter{

public boolean matches(Persistable persistable) {
Person person = (Person) persistable;
return person.getGender() == "M";
}

}

class AgeCompator implements Comparator{

public int compare(Persistable p1, Persistable p2) {
Person per = (Person) p1;
Person pers = (Person) p2;
if ( per.getBirthday().length() < pers.getBirthday().length()) { return PRECEDES; }
if ( per.getBirthday().length() > pers.getBirthday().length()) { return FOLLOWS; }

return EQUIVALENT;
}
 类似资料:

相关阅读

相关文章

相关问答