1 20 21 22 package org.snmp4j.agent; 23 24 import org.snmp4j.smi.*; 25 26 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 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 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 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 136 public boolean covers(MOScope covered) { 137 return covers(this, covered); 138 } 139 140 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 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 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 toString() { 241 return getClass().getName()+ 242 "[lowerBound="+lowerBound+",lowerIncluded="+ 243 lowerIncluded+",upperBound="+upperBound+ 244 ",upperIncluded="+upperIncluded+"]"; 245 } 246 247 } 248 | Popular Tags |