001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - DefaultMOContextScope.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.OctetString;
024: import org.snmp4j.smi.OID;
025:
026: /**
027: * The <code>DefaultMOContextScope</code> is the default implementation of
028: * a {@link MOContextScope} representing an OID scope that distinguishes between
029: * different contexts.
030: *
031: * @author Frank Fock
032: * @version 1.1
033: */
034: public class DefaultMOContextScope extends DefaultMOScope implements
035: MOContextScope {
036:
037: private OctetString context;
038:
039: /**
040: * Creates a context scope from a context, upper, and lower bound OID.
041: * @param context
042: * the context for which this scope is valid.
043: * @param lowerBound
044: * the lower bound of the OID scope (must not be <code>null</code>).
045: * @param lowerIncluded
046: * specifies whether the lower bound is included or not.
047: * @param upperBound
048: * the upper bound of the OID scope (<code>null</code> for no upper limit).
049: * @param upperIncluded
050: * specifies whether the upper bound is included or not.
051: */
052: public DefaultMOContextScope(OctetString context, OID lowerBound,
053: boolean lowerIncluded, OID upperBound, boolean upperIncluded) {
054: super (lowerBound, lowerIncluded, upperBound, upperIncluded);
055: this .context = context;
056: }
057:
058: /**
059: * Creates a context scope from another context scope.
060: * @param scope
061: * a MOContextScope instance whose context and bounds are copied by
062: * reference.
063: */
064: public DefaultMOContextScope(MOContextScope scope) {
065: super (scope.getLowerBound(), scope.isLowerIncluded(), scope
066: .getUpperBound(), scope.isUpperIncluded());
067: this .context = scope.getContext();
068: }
069:
070: /**
071: * Creates a context scope from a plain OID scope.
072: * @param context
073: * the context name for the new context scope.
074: * @param extendedScope
075: * the OID scope that defines the OID range of the new scope (boundaries
076: * are copied by reference).
077: */
078: public DefaultMOContextScope(OctetString context,
079: MOScope extendedScope) {
080: super (extendedScope);
081: this .context = context;
082: }
083:
084: /**
085: * Gets the context of the scope.
086: * @return
087: * the context name this scope applies to.
088: */
089: public OctetString getContext() {
090: return context;
091: }
092:
093: /**
094: * Sets the context name for this scope.
095: * @param context
096: * a context name.
097: */
098: public void setContext(OctetString context) {
099: this .context = context;
100: }
101:
102: /**
103: * Indicates whether an object is equal to this one.
104: * @param obj
105: * some object.
106: * @return
107: * <code>true</code> only if <code>obj</code> is a {@link MOContextScope}
108: * and if context and scope equals this one's.
109: */
110: public boolean equals(Object obj) {
111: if (obj instanceof MOContextScope) {
112: MOContextScope other = (MOContextScope) obj;
113: return (context.equals(other.getContext()) && super
114: .equals(obj));
115: }
116: return false;
117: }
118:
119: public int hashCode() {
120: if (context != null) {
121: int hash = super .hashCode();
122: // One-at-a-time Hash adapted from Bob Jenkins
123: for (int i = 0; i < context.length(); i++) {
124: hash += context.get(i);
125: hash += (hash << 10);
126: hash ^= (hash >> 6);
127: }
128: hash += (hash << 3);
129: hash ^= (hash >> 11);
130: hash += (hash << 15);
131: return hash;
132: }
133: return super .hashCode();
134: }
135:
136: /**
137: * Indicates whether the given scopes have a matching context. The context
138: * does not match if both are {@link MOContextScope} instances and both
139: * contexts are not <code>null</code> and different.
140: * @param a
141: * a MOScope instance.
142: * @param b
143: * another MOScope instance.
144: * @return
145: * <code>true</code> if both scopes have matching contexts (or at least one
146: * has no context defined).
147: * @since 1.1
148: */
149: public static final boolean isContextMatching(MOScope a, MOScope b) {
150: if ((a instanceof MOContextScope)
151: && (b instanceof MOContextScope)) {
152: OctetString ca = ((MOContextScope) a).getContext();
153: OctetString cb = ((MOContextScope) b).getContext();
154: if ((ca != null) && (!ca.equals(cb))) {
155: return false;
156: }
157: }
158: return true;
159: }
160:
161: public boolean isCovered(MOScope other) {
162: if ((context != null) && (other instanceof MOContextScope)) {
163: if (!context.equals(((MOContextScope) other).getContext())) {
164: return false;
165: }
166: }
167: return covers(this , other);
168: }
169:
170: public String toString() {
171: return getClass().getName() + "[context=" + context
172: + ",lowerBound=" + lowerBound + ",lowerIncluded="
173: + lowerIncluded + ",upperBound=" + upperBound
174: + ",upperIncluded=" + upperIncluded + "]";
175: }
176:
177: public boolean isOverlapping(MOScope other) {
178: if (!isContextMatching(this , other)) {
179: return false;
180: }
181: return super.isOverlapping(other);
182: }
183:
184: }
|