01: /*
02: * argun 1.0
03: * Web 2.0 delivery framework
04: * Copyright (C) 2007 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.biz
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.web.menu.matchers;
24:
25: import java.util.Comparator;
26:
27: import biz.hammurapi.web.menu.Menu;
28:
29: /**
30: * @author Pavel Vlasov
31: * @version $Revision: 1.1 $
32: */
33: public class MatchResultComparator implements Comparator {
34:
35: /**
36: * Better matched is less as it should be first in the list
37: */
38: public int compare(Object o1, Object o2) {
39: if (o1 == o2) {
40: return 0;
41: } else if (o1 instanceof MatchResult
42: && o2 instanceof MatchResult) {
43: MatchResult mr1 = (MatchResult) o1;
44: MatchResult mr2 = (MatchResult) o2;
45:
46: if (mr1.getWeight() != mr2.getWeight()) {
47: return mr2.getWeight() - mr1.getWeight();
48: }
49:
50: RequestMatcher rm1 = mr1.getMatcher();
51: RequestMatcher rm2 = mr2.getMatcher();
52:
53: if (rm1 instanceof Menu && rm2 instanceof Menu) {
54: Menu m1 = (Menu) rm1;
55: Menu m2 = (Menu) rm2;
56:
57: // Return last matched
58: int cr = m2.getLastMatch() - m1.getLastMatch();
59: if (cr != 0) {
60: return cr;
61: }
62:
63: // Return with shortest path
64: cr = m1.getPath().length - m2.getPath().length;
65: if (cr != 0) {
66: return cr;
67: }
68:
69: cr = m1.getName().compareTo(m2.getName());
70: if (cr != 0) {
71: return cr;
72: }
73: }
74:
75: return rm1.hashCode() - rm2.hashCode();
76: } else {
77: throw new IllegalArgumentException(
78: "Objects being compared are not of type MatchResut: "
79: + o1.getClass().getName() + ", "
80: + o2.getClass().getName());
81: }
82: }
83: }
|