KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - DefaultMOScope.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 org.snmp4j.smi.*;
25
26 /**
27  * The <code>DefaultMOScope</code> is the default {@link MOScope} and
28  * {@link MutableMOScope} implementation.
29  *
30  * @author Frank Fock
31  * @version 1.0
32  */

33 public class DefaultMOScope implements MOScope, MutableMOScope {
34
35   protected OID lowerBound;
36   protected OID upperBound;
37   protected boolean lowerIncluded;
38   protected boolean upperIncluded;
39
40   /**
41    * Creates an OID scope from lower and upper bound.
42    * @param lowerBound
43    * the lower bound of the scope.
44    * @param lowerIncluded
45    * indicates whether the lower bound is included in the scope or not.
46    * @param upperBound
47    * the upper bound of the scope, <code>null</code> can be specified to
48    * set no upper limit.
49    * @param upperIncluded
50    * indicates whether the upper bound is included in the scope or not.
51    */

52   public DefaultMOScope(OID lowerBound, boolean lowerIncluded,
53                         OID upperBound, boolean upperIncluded) {
54     this.lowerBound = lowerBound;
55     this.upperBound = upperBound;
56     this.lowerIncluded = lowerIncluded;
57     this.upperIncluded = upperIncluded;
58   }
59
60   /**
61    * Creates a scope from another scope by referencing its bound values.
62    * @param other
63    * another scope.
64    */

65   public DefaultMOScope(MOScope other) {
66     this.lowerBound = other.getLowerBound();
67     this.upperBound = other.getUpperBound();
68     this.lowerIncluded = other.isLowerIncluded();
69     this.upperIncluded = other.isUpperIncluded();
70   }
71
72   public OID getLowerBound() {
73     return lowerBound;
74   }
75
76   public OID getUpperBound() {
77     return upperBound;
78   }
79
80   public boolean isLowerIncluded() {
81     return lowerIncluded;
82   }
83
84   public boolean isUpperIncluded() {
85     return upperIncluded;
86   }
87
88   public boolean isCovered(MOScope other) {
89     return covers(this, other);
90   }
91
92   public boolean isOverlapping(MOScope other) {
93     return overlaps(this, other);
94   }
95
96   public void setLowerBound(OID lowerBound) {
97     this.lowerBound = lowerBound;
98   }
99   public void setLowerIncluded(boolean lowerIncluded) {
100     this.lowerIncluded = lowerIncluded;
101   }
102   public void setUpperBound(OID upperBound) {
103     this.upperBound = upperBound;
104   }
105   public void setUpperIncluded(boolean upperIncluded) {
106     this.upperIncluded = upperIncluded;
107   }
108
109   public boolean equals(Object JavaDoc obj) {
110     if (obj instanceof MOScope) {
111       MOScope other = (MOScope)obj;
112       return (lowerBound.equals(other.getLowerBound()) &&
113               (((upperBound == null) && (other.getUpperBound() == null)) ||
114                (upperBound.equals(other.getUpperBound()))) &&
115               (lowerIncluded == other.isLowerIncluded()) &&
116               (upperIncluded == other.isUpperIncluded()));
117     }
118     return false;
119   }
120
121   public int hashCode() {
122     return lowerBound.hashCode();
123   }
124
125   /**
126    * Indicates whether this scope covers by the supplied one, that is whether
127    * the lower bound of this scope is less or equal to the lower bound of the
128    * covered scope and if the upper bound is greater or equal to the upper
129    * bound of the covered scope.
130    *
131    * @param covered
132    * a MOScope instance.
133    * @return
134    * <code>true</code> if this OID scope covers the supplied one.
135    */

136   public boolean covers(MOScope covered) {
137     return covers(this, covered);
138   }
139
140   /**
141    * Indicates whether the first supplied scope covers by second one.
142    * @param scope
143    * the covering scope.
144    * @param covered
145    * the covered scope.
146    * @return
147    * <code>true</code> if the lower bound of <code>scope</code> is less or
148    * equal to the lower bound of <code>covered</code> and if the upper bound
149    * is greater or equal to the upper bound of <code>covered</code>.
150    */

151   public static boolean covers(MOScope scope, MOScope covered) {
152     int lowerResult = scope.getLowerBound().compareTo(covered.getLowerBound());
153     if ((lowerResult < 0) ||
154         ((lowerResult == 0) && (scope.isLowerIncluded()))) {
155       if (scope.getUpperBound() == null) {
156         return true;
157       }
158       int upperResult =
159           scope.getUpperBound().compareTo(covered.getLowerBound());
160       if ((upperResult > 0) ||
161           ((upperResult == 0) && (scope.isUpperIncluded()) &&
162            (covered.isLowerIncluded()))) {
163         return true;
164       }
165     }
166     return false;
167   }
168
169   /**
170    * Indicates whether the first scope supplied overlaps with the second one.
171    * If both scopes are instances of MOContextScope their context must match
172    *
173    * @param scope
174    * a MOScope instance.
175    * @param intersected
176    * the presumable intersected MOScope.
177    * @return
178    * <code>true</code> if <code>scope</code> overlaps any bound of
179    * <code>intersected</code>. This is always the case, if the upper bound
180    * of both scopes is <code>null</code>.
181    */

182   public static boolean overlaps(MOScope scope, MOScope intersected) {
183     OID iUpper = intersected.getUpperBound();
184     if (iUpper == null) {
185       if (scope.getUpperBound() == null) {
186         return true;
187       }
188       int upperResult =
189           scope.getUpperBound().compareTo(intersected.getLowerBound());
190       return ((upperResult > 0) ||
191               ((upperResult == 0) &&
192                (scope.isUpperIncluded() && intersected.isLowerIncluded())));
193     }
194     int lowerResult = scope.getLowerBound().compareTo(iUpper);
195     int upperResult = 1;
196     if (scope.getUpperBound() != null) {
197       upperResult = scope.getUpperBound().compareTo(intersected.getLowerBound());
198     }
199     if ((lowerResult == 0) &&
200         (scope.isLowerIncluded()) && (intersected.isUpperIncluded())) {
201       return true;
202     }
203     if ((upperResult == 0) &&
204         (scope.isUpperIncluded()) && (intersected.isLowerIncluded())) {
205       return true;
206     }
207     return (lowerResult < 0) && (upperResult > 0);
208   }
209
210   public void substractScope(MOScope scope) {
211     lowerBound = scope.getUpperBound();
212     lowerIncluded = !scope.isUpperIncluded();
213   }
214
215   public boolean covers(OID oid) {
216     if (oid == null) {
217       return false;
218     }
219     return (((getLowerBound().compareTo(oid) < 0) ||
220              (isLowerIncluded() && getLowerBound().equals(oid))) &&
221             ((getUpperBound() == null) ||
222              (getUpperBound().compareTo(oid) > 0) ||
223              (isUpperIncluded() && getUpperBound().equals(oid))));
224   }
225
226   /**
227    * Checks if this scope is empty or not. An empty scope cannot cover any
228    * OID (i.e. lower bound is greater than upper bound).
229    * @return
230    * <code>true</code> if lower bound is greater than upper bound or if
231    * both bounds equal but one of the bounds is not-included.
232    */

233   public boolean isEmpty() {
234     return (((lowerBound != null) && (upperBound != null)) &&
235             ((lowerBound.compareTo(upperBound) > 0) ||
236              (lowerBound.equals(upperBound) &&
237               !(isLowerIncluded() && isUpperIncluded()))));
238   }
239
240   public String JavaDoc toString() {
241     return getClass().getName()+
242         "[lowerBound="+lowerBound+",lowerIncluded="+
243         lowerIncluded+",upperBound="+upperBound+
244         ",upperIncluded="+upperIncluded+"]";
245   }
246
247 }
248
Popular Tags