1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.search.impl;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Comparator;
22 import java.util.List;
23
24 import net.sf.ehcache.search.Result;
25 import net.sf.ehcache.store.StoreQuery.Ordering;
26
27 /***
28 * Compound sort ordering comparactor
29 *
30 * @author teck
31 */
32 public class OrderComparator implements Comparator<Result> {
33
34 private final List<Comparator<Result>> comparators;
35
36 /***
37 * Constructor
38 *
39 * @param orderings
40 */
41 public OrderComparator(List<Ordering> orderings) {
42 comparators = new ArrayList<Comparator<Result>>();
43 int pos = 0;
44 for (Ordering ordering : orderings) {
45 switch (ordering.getDirection()) {
46 case ASCENDING: {
47 comparators.add(new AscendingComparator(pos));
48 break;
49 }
50 case DESCENDING: {
51 comparators.add(new DescendingComparator(pos));
52 break;
53 }
54 default: {
55 throw new AssertionError(ordering.getDirection());
56 }
57 }
58
59 pos++;
60 }
61 }
62
63 /***
64 * {@inheritDoc}
65 */
66 public int compare(Result o1, Result o2) {
67 for (Comparator<Result> c : comparators) {
68 int cmp = c.compare(o1, o2);
69 if (cmp != 0) {
70 return cmp;
71 }
72 }
73 return 0;
74 }
75
76 /***
77 * Simple ascending comparator
78 */
79 private static class AscendingComparator implements Comparator<Result>, Serializable {
80
81 private final int pos;
82
83 AscendingComparator(int pos) {
84 this.pos = pos;
85 }
86
87 public int compare(Result o1, Result o2) {
88 Object attr1 = ((ResultImpl) o1).getSortAttribute(pos);
89 Object attr2 = ((ResultImpl) o2).getSortAttribute(pos);
90
91 if ((attr1 == null) && (attr2 == null)) {
92 return 0;
93 }
94
95 if (attr1 == null) {
96 return -1;
97 }
98
99 if (attr2 == null) {
100 return 1;
101 }
102
103 return ((Comparable) attr1).compareTo(attr2);
104 }
105 }
106
107 /***
108 * Simple descending comparator
109 */
110 private static class DescendingComparator implements Comparator<Result>, Serializable {
111
112 private final int pos;
113
114 DescendingComparator(int pos) {
115 this.pos = pos;
116 }
117
118 public int compare(Result o1, Result o2) {
119 Object attr1 = ((ResultImpl) o1).getSortAttribute(pos);
120 Object attr2 = ((ResultImpl) o2).getSortAttribute(pos);
121
122 if ((attr1 == null) && (attr2 == null)) {
123 return 0;
124 }
125
126 if (attr1 == null) {
127 return 1;
128 }
129
130 if (attr2 == null) {
131 return -1;
132 }
133
134 return ((Comparable) attr2).compareTo(attr1);
135 }
136 }
137 }