01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.algebra.evaluation.util;
07:
08: import java.util.Comparator;
09:
10: import org.openrdf.model.Value;
11: import org.openrdf.query.BindingSet;
12: import org.openrdf.query.QueryEvaluationException;
13: import org.openrdf.query.algebra.Order;
14: import org.openrdf.query.algebra.OrderElem;
15: import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
16: import org.slf4j.Logger;
17: import org.slf4j.LoggerFactory;
18:
19: /**
20: *
21: * @author james
22: *
23: */
24: public class OrderComparator implements Comparator<BindingSet> {
25: private Logger logger = LoggerFactory
26: .getLogger(OrderComparator.class);
27:
28: private EvaluationStrategy strategy;
29:
30: private Order order;
31:
32: private ValueComparator cmp;
33:
34: public OrderComparator(EvaluationStrategy strategy, Order order,
35: ValueComparator vcmp) {
36: this .strategy = strategy;
37: this .order = order;
38: this .cmp = vcmp;
39: }
40:
41: public int compare(BindingSet o1, BindingSet o2) {
42: try {
43: for (OrderElem element : order.getElements()) {
44: Value v1 = strategy.evaluate(element.getExpr(), o1);
45: Value v2 = strategy.evaluate(element.getExpr(), o2);
46: int compare = cmp.compare(v1, v2);
47: if (compare == 0)
48: continue;
49: if (element.isAscending())
50: return compare;
51: if (compare > 0)
52: return -1;
53: if (compare < 0)
54: return 1;
55: }
56: return 0;
57: } catch (QueryEvaluationException e) {
58: logger.debug(e.getMessage(), e);
59: return 0;
60: } catch (IllegalArgumentException e) {
61: logger.debug(e.getMessage(), e);
62: return 0;
63: }
64: }
65:
66: }
|