2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.java.api.generator;
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;
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;
33 * By type member {@link Comparator} which provides sorting by type for members (variables)
34 * in a generated class.
36 * @param <T> TypeMember type
39 final class ByTypeMemberComparator<T extends TypeMember> implements Comparator<T>, Serializable {
41 private static final long serialVersionUID = 1L;
44 * Fixed-size comparison. These are all numeric types, boolean, empty, identityref.
46 private static final int RANK_FIXED_SIZE = 0;
48 * Variable-sized comparison across simple components. These are string, binary and bits type.
50 private static final int RANK_VARIABLE_ARRAY = 1;
52 * Variable-size comparison across complex components.
54 private static final int RANK_INSTANCE_IDENTIFIER = 2;
56 * Composite structure. DataObject, OpaqueObject and similar.
58 private static final int RANK_COMPOSITE = 3;
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);
76 private static final @NonNull ByTypeMemberComparator<?> INSTANCE = new ByTypeMemberComparator<>();
78 private ByTypeMemberComparator() {
83 * Returns the one and only instance of this class.
85 * @return this comparator
87 @SuppressWarnings("unchecked")
88 public static <T extends TypeMember> ByTypeMemberComparator<T> getInstance() {
89 return (ByTypeMemberComparator<T>) INSTANCE;
92 public static <T extends TypeMember> Collection<T> sort(final Collection<T> input) {
93 if (input.size() < 2) {
97 final List<T> ret = new ArrayList<>(input);
98 ret.sort(getInstance());
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);
112 return member1.getName().compareTo(member2.getName());
116 @SuppressWarnings("static-method")
117 private Object readResolve() {
121 private static Type getConcreteType(final Type type) {
122 if (type instanceof ConcreteType) {
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();
131 for (GeneratedProperty s : rootGto.getProperties()) {
132 if (TypeConstants.VALUE_PROP.equals(s.getName())) {
133 return s.getReturnType();
140 private static int rankOf(final Type type) {
141 if (FIXED_TYPES.contains(type) || BindingTypes.isIdentityType(type)) {
142 return RANK_FIXED_SIZE;
144 if (type.equals(BaseYangTypes.STRING_TYPE) || type.equals(Types.BYTE_ARRAY)) {
145 return RANK_VARIABLE_ARRAY;
147 if (type.equals(BindingTypes.INSTANCE_IDENTIFIER) || type.equals(BindingTypes.KEYED_INSTANCE_IDENTIFIER)) {
148 return RANK_INSTANCE_IDENTIFIER;
150 if (type instanceof GeneratedTransferObject gto) {
151 final TypeDefinition<?> typedef = topParentTransportObject(gto).getBaseType();
152 if (typedef instanceof BitsTypeDefinition) {
153 return RANK_VARIABLE_ARRAY;
156 return RANK_COMPOSITE;
159 private static GeneratedTransferObject topParentTransportObject(final GeneratedTransferObject type) {
160 GeneratedTransferObject ret = type;
161 GeneratedTransferObject parent = ret.getSuperType();
162 while (parent != null) {
164 parent = ret.getSuperType();