• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 /**
19 * @author Alexander V. Esin
20 * @version $Revision$
21 */
22 
23 package org.apache.harmony.security.x501;
24 
25 import java.io.Serializable;
26 import java.util.Comparator;
27 import org.apache.harmony.security.utils.ObjectIdentifier;
28 
29 /**
30  * AttributeTypeAndValue comparator
31  *
32  */
33 public class AttributeTypeAndValueComparator implements Comparator, Serializable {
34 
35     private static final long serialVersionUID = -1286471842007103132L;
36 
37     /**
38      * compares two AttributeTypeAndValues
39      *
40      * @param obj1
41      *            first AttributeTypeAndValue
42      * @param obj2
43      *            second AttributeTypeAndValue
44      * @return -1 of first AttributeTypeAndValue "less" than second
45      *         AttributeTypeAndValue 1 otherwise, 0 if they are equal
46      */
compare(Object obj1, Object obj2)47     public int compare(Object obj1, Object obj2) {
48         if (obj1 == obj2) {
49             return 0;
50         }
51 
52         AttributeTypeAndValue atav1 = (AttributeTypeAndValue) obj1;
53         AttributeTypeAndValue atav2 = (AttributeTypeAndValue) obj2;
54         String kw1 = atav1.getType().getName();
55         String kw2 = atav2.getType().getName();
56         if (kw1 != null && kw2 == null) {
57             return -1;
58         }
59         if (kw1 == null && kw2 != null) {
60             return 1;
61         }
62         if (kw1 != null && kw2 != null) {
63             return kw1.compareTo(kw2);
64         }
65 
66         return compateOids(atav1.getType(), atav2.getType());
67     }
68 
69     /**
70      * compares two Object identifiers
71      *
72      * @param oid1
73      *            first OID
74      * @param oid2
75      *            second OID
76      * @return -1 of first OID "less" than second OID 1 otherwise, 0 if they are
77      *         equal
78      */
compateOids(ObjectIdentifier oid1, ObjectIdentifier oid2)79     private static int compateOids(ObjectIdentifier oid1, ObjectIdentifier oid2) {
80         if (oid1 == oid2) {
81             return 0;
82         }
83 
84         int[] ioid1 = oid1.getOid();
85         int[] ioid2 = oid2.getOid();
86         int min = ioid1.length < ioid2.length ? ioid1.length : ioid2.length;
87         for (int i = 0; i < min; ++i) {
88             if (ioid1[i] < ioid2[i]) {
89                 return -1;
90             }
91             if (ioid1[i] > ioid2[i]) {
92                 return 1;
93             }
94             if ((i + 1) == ioid1.length && (i + 1) < ioid2.length) {
95                 return -1;
96             }
97             if ((i + 1) < ioid1.length && (i + 1) == ioid2.length) {
98                 return 1;
99             }
100         }
101         return 0;
102     }
103 }
104