1 20 21 22 package org.snmp4j.agent; 23 24 import java.util.Comparator ; 25 import org.snmp4j.smi.OctetString; 26 27 45 public class MOScopeComparator implements Comparator { 46 47 public MOScopeComparator() { 48 } 49 50 62 public int compare(Object o1, Object o2) { 63 if (o1 == o2) { 64 return 0; } 66 int result = 0; 67 if (o2 instanceof MOQuery) { 68 if (!(o1 instanceof MOScope)) { 69 result = compare(((MOQuery)o1).getScope(), ((MOQuery)o2).getScope()); 70 } 71 else { 72 result = compareScopeAndQuery((MOScope) o1, (MOQuery) o2); 73 } 74 } 75 else if (o1 instanceof MOQuery) { 76 result = -compareScopeAndQuery((MOScope)o2, (MOQuery)o1); 77 } 78 else { 79 MOScope s1 = (MOScope) o1; 80 MOScope s2 = (MOScope) o2; 81 if ((s1 instanceof MOContextScope) && 82 (s2 instanceof MOContextScope)) { 83 OctetString c1 = ((MOContextScope) s1).getContext(); 84 OctetString c2 = ((MOContextScope) s2).getContext(); 85 if ((c1 != null) && (c2 != null)) { 86 result = c1.compareTo(c2); 87 } 88 } 89 if (result == 0) { 90 result = s1.getLowerBound().compareTo(s2.getLowerBound()); 91 if (result == 0) { 92 if (s1 instanceof MOContextScope) { 93 result = -1; 94 } 95 else { 96 result = 1; 97 } 98 } 99 } 100 } 101 return result; 102 } 103 104 private static int compareScopeAndQuery(MOScope scope, MOQuery query) { 105 int result = 0; 106 if (scope instanceof MOContextScope) { 107 OctetString c1 = ((MOContextScope)scope).getContext(); 108 OctetString c2 = query.getScope().getContext(); 109 if ((c1 != null) && (c2 != null)) { 110 result = c1.compareTo(c2); 111 } 112 } 113 if (result != 0) { 114 return result; 115 } 116 if (scope.getUpperBound() == null) { 117 return 1; 118 } 119 else { 120 result = 121 scope.getUpperBound().compareTo(query.getScope().getLowerBound()); 122 if (result == 0) { 123 if ((!scope.isUpperIncluded()) || 124 (!query.getScope().isLowerIncluded())) { 125 return -1; 126 } 127 } 128 } 129 return result; 130 } 131 132 public boolean equals(Object obj) { 133 return (this == obj); 134 } 135 136 public int hashCode() { 137 return super.hashCode(); 138 } 139 } 140 | Popular Tags |