Source Code Cross Referenced for JdkComparatorSort.java in  » Database-DBMS » db4o-6.4 » com » db4o » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db4o 6.4 » com.db4o.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.