ed33bac1e9b631928c73b8e91f0e2bdee6597ebf
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / 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.util;
9
10 import java.net.URI;
11 import java.util.Comparator;
12 import java.util.Date;
13
14 import org.opendaylight.yangtools.yang.common.QName;
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 qname's local name.
22      */
23     public static final QNameComparator QNAME_COMP = new QNameComparator();
24
25     /**
26      * Comparator based on alphabetical order of local name of SchemaNode's qname.
27      */
28     public static final SchemaNodeComparator SCHEMA_NODE_COMP = new SchemaNodeComparator();
29
30     /**
31      * Comparator based on augment target path length.
32      */
33     public static final AugmentComparator AUGMENT_COMP = new AugmentComparator();
34
35     private Comparators() {
36     }
37
38     private static final class QNameComparator implements Comparator<QName> {
39         @Override
40         public int compare(QName o1, QName o2) {
41             return o1.getLocalName().compareTo(o2.getLocalName());
42         }
43     }
44
45     private static final class SchemaNodeComparator implements Comparator<SchemaNode> {
46         @Override
47         public int compare(SchemaNode o1, SchemaNode o2) {
48             QName q1 = o1.getQName();
49             QName q2 = o2.getQName();
50             int result = q1.getLocalName().compareTo(q2.getLocalName());
51             if (result == 0) {
52                 URI ns1 = q1.getNamespace();
53                 URI ns2 = q2.getNamespace();
54                 if (ns1 == null && ns2 == null) {
55                     Date rev1 = q1.getRevision();
56                     Date rev2 = q2.getRevision();
57
58                     if (rev1 == null && rev2 == null) {
59                         String p1 = q1.getPrefix();
60                         String p2 = q2.getPrefix();
61                         if (p1 == null && p2 == null) {
62                             throw new IllegalArgumentException("Failed to sort nodes: " + o1 + ", " + o2);
63                         }
64                         if (p1 == null || p2 == null) {
65                             if (p1 == null) {
66                                 return -1;
67                             } else {
68                                 return 1;
69                             }
70                         }
71                         return p1.compareTo(p2);
72                     }
73                     if (rev1 == null || rev2 == null) {
74                         if (rev1 == null) {
75                             return -1;
76                         } else {
77                             return -2;
78                         }
79                     }
80                     return rev1.compareTo(rev2);
81                 }
82                 if (ns1 == null || ns2 == null) {
83                     if (ns1 == null) {
84                         return -1;
85                     } else {
86                         return 1;
87                     }
88                 }
89                 return ns1.toString().compareTo(ns2.toString());
90             } else {
91                 return result;
92             }
93         }
94     }
95
96     private static final class AugmentComparator implements Comparator<AugmentationSchemaBuilder> {
97         @Override
98         public int compare(AugmentationSchemaBuilder o1, AugmentationSchemaBuilder o2) {
99             return o1.getTargetPath().getPath().size() - o2.getTargetPath().getPath().size();
100         }
101
102     }
103
104 }