26f19e07d4b53573201e19b6e379d35ad4ccc4a2
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / util / Comparators.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.builder.util;
9
10 import com.google.common.collect.Iterables;
11 import java.util.Comparator;
12 import java.util.Iterator;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
15 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
16 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
17
18 public final class Comparators {
19
20     /**
21      * Comparator based on alphabetical order of local name of SchemaNode's
22      * qname.
23      */
24     public static final SchemaNodeComparator SCHEMA_NODE_COMP = new SchemaNodeComparator();
25
26     /**
27      * Comparator based on augment target path.
28      */
29     public static final AugmentComparator AUGMENT_COMP = new AugmentComparator();
30
31     public static final AugmentBuilderComparator AUGMENT_BUILDER_COMP = new AugmentBuilderComparator();
32
33     private Comparators() {
34     }
35
36     private static final class SchemaNodeComparator implements Comparator<SchemaNode> {
37         @Override
38         public int compare(final SchemaNode o1, final SchemaNode o2) {
39             return o1.getQName().compareTo(o2.getQName());
40         }
41     }
42
43     private static final class AugmentBuilderComparator implements Comparator<AugmentationSchemaBuilder> {
44         @Override
45         public int compare(AugmentationSchemaBuilder o1, AugmentationSchemaBuilder o2) {
46             int length1 = Iterables.size(o1.getTargetPath().getPathFromRoot());
47             int length2 = Iterables.size(o2.getTargetPath().getPathFromRoot());
48             return length1 - length2;
49         }
50     }
51
52     private static final class AugmentComparator implements Comparator<AugmentationSchema> {
53         @Override
54         public int compare(AugmentationSchema augSchema1, AugmentationSchema augSchema2) {
55             final Iterator<QName> thisIt = augSchema1.getTargetPath().getPathFromRoot().iterator();
56             final Iterator<QName> otherIt = augSchema2.getTargetPath().getPathFromRoot().iterator();
57
58             while (thisIt.hasNext()) {
59                 if (otherIt.hasNext()) {
60                     final int comp = thisIt.next().compareTo(otherIt.next());
61                     if (comp != 0) {
62                         return comp;
63                     }
64                 } else {
65                     return 1;
66                 }
67             }
68             if (otherIt.hasNext()) {
69                 return -1;
70             }
71             return 0;
72         }
73
74     }
75
76 }