001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - DefaultMOScope.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.agent;
022:
023: import org.snmp4j.smi.*;
024:
025: /**
026: * The <code>DefaultMOScope</code> is the default {@link MOScope} and
027: * {@link MutableMOScope} implementation.
028: *
029: * @author Frank Fock
030: * @version 1.0
031: */
032: public class DefaultMOScope implements MOScope, MutableMOScope {
033:
034: protected OID lowerBound;
035: protected OID upperBound;
036: protected boolean lowerIncluded;
037: protected boolean upperIncluded;
038:
039: /**
040: * Creates an OID scope from lower and upper bound.
041: * @param lowerBound
042: * the lower bound of the scope.
043: * @param lowerIncluded
044: * indicates whether the lower bound is included in the scope or not.
045: * @param upperBound
046: * the upper bound of the scope, <code>null</code> can be specified to
047: * set no upper limit.
048: * @param upperIncluded
049: * indicates whether the upper bound is included in the scope or not.
050: */
051: public DefaultMOScope(OID lowerBound, boolean lowerIncluded,
052: OID upperBound, boolean upperIncluded) {
053: this .lowerBound = lowerBound;
054: this .upperBound = upperBound;
055: this .lowerIncluded = lowerIncluded;
056: this .upperIncluded = upperIncluded;
057: }
058:
059: /**
060: * Creates a scope from another scope by referencing its bound values.
061: * @param other
062: * another scope.
063: */
064: public DefaultMOScope(MOScope other) {
065: this .lowerBound = other.getLowerBound();
066: this .upperBound = other.getUpperBound();
067: this .lowerIncluded = other.isLowerIncluded();
068: this .upperIncluded = other.isUpperIncluded();
069: }
070:
071: public OID getLowerBound() {
072: return lowerBound;
073: }
074:
075: public OID getUpperBound() {
076: return upperBound;
077: }
078:
079: public boolean isLowerIncluded() {
080: return lowerIncluded;
081: }
082:
083: public boolean isUpperIncluded() {
084: return upperIncluded;
085: }
086:
087: public boolean isCovered(MOScope other) {
088: return covers(this , other);
089: }
090:
091: public boolean isOverlapping(MOScope other) {
092: return overlaps(this , other);
093: }
094:
095: public void setLowerBound(OID lowerBound) {
096: this .lowerBound = lowerBound;
097: }
098:
099: public void setLowerIncluded(boolean lowerIncluded) {
100: this .lowerIncluded = lowerIncluded;
101: }
102:
103: public void setUpperBound(OID upperBound) {
104: this .upperBound = upperBound;
105: }
106:
107: public void setUpperIncluded(boolean upperIncluded) {
108: this .upperIncluded = upperIncluded;
109: }
110:
111: public boolean equals(Object obj) {
112: if (obj instanceof MOScope) {
113: MOScope other = (MOScope) obj;
114: return (lowerBound.equals(other.getLowerBound())
115: && (((upperBound == null) && (other.getUpperBound() == null)) || (upperBound
116: .equals(other.getUpperBound())))
117: && (lowerIncluded == other.isLowerIncluded()) && (upperIncluded == other
118: .isUpperIncluded()));
119: }
120: return false;
121: }
122:
123: public int hashCode() {
124: return lowerBound.hashCode();
125: }
126:
127: /**
128: * Indicates whether this scope covers by the supplied one, that is whether
129: * the lower bound of this scope is less or equal to the lower bound of the
130: * covered scope and if the upper bound is greater or equal to the upper
131: * bound of the covered scope.
132: *
133: * @param covered
134: * a MOScope instance.
135: * @return
136: * <code>true</code> if this OID scope covers the supplied one.
137: */
138: public boolean covers(MOScope covered) {
139: return covers(this , covered);
140: }
141:
142: /**
143: * Indicates whether the first supplied scope covers by second one.
144: * @param scope
145: * the covering scope.
146: * @param covered
147: * the covered scope.
148: * @return
149: * <code>true</code> if the lower bound of <code>scope</code> is less or
150: * equal to the lower bound of <code>covered</code> and if the upper bound
151: * is greater or equal to the upper bound of <code>covered</code>.
152: */
153: public static boolean covers(MOScope scope, MOScope covered) {
154: int lowerResult = scope.getLowerBound().compareTo(
155: covered.getLowerBound());
156: if ((lowerResult < 0)
157: || ((lowerResult == 0) && (scope.isLowerIncluded()))) {
158: if (scope.getUpperBound() == null) {
159: return true;
160: }
161: int upperResult = scope.getUpperBound().compareTo(
162: covered.getLowerBound());
163: if ((upperResult > 0)
164: || ((upperResult == 0) && (scope.isUpperIncluded()) && (covered
165: .isLowerIncluded()))) {
166: return true;
167: }
168: }
169: return false;
170: }
171:
172: /**
173: * Indicates whether the first scope supplied overlaps with the second one.
174: * If both scopes are instances of MOContextScope their context must match
175: *
176: * @param scope
177: * a MOScope instance.
178: * @param intersected
179: * the presumable intersected MOScope.
180: * @return
181: * <code>true</code> if <code>scope</code> overlaps any bound of
182: * <code>intersected</code>. This is always the case, if the upper bound
183: * of both scopes is <code>null</code>.
184: */
185: public static boolean overlaps(MOScope scope, MOScope intersected) {
186: OID iUpper = intersected.getUpperBound();
187: if (iUpper == null) {
188: if (scope.getUpperBound() == null) {
189: return true;
190: }
191: int upperResult = scope.getUpperBound().compareTo(
192: intersected.getLowerBound());
193: return ((upperResult > 0) || ((upperResult == 0) && (scope
194: .isUpperIncluded() && intersected.isLowerIncluded())));
195: }
196: int lowerResult = scope.getLowerBound().compareTo(iUpper);
197: int upperResult = 1;
198: if (scope.getUpperBound() != null) {
199: upperResult = scope.getUpperBound().compareTo(
200: intersected.getLowerBound());
201: }
202: if ((lowerResult == 0) && (scope.isLowerIncluded())
203: && (intersected.isUpperIncluded())) {
204: return true;
205: }
206: if ((upperResult == 0) && (scope.isUpperIncluded())
207: && (intersected.isLowerIncluded())) {
208: return true;
209: }
210: return (lowerResult < 0) && (upperResult > 0);
211: }
212:
213: public void substractScope(MOScope scope) {
214: lowerBound = scope.getUpperBound();
215: lowerIncluded = !scope.isUpperIncluded();
216: }
217:
218: public boolean covers(OID oid) {
219: if (oid == null) {
220: return false;
221: }
222: return (((getLowerBound().compareTo(oid) < 0) || (isLowerIncluded() && getLowerBound()
223: .equals(oid))) && ((getUpperBound() == null)
224: || (getUpperBound().compareTo(oid) > 0) || (isUpperIncluded() && getUpperBound()
225: .equals(oid))));
226: }
227:
228: /**
229: * Checks if this scope is empty or not. An empty scope cannot cover any
230: * OID (i.e. lower bound is greater than upper bound).
231: * @return
232: * <code>true</code> if lower bound is greater than upper bound or if
233: * both bounds equal but one of the bounds is not-included.
234: */
235: public boolean isEmpty() {
236: return (((lowerBound != null) && (upperBound != null)) && ((lowerBound
237: .compareTo(upperBound) > 0) || (lowerBound
238: .equals(upperBound) && !(isLowerIncluded() && isUpperIncluded()))));
239: }
240:
241: public String toString() {
242: return getClass().getName() + "[lowerBound=" + lowerBound
243: + ",lowerIncluded=" + lowerIncluded + ",upperBound="
244: + upperBound + ",upperIncluded=" + upperIncluded + "]";
245: }
246:
247: }
|