Do not generate prime when not needed
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ByTypeMemberComparator.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.mdsal.binding.java.api.generator;
9
10 import com.google.common.annotations.Beta;
11 import java.io.Serial;
12 import java.io.Serializable;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Comparator;
16 import java.util.List;
17 import java.util.Set;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.mdsal.binding.model.api.TypeMember;
25 import org.opendaylight.mdsal.binding.model.ri.BaseYangTypes;
26 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
27 import org.opendaylight.mdsal.binding.model.ri.TypeConstants;
28 import org.opendaylight.mdsal.binding.model.ri.Types;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
31
32 /**
33  * By type member {@link Comparator} which provides sorting by type for members (variables)
34  * in a generated class.
35  *
36  * @param <T> TypeMember type
37  */
38 @Beta
39 final class ByTypeMemberComparator<T extends TypeMember> implements Comparator<T>, Serializable {
40     @Serial
41     private static final long serialVersionUID = 1L;
42
43     /**
44      * Fixed-size comparison. These are all numeric types, boolean, empty, identityref.
45      */
46     private static final int RANK_FIXED_SIZE          = 0;
47     /**
48      * Variable-sized comparison across simple components. These are string, binary and bits type.
49      */
50     private static final int RANK_VARIABLE_ARRAY      = 1;
51     /**
52      * Variable-size comparison across complex components.
53      */
54     private static final int RANK_INSTANCE_IDENTIFIER = 2;
55     /**
56      * Composite structure. DataObject, OpaqueObject and similar.
57      */
58     private static final int RANK_COMPOSITE           = 3;
59
60     private static final Set<Type> FIXED_TYPES = Set.of(
61         BaseYangTypes.INT8_TYPE,
62         BaseYangTypes.INT16_TYPE,
63         BaseYangTypes.INT32_TYPE,
64         BaseYangTypes.INT64_TYPE,
65         BaseYangTypes.DECIMAL64_TYPE,
66         BaseYangTypes.UINT8_TYPE,
67         BaseYangTypes.UINT16_TYPE,
68         BaseYangTypes.UINT32_TYPE,
69         BaseYangTypes.UINT64_TYPE,
70         BaseYangTypes.BOOLEAN_TYPE,
71         BaseYangTypes.EMPTY_TYPE);
72
73     /**
74      * Singleton instance.
75      */
76     private static final @NonNull ByTypeMemberComparator<?> INSTANCE = new ByTypeMemberComparator<>();
77
78     private ByTypeMemberComparator() {
79         // Hidden on purpose
80     }
81
82     /**
83      * Returns the one and only instance of this class.
84      *
85      * @return this comparator
86      */
87     @SuppressWarnings("unchecked")
88     public static <T extends TypeMember> ByTypeMemberComparator<T> getInstance() {
89         return (ByTypeMemberComparator<T>) INSTANCE;
90     }
91
92     public static <T extends TypeMember> Collection<T> sort(final Collection<T> input) {
93         if (input.size() < 2) {
94             return input;
95         }
96
97         final List<T> ret = new ArrayList<>(input);
98         ret.sort(getInstance());
99         return ret;
100     }
101
102     @Override
103     public int compare(final T member1, final T member2) {
104         final Type type1 = getConcreteType(member1.getReturnType());
105         final Type type2 = getConcreteType(member2.getReturnType());
106         if (!type1.getIdentifier().equals(type2.getIdentifier())) {
107             final int cmp = rankOf(type1) - rankOf(type2);
108             if (cmp != 0) {
109                 return cmp;
110             }
111         }
112         return member1.getName().compareTo(member2.getName());
113     }
114
115     @Serial
116     @SuppressWarnings("static-method")
117     private Object readResolve() {
118         return INSTANCE;
119     }
120
121     private static Type getConcreteType(final Type type) {
122         if (type instanceof ConcreteType) {
123             return type;
124         } else if (type instanceof ParameterizedType generated) {
125             return generated.getRawType();
126         } else if (type instanceof GeneratedTransferObject gto) {
127             GeneratedTransferObject rootGto = gto;
128             while (rootGto.getSuperType() != null) {
129                 rootGto = rootGto.getSuperType();
130             }
131             for (GeneratedProperty s : rootGto.getProperties()) {
132                 if (TypeConstants.VALUE_PROP.equals(s.getName())) {
133                     return s.getReturnType();
134                 }
135             }
136         }
137         return type;
138     }
139
140     private static int rankOf(final Type type) {
141         if (FIXED_TYPES.contains(type) || BindingTypes.isIdentityType(type)) {
142             return RANK_FIXED_SIZE;
143         }
144         if (type.equals(BaseYangTypes.STRING_TYPE) || type.equals(Types.BYTE_ARRAY)) {
145             return RANK_VARIABLE_ARRAY;
146         }
147         if (type.equals(BindingTypes.INSTANCE_IDENTIFIER) || type.equals(BindingTypes.KEYED_INSTANCE_IDENTIFIER)) {
148             return RANK_INSTANCE_IDENTIFIER;
149         }
150         if (type instanceof GeneratedTransferObject gto) {
151             final TypeDefinition<?> typedef = topParentTransportObject(gto).getBaseType();
152             if (typedef instanceof BitsTypeDefinition) {
153                 return RANK_VARIABLE_ARRAY;
154             }
155         }
156         return RANK_COMPOSITE;
157     }
158
159     private static GeneratedTransferObject topParentTransportObject(final GeneratedTransferObject type) {
160         GeneratedTransferObject ret = type;
161         GeneratedTransferObject parent = ret.getSuperType();
162         while (parent != null) {
163             ret = parent;
164             parent = ret.getSuperType();
165         }
166         return ret;
167     }
168 }