KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > util > LexicographicOctetStringComparator


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - LexicographicOctetStringComparator.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.mo.util;
23
24 import java.util.*;
25 import org.snmp4j.smi.OctetString;
26
27 public class LexicographicOctetStringComparator implements Comparator {
28
29   private boolean impliedLength = false;
30
31   public LexicographicOctetStringComparator() {
32   }
33
34   public LexicographicOctetStringComparator(boolean impliedLength) {
35     this.impliedLength = impliedLength;
36   }
37
38   /**
39    * Compares its two OctetString instances for lexicographic order.
40    *
41    * @param o1 the first object to be compared.
42    * @param o2 the second object to be compared.
43    * @return a negative integer, zero, or a positive integer as the first
44    * argument is less than, equal to, or greater than the second.
45    */

46   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
47     OctetString os1 = (OctetString) o1;
48     OctetString os2 = (OctetString) o2;
49
50     int result = 0;
51     if (!impliedLength) {
52       result = os1.length() - os2.length();
53     }
54     if (result == 0) {
55       result = os1.compareTo(os2);
56     }
57     return result;
58   }
59 }
60
Popular Tags