001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.query.*;
027:
028: public class JdkComparatorSort {
029: private static class AscendingIdComparator implements Comparator {
030: public int compare(Object first, Object second) {
031: return ((JdkComparatorSort) first)._id
032: - ((JdkComparatorSort) second)._id;
033: }
034: }
035:
036: private static class DescendingIdComparator implements Comparator {
037: public int compare(Object first, Object second) {
038: return ((JdkComparatorSort) second)._id
039: - ((JdkComparatorSort) first)._id;
040: }
041: }
042:
043: private static class OddEvenIdComparator implements Comparator {
044: public int compare(Object first, Object second) {
045: int idA = ((JdkComparatorSort) first)._id;
046: int idB = ((JdkComparatorSort) second)._id;
047: int modA = idA % 2;
048: int modB = idB % 2;
049: if (modA != modB) {
050: return modA - modB;
051: }
052: return idA - idB;
053: }
054: }
055:
056: private static class AscendingNameComparator implements Comparator {
057: public int compare(Object first, Object second) {
058: return ((JdkComparatorSort) first)._name
059: .compareTo(((JdkComparatorSort) second)._name);
060: }
061: }
062:
063: private static class SmallerThanThreePredicate extends
064: Predicate<JdkComparatorSort> {
065: public boolean match(JdkComparatorSort candidate) {
066: return candidate._id < 3;
067: }
068: }
069:
070: public int _id;
071: public String _name;
072:
073: public JdkComparatorSort() {
074: this (0, null);
075: }
076:
077: public JdkComparatorSort(int id, String name) {
078: this ._id = id;
079: this ._name = name;
080: }
081:
082: public void store() {
083: for (int i = 0; i < 4; i++) {
084: Test.store(new JdkComparatorSort(i, String.valueOf(3 - i)));
085: }
086: }
087:
088: public void testByIdAscending() {
089: assertIdOrder(new AscendingIdComparator(), new int[] { 0, 1, 2,
090: 3 });
091: }
092:
093: public void testByIdAscendingConstrained() {
094: Query query = Test.query();
095: query.constrain(getClass());
096: query.descend("_id").constrain(new Integer(3)).smaller();
097: assertIdOrder(query, new AscendingIdComparator(), new int[] {
098: 0, 1, 2 });
099: }
100:
101: public void testByIdAscendingNQ() {
102: ObjectSet result = Test.objectContainer().query(
103: new SmallerThanThreePredicate(),
104: new AscendingIdComparator());
105: assertIdOrder(result, new int[] { 0, 1, 2 });
106: }
107:
108: public void testByIdDescending() {
109: assertIdOrder(new DescendingIdComparator(), new int[] { 3, 2,
110: 1, 0 });
111: }
112:
113: public void testByIdDescendingConstrained() {
114: Query query = Test.query();
115: query.constrain(getClass());
116: query.descend("_id").constrain(new Integer(3)).smaller();
117: assertIdOrder(query, new DescendingIdComparator(), new int[] {
118: 2, 1, 0 });
119: }
120:
121: public void testByIdDescendingNQ() {
122: ObjectSet result = Test.objectContainer().query(
123: new SmallerThanThreePredicate(),
124: new DescendingIdComparator());
125: assertIdOrder(result, new int[] { 2, 1, 0 });
126: }
127:
128: public void testByIdOddEven() {
129: assertIdOrder(new OddEvenIdComparator(),
130: new int[] { 0, 2, 1, 3 });
131: }
132:
133: public void testByIdOddEvenConstrained() {
134: Query query = Test.query();
135: query.constrain(getClass());
136: query.descend("_id").constrain(new Integer(3)).smaller();
137: assertIdOrder(query, new OddEvenIdComparator(), new int[] { 0,
138: 2, 1 });
139: }
140:
141: public void testByIdOddEvenNQ() {
142: ObjectSet result = Test.objectContainer().query(
143: new SmallerThanThreePredicate(),
144: new OddEvenIdComparator());
145: assertIdOrder(result, new int[] { 0, 2, 1 });
146: }
147:
148: public void testByNameAscending() {
149: assertIdOrder(new AscendingNameComparator(), new int[] { 3, 2,
150: 1, 0 });
151: }
152:
153: public void testByNameAscendingConstrained() {
154: Query query = Test.query();
155: query.constrain(getClass());
156: query.descend("_id").constrain(new Integer(3)).smaller();
157: assertIdOrder(query, new AscendingNameComparator(), new int[] {
158: 2, 1, 0 });
159: }
160:
161: public void testByNameAscendingNQ() {
162: ObjectSet result = Test.objectContainer().query(
163: new SmallerThanThreePredicate(),
164: new AscendingNameComparator());
165: assertIdOrder(result, new int[] { 2, 1, 0 });
166: }
167:
168: private void assertIdOrder(Comparator comparator, int[] ids) {
169: Query query = Test.query();
170: query.constrain(getClass());
171: assertIdOrder(query, comparator, ids);
172: }
173:
174: private void assertIdOrder(Query query, Comparator comparator,
175: int[] ids) {
176: query.sortBy(comparator);
177: ObjectSet result = query.execute();
178: assertIdOrder(result, ids);
179: }
180:
181: private void assertIdOrder(ObjectSet result, int[] ids) {
182: Test.ensureEquals(ids.length, result.size());
183: for (int idx = 0; idx < ids.length; idx++) {
184: Test.ensureEquals(ids[idx], ((JdkComparatorSort) result
185: .next())._id);
186: }
187: }
188:
189: public static void main(String[] args) {
190: Test.run(JdkComparatorSort.class);
191: }
192: }
|