001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander V. Esin
020: * @version $Revision$
021: */package org.apache.harmony.security.x501;
022:
023: import java.io.Serializable;
024: import java.util.Comparator;
025:
026: import org.apache.harmony.security.utils.ObjectIdentifier;
027:
028: /**
029: * AttributeTypeAndValue comparator
030: *
031: */
032: public class AttributeTypeAndValueComparator implements Comparator,
033: Serializable {
034:
035: private static final long serialVersionUID = -1286471842007103132L;
036:
037: /**
038: * compares two AttributeTypeAndValues
039: *
040: * @param obj1
041: * first AttributeTypeAndValue
042: * @param obj2
043: * second AttributeTypeAndValue
044: * @return -1 of first AttributeTypeAndValue "less" than second
045: * AttributeTypeAndValue 1 otherwise, 0 if they are equal
046: */
047: public int compare(Object obj1, Object obj2) {
048: if (obj1 == obj2) {
049: return 0;
050: }
051:
052: AttributeTypeAndValue atav1 = (AttributeTypeAndValue) obj1;
053: AttributeTypeAndValue atav2 = (AttributeTypeAndValue) obj2;
054: String kw1 = atav1.getType().getName();
055: String kw2 = atav2.getType().getName();
056: if (kw1 != null && kw2 == null) {
057: return -1;
058: }
059: if (kw1 == null && kw2 != null) {
060: return 1;
061: }
062: if (kw1 != null && kw2 != null) {
063: return kw1.compareTo(kw2);
064: }
065:
066: return compateOids(atav1.getType(), atav2.getType());
067: }
068:
069: /**
070: * compares two Object identifiers
071: *
072: * @param oid1
073: * first OID
074: * @param oid2
075: * second OID
076: * @return -1 of first OID "less" than second OID 1 otherwise, 0 if they are
077: * equal
078: */
079: private static int compateOids(ObjectIdentifier oid1,
080: ObjectIdentifier oid2) {
081: if (oid1 == oid2) {
082: return 0;
083: }
084:
085: int[] ioid1 = oid1.getOid();
086: int[] ioid2 = oid2.getOid();
087: int min = ioid1.length < ioid2.length ? ioid1.length
088: : ioid2.length;
089: for (int i = 0; i < min; ++i) {
090: if (ioid1[i] < ioid2[i]) {
091: return -1;
092: }
093: if (ioid1[i] > ioid2[i]) {
094: return 1;
095: }
096: if ((i + 1) == ioid1.length && (i + 1) < ioid2.length) {
097: return -1;
098: }
099: if ((i + 1) < ioid1.length && (i + 1) == ioid2.length) {
100: return 1;
101: }
102: }
103: return 0;
104: }
105: }
|