KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > MOScopeComparator


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - MOScopeComparator.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent;
23
24 import java.util.Comparator JavaDoc;
25 import org.snmp4j.smi.OctetString;
26
27 /**
28  * The <code>MOScopeComparator</code> compares two scopes with each other or
29  * it compares a scope and a {@link MOQuery} with each other.
30  * <p>
31  * Two scopes are compared by their context (if both are {@link MOContextScope}
32  * instances) first and then by their lower bound.
33  * <p>
34  * A scope is compared with a query by comparing the scope with the queries
35  * scope and then if both are deemed to be equal, the upper bound of the scope
36  * is checked. If it is unbounded (upper bound is <code>null</code), then
37  * the scoped is deemed to be greater than the query. Otherwise, the upper bound
38  * of the scope is compared with the lower bound of the query. Scope and query
39  * are deemd to be equal if both bounds are equal and are both included.
40  * Otherwise the scope is deemed to be less than the query.
41  *
42  * @author Frank Fock
43  * @version 1.0
44  */

45 public class MOScopeComparator implements Comparator JavaDoc {
46
47   public MOScopeComparator() {
48   }
49
50   /**
51    * Compares a scope with another scope or query. See also the class
52    * description how comparison is done.
53    * @param o1
54    * a MOscope or MOQuery instance.
55    * @param o2
56    * a MOscope or MOQuery instance.
57    * @return
58    * an integer less than zero if <code>o1</code> is less than <code>o2</code>
59    * and zero if both values are deemed to be equal and a value greater than
60    * zero if <code>o1</code> is greater than <code>o2</code>.
61    */

62   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
63     if (o1 == o2) {
64       return 0; // ensure identity is equal
65
}
66     int result = 0;
67     if (o2 instanceof MOQuery) {
68       if (!(o1 instanceof MOScope)) {
69         result = compare(((MOQuery)o1).getScope(), ((MOQuery)o2).getScope());
70       }
71       else {
72         result = compareScopeAndQuery((MOScope) o1, (MOQuery) o2);
73       }
74     }
75     else if (o1 instanceof MOQuery) {
76       result = -compareScopeAndQuery((MOScope)o2, (MOQuery)o1);
77     }
78     else {
79       MOScope s1 = (MOScope) o1;
80       MOScope s2 = (MOScope) o2;
81       if ((s1 instanceof MOContextScope) &&
82           (s2 instanceof MOContextScope)) {
83         OctetString c1 = ((MOContextScope) s1).getContext();
84         OctetString c2 = ((MOContextScope) s2).getContext();
85         if ((c1 != null) && (c2 != null)) {
86           result = c1.compareTo(c2);
87         }
88       }
89       if (result == 0) {
90         result = s1.getLowerBound().compareTo(s2.getLowerBound());
91         if (result == 0) {
92           if (s1 instanceof MOContextScope) {
93             result = -1;
94           }
95           else {
96             result = 1;
97           }
98         }
99       }
100     }
101     return result;
102   }
103
104   private static int compareScopeAndQuery(MOScope scope, MOQuery query) {
105     int result = 0;
106     if (scope instanceof MOContextScope) {
107       OctetString c1 = ((MOContextScope)scope).getContext();
108       OctetString c2 = query.getScope().getContext();
109       if ((c1 != null) && (c2 != null)) {
110         result = c1.compareTo(c2);
111       }
112     }
113     if (result != 0) {
114       return result;
115     }
116     if (scope.getUpperBound() == null) {
117       return 1;
118     }
119     else {
120       result =
121           scope.getUpperBound().compareTo(query.getScope().getLowerBound());
122       if (result == 0) {
123         if ((!scope.isUpperIncluded()) ||
124             (!query.getScope().isLowerIncluded())) {
125           return -1;
126         }
127       }
128     }
129     return result;
130   }
131
132   public boolean equals(Object JavaDoc obj) {
133     return (this == obj);
134   }
135
136   public int hashCode() {
137     return super.hashCode();
138   }
139 }
140
Popular Tags