This topic applies to Java version only.
01private static void querySyntax1() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(Pilot.class); 06
container.ext().configure().freespace(); 07
listResult(result); 08
} catch (Exception ex) { 09
System.out.println("System Exception: " + ex.getMessage()); 10
} finally { 11
closeDatabase(); 12
} 13
} 14
}
01private static void querySyntax2() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
public boolean match(Pilot pilot) { 07
// each Pilot is included in the result 08
return true; 09
} 10
}); 11
listResult(result); 12
} catch (Exception ex) { 13
System.out.println("System Exception: " + ex.getMessage()); 14
} finally { 15
closeDatabase(); 16
} 17
} 18
}
01private static void querySyntax3() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
public boolean match(Pilot pilot) { 07
// each Pilot is included in the result 08
return true; 09
} 10
}, new Comparator<Pilot>() { 11
public int compare(Pilot pilot1, Pilot pilot2) { 12
return pilot1.getPoints() - pilot2.getPoints(); 13
} 14
}); 15
listResult(result); 16
} catch (Exception ex) { 17
System.out.println("System Exception: " + ex.getMessage()); 18
} finally { 19
closeDatabase(); 20
} 21
} 22
}
1private static class PilotPredicate extends Predicate<Pilot> { 2
public boolean match(Pilot pilot) { 3
// each Pilot is included in the result 4
return true; 5
} 6
}
1private static class PilotComparator implements Comparator<Pilot> { 2
public int compare(Pilot pilot1, Pilot pilot2) { 3
return pilot1.getPoints() - pilot2.getPoints(); 4
} 5
}
01private static void querySyntax4() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new PilotPredicate(), 06
new PilotComparator()); 07
listResult(result); 08
} catch (Exception ex) { 09
System.out.println("System Exception: " + ex.getMessage()); 10
} finally { 11
closeDatabase(); 12
} 13
} 14
}
01private static void querySyntax5() { 02
ObjectContainer container = database(); 03
if (container != null) { 04
try { 05
List result = container.query(new Predicate() { 06
public boolean match(Object obj) { 07
// each Pilot is included in the result 08
if (obj instanceof Pilot) { 09
return true; 10
} 11
return false; 12
} 13
}, new Comparator() { 14
public int compare(Object object1, Object object2) { 15
return ((Pilot) object1).getPoints() 16
- ((Pilot) object2).getPoints(); 17
} 18
}); 19
listResult(result); 20
} catch (Exception ex) { 21
System.out.println("System Exception: " + ex.getMessage()); 22
} finally { 23
closeDatabase(); 24
} 25
} 26
}
01private static void querySyntax6() { 02
// this example will only work with java versions without 03
// generics support 04
ObjectContainer container = database(); 05
if (container != null) { 06
try { 07
List result = container.query(new Predicate() { 08
public boolean match(Object obj) { 09
// each Pilot is included in the result 10
if (obj instanceof Pilot) { 11
return true; 12
} 13
return false; 14
} 15
}, new QueryComparator() { 16
public int compare(Object pilot1, Object pilot2) { 17
return ((Pilot) pilot1).getPoints() 18
- ((Pilot) pilot2).getPoints(); 19
} 20
}); 21
listResult(result); 22
} catch (Exception ex) { 23
System.out.println("System Exception: " + ex.getMessage()); 24
} finally { 25
closeDatabase(); 26
} 27
} 28
}