BUG-865: deprecate pre-Beryllium parser elements
[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 /**
19  * @deprecated Pre-Beryllium implementation, scheduled for removal.
20  */
21 @Deprecated
22 public final class Comparators {
23
24     /**
25      * Comparator based on alphabetical order of local name of SchemaNode's
26      * qname.
27      */
28     public static final SchemaNodeComparator SCHEMA_NODE_COMP = new SchemaNodeComparator();
29
30     /**
31      * Comparator based on augment target path.
32      */
33     public static final AugmentComparator AUGMENT_COMP = new AugmentComparator();
34
35     public static final AugmentBuilderComparator AUGMENT_BUILDER_COMP = new AugmentBuilderComparator();
36
37     private Comparators() {
38     }
39
40     private static final class SchemaNodeComparator implements Comparator<SchemaNode> {
41         @Override
42         public int compare(final SchemaNode o1, final SchemaNode o2) {
43             return o1.getQName().compareTo(o2.getQName());
44         }
45     }
46
47     private static final class AugmentBuilderComparator implements Comparator<AugmentationSchemaBuilder> {
48         @Override
49         public int compare(final AugmentationSchemaBuilder o1, final AugmentationSchemaBuilder o2) {
50             int length1 = Iterables.size(o1.getTargetPath().getPathFromRoot());
51             int length2 = Iterables.size(o2.getTargetPath().getPathFromRoot());
52             return length1 - length2;
53         }
54     }
55
56     private static final class AugmentComparator implements Comparator<AugmentationSchema> {
57         @Override
58         public int compare(final AugmentationSchema augSchema1, final AugmentationSchema augSchema2) {
59             final Iterator<QName> thisIt = augSchema1.getTargetPath().getPathFromRoot().iterator();
60             final Iterator<QName> otherIt = augSchema2.getTargetPath().getPathFromRoot().iterator();
61
62             while (thisIt.hasNext()) {
63                 if (otherIt.hasNext()) {
64                     final int comp = thisIt.next().compareTo(otherIt.next());
65                     if (comp != 0) {
66                         return comp;
67                     }
68                 } else {
69                     return 1;
70                 }
71             }
72             if (otherIt.hasNext()) {
73                 return -1;
74             }
75             return 0;
76         }
77
78     }
79
80 }