org.snmp4j.agent.MOScopeComparator Maven / Gradle / Ivy
/*_############################################################################ _## _## SNMP4J-Agent - MOScopeComparator.java _## _## Copyright (C) 2005-2009 Frank Fock (SNMP4J.org) _## _## Licensed under the Apache License, Version 2.0 (the "License"); _## you may not use this file except in compliance with the License. _## You may obtain a copy of the License at _## _## http://www.apache.org/licenses/LICENSE-2.0 _## _## Unless required by applicable law or agreed to in writing, software _## distributed under the License is distributed on an "AS IS" BASIS, _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. _## See the License for the specific language governing permissions and _## limitations under the License. _## _##########################################################################*/ package org.snmp4j.agent; import java.util.Comparator; import org.snmp4j.smi.OctetString; /** * The
is less thanMOScopeComparator
compares two scopes with each other or * it compares a scope and a {@link MOQuery} with each other. ** Two scopes are compared by their context (if both are {@link MOContextScope} * instances) first and then by their lower bound. *
* A scope is compared with a query by comparing the scope with the queries * scope and then if both are deemed to be equal, the upper bound of the scope * is checked. If it is unbounded (upper bound is
null
o1o2
* and zero if both values are deemed to be equal and a value greater than * zero ifo1
is greater thano2
. */ public int compare(Object o1, Object o2) { if (o1 == o2) { return 0; // ensure identity is equal } int result = 0; if (o2 instanceof MOQuery) { if (!(o1 instanceof MOScope)) { result = compare(((MOQuery)o1).getScope(), ((MOQuery)o2).getScope()); } else { result = compareScopeAndQuery((MOScope) o1, (MOQuery) o2); } } else if (o1 instanceof MOQuery) { result = -compareScopeAndQuery((MOScope)o2, (MOQuery)o1); } else { MOScope s1 = (MOScope) o1; MOScope s2 = (MOScope) o2; if ((s1 instanceof MOContextScope) && (s2 instanceof MOContextScope)) { OctetString c1 = ((MOContextScope) s1).getContext(); OctetString c2 = ((MOContextScope) s2).getContext(); if ((c1 != null) && (c2 != null)) { result = c1.compareTo(c2); } } if (result == 0) { result = s1.getLowerBound().compareTo(s2.getLowerBound()); if (result == 0) { if (s1 instanceof MOContextScope) { result = -1; } else { result = 1; } } } } return result; } private static int compareScopeAndQuery(MOScope scope, MOQuery query) { int result = 0; if (scope instanceof MOContextScope) { OctetString c1 = ((MOContextScope)scope).getContext(); OctetString c2 = query.getScope().getContext(); if ((c1 != null) && (c2 != null)) { result = c1.compareTo(c2); } } if (result != 0) { return result; } if (scope.getUpperBound() == null) { return 1; } else { result = scope.getUpperBound().compareTo(query.getScope().getLowerBound()); if (result == 0) { if ((!scope.isUpperIncluded()) || (!query.getScope().isLowerIncluded())) { return -1; } } } return result; } public boolean equals(Object obj) { return (this == obj); } public int hashCode() { return super.hashCode(); } }