KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - DefaultMOContextScope.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.OctetString;
25 import org.snmp4j.smi.OID;
26
27 /**
28  * The <code>DefaultMOContextScope</code> is the default implementation of
29  * a {@link MOContextScope} representing an OID scope that distinguishes between
30  * different contexts.
31  *
32  * @author Frank Fock
33  * @version 1.1
34  */

35 public class DefaultMOContextScope
36     extends DefaultMOScope implements MOContextScope
37 {
38
39   private OctetString context;
40
41   /**
42    * Creates a context scope from a context, upper, and lower bound OID.
43    * @param context
44    * the context for which this scope is valid.
45    * @param lowerBound
46    * the lower bound of the OID scope (must not be <code>null</code>).
47    * @param lowerIncluded
48    * specifies whether the lower bound is included or not.
49    * @param upperBound
50    * the upper bound of the OID scope (<code>null</code> for no upper limit).
51    * @param upperIncluded
52    * specifies whether the upper bound is included or not.
53    */

54   public DefaultMOContextScope(OctetString context,
55                                OID lowerBound, boolean lowerIncluded,
56                                OID upperBound, boolean upperIncluded) {
57     super(lowerBound, lowerIncluded, upperBound, upperIncluded);
58     this.context = context;
59   }
60
61   /**
62    * Creates a context scope from another context scope.
63    * @param scope
64    * a MOContextScope instance whose context and bounds are copied by
65    * reference.
66    */

67   public DefaultMOContextScope(MOContextScope scope) {
68     super(scope.getLowerBound(), scope.isLowerIncluded(), scope.getUpperBound(),
69           scope.isUpperIncluded());
70     this.context = scope.getContext();
71   }
72
73   /**
74    * Creates a context scope from a plain OID scope.
75    * @param context
76    * the context name for the new context scope.
77    * @param extendedScope
78    * the OID scope that defines the OID range of the new scope (boundaries
79    * are copied by reference).
80    */

81   public DefaultMOContextScope(OctetString context,
82                                MOScope extendedScope) {
83     super(extendedScope);
84     this.context = context;
85   }
86
87   /**
88    * Gets the context of the scope.
89    * @return
90    * the context name this scope applies to.
91    */

92   public OctetString getContext() {
93     return context;
94   }
95
96   /**
97    * Sets the context name for this scope.
98    * @param context
99    * a context name.
100    */

101   public void setContext(OctetString context) {
102     this.context = context;
103   }
104
105   /**
106    * Indicates whether an object is equal to this one.
107    * @param obj
108    * some object.
109    * @return
110    * <code>true</code> only if <code>obj</code> is a {@link MOContextScope}
111    * and if context and scope equals this one's.
112    */

113   public boolean equals(Object JavaDoc obj) {
114     if (obj instanceof MOContextScope) {
115       MOContextScope other = (MOContextScope)obj;
116       return (context.equals(other.getContext()) && super.equals(obj));
117     }
118     return false;
119   }
120
121   public int hashCode() {
122     if (context != null) {
123       int hash = super.hashCode();
124       // One-at-a-time Hash adapted from Bob Jenkins
125
for (int i = 0; i < context.length(); i++) {
126           hash += context.get(i);
127           hash += (hash << 10);
128           hash ^= (hash >> 6);
129       }
130       hash += (hash << 3);
131       hash ^= (hash >> 11);
132       hash += (hash << 15);
133       return hash;
134     }
135     return super.hashCode();
136   }
137
138   /**
139    * Indicates whether the given scopes have a matching context. The context
140    * does not match if both are {@link MOContextScope} instances and both
141    * contexts are not <code>null</code> and different.
142    * @param a
143    * a MOScope instance.
144    * @param b
145    * another MOScope instance.
146    * @return
147    * <code>true</code> if both scopes have matching contexts (or at least one
148    * has no context defined).
149    * @since 1.1
150    */

151   public static final boolean isContextMatching(MOScope a, MOScope b) {
152     if ((a instanceof MOContextScope) &&
153         (b instanceof MOContextScope)) {
154       OctetString ca = ((MOContextScope)a).getContext();
155       OctetString cb = ((MOContextScope)b).getContext();
156       if ((ca != null) && (!ca.equals(cb))) {
157         return false;
158       }
159     }
160     return true;
161   }
162
163   public boolean isCovered(MOScope other) {
164     if ((context != null) && (other instanceof MOContextScope)) {
165       if (!context.equals(((MOContextScope)other).getContext())) {
166         return false;
167       }
168     }
169     return covers(this, other);
170   }
171
172   public String JavaDoc toString() {
173     return getClass().getName()+"[context="+context+
174         ",lowerBound="+lowerBound+
175         ",lowerIncluded="+lowerIncluded+
176         ",upperBound="+upperBound+
177         ",upperIncluded="+upperIncluded+"]";
178   }
179
180   public boolean isOverlapping(MOScope other) {
181     if (!isContextMatching(this, other)) {
182       return false;
183     }
184     return super.isOverlapping(other);
185   }
186
187 }
188
Popular Tags