@@ -38,6 +38,7 @@ import io.spine.server.entity.Entity
38
38
import io.spine.string.simply
39
39
import java.lang.reflect.Method
40
40
import java.lang.reflect.Modifier
41
+ import java.util.SortedMap
41
42
42
43
/* *
43
44
* The abstract base for classes for scanning routing methods defined in an entity class.
@@ -60,7 +61,7 @@ public sealed class RoutingMethodMap<I: Any>(
60
61
61
62
protected val idClass: Class <* > = GenericParameter .ID .argumentIn(this ::class .java)
62
63
63
- internal val methods: Map <Class <out SignalMessage >, RoutingMethod <I , * , * , * >>
64
+ internal val methods: SortedMap <Class <out SignalMessage >, RoutingMethod <I , * , * , * >>
64
65
65
66
init {
66
67
val collecting = mutableMapOf<Class <out SignalMessage >, RoutingMethod <I , * , * , * >>()
@@ -74,8 +75,7 @@ public sealed class RoutingMethodMap<I: Any>(
74
75
val firstParam = method.parameters[0 ].type as Class <out SignalMessage >
75
76
collecting[firstParam] = createMethod(method)
76
77
}
77
- // TODO:2025-01-09:alexander.yevsyukov: Add sorting by interfaces.
78
- methods = collecting.toMap()
78
+ methods = collecting.toSortedMap(SignalClassComparator ())
79
79
}
80
80
81
81
@Suppress(" ReturnCount" )
@@ -123,6 +123,36 @@ public sealed class RoutingMethodMap<I: Any>(
123
123
}
124
124
}
125
125
126
+ /* *
127
+ * Sorts classes of signal messages putting more abstract types further.
128
+ */
129
+ private class SignalClassComparator : Comparator <Class <out SignalMessage >> {
130
+
131
+ @Suppress(" ReturnCount" )
132
+ override fun compare (o1 : Class <out SignalMessage >, o2 : Class <out SignalMessage >): Int {
133
+ if (o1 == o2) {
134
+ return 0
135
+ }
136
+ // An interface should come after a class in the sorting.
137
+ if (o1.isInterface && ! o2.isInterface) {
138
+ return 1
139
+ }
140
+ if (! o1.isInterface && o2.isInterface) {
141
+ return - 1
142
+ }
143
+ // Both are either classes or interfaces.
144
+ // The one that is more abstract further in sorting.
145
+ if (o1.isAssignableFrom(o2)) {
146
+ return 1
147
+ }
148
+ if (o2.isAssignableFrom(o1)) {
149
+ return - 1
150
+ }
151
+ // Sort alphabetically then.
152
+ return o1.canonicalName.compareTo(o2.canonicalName)
153
+ }
154
+ }
155
+
126
156
/* *
127
157
* Collects routing methods for commands.
128
158
*/
0 commit comments