|
1 | 1 | package org.tensorics.core.tensorbacked.dimtyped;
|
2 | 2 |
|
| 3 | +import com.google.common.collect.ImmutableList; |
3 | 4 | import com.google.common.collect.ImmutableMap;
|
4 | 5 | import com.google.common.collect.ImmutableSet;
|
5 | 6 | import com.google.common.collect.Iterables;
|
6 | 7 | import com.google.common.reflect.TypeToken;
|
7 | 8 |
|
| 9 | +import java.util.Collection; |
| 10 | +import java.util.List; |
8 | 11 | import java.util.NoSuchElementException;
|
9 | 12 | import java.util.Set;
|
10 | 13 | import java.util.stream.Collectors;
|
11 | 14 | import java.util.stream.IntStream;
|
12 | 15 |
|
| 16 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
13 | 17 | import static java.util.stream.Collectors.toSet;
|
14 | 18 |
|
15 | 19 | public final class DimtypedTypes {
|
@@ -63,29 +67,42 @@ public static Class<? extends DimtypedTensorbacked> coordinateParametrizedSubtyp
|
63 | 67 | return Iterables.getOnlyElement(found);
|
64 | 68 | }
|
65 | 69 |
|
66 |
| - public static Set<Class<?>> dimensionsFrom(Class<? extends DimtypedTensorbacked> tensorbackedClass) { |
| 70 | + public static List<Class<?>> dimensionListFrom(Class<? extends DimtypedTensorbacked> tensorbackedClass) { |
67 | 71 | Class<? extends DimtypedTensorbacked> coordinateParametrized = coordinateParametrizedSubtypeOf(tensorbackedClass);
|
68 | 72 | int dimensionality = DIMENSION_PARAMETRIZED_TYPES.get(coordinateParametrized);
|
69 | 73 |
|
70 | 74 | TypeToken<?> tt = TypeToken.of(tensorbackedClass);
|
71 |
| - Set<Class<?>> dimensions = IntStream.range(0, dimensionality) |
| 75 | + List<Class<?>> dimensions = IntStream.range(0, dimensionality) |
72 | 76 | .mapToObj(i -> tt.resolveType(coordinateParametrized.getTypeParameters()[i]))
|
73 | 77 | .map(typeToken -> typeToken.getRawType())
|
74 |
| - .collect(toSet()); |
| 78 | + .collect(toImmutableList()); |
| 79 | + |
| 80 | + assertNoObjectDimension(tensorbackedClass, dimensions); |
| 81 | + assertDistinctDimensionality(coordinateParametrized, dimensions); |
| 82 | + return dimensions; |
| 83 | + } |
75 | 84 |
|
| 85 | + private static void assertNoObjectDimension(Class<? extends DimtypedTensorbacked> tensorbackedClass, Collection<Class<?>> dimensions) { |
76 | 86 | if (dimensions.contains(Object.class)) {
|
77 | 87 | /* If type parameters are not explicitely defined in a subtype, then they seem to be resolved to Object.class.
|
78 | 88 | However, this is not what we want for dimensions... */
|
79 | 89 | throw new IllegalArgumentException("At least one of the extracted dimensions is '" + Object.class + "'. This is most probably a conceptual error.\n" +
|
80 | 90 | "Most probably, the passed in class (" + tensorbackedClass + ") is not a valid subtype with resolvable type parameters.");
|
81 | 91 | }
|
82 |
| - if (dimensions.size() != dimensionality) { |
83 |
| - throw new IllegalArgumentException("The size of the set of the retrieved dimensions (size=" + dimensions.size() + ", dimensions=" + dimensions + "), " + |
| 92 | + } |
| 93 | + |
| 94 | + private static void assertDistinctDimensionality(Class<? extends DimtypedTensorbacked> coordinateParametrized, Collection<Class<?>> dimensions) { |
| 95 | + int dimensionality = DIMENSION_PARAMETRIZED_TYPES.get(coordinateParametrized); |
| 96 | + Collection<Class<?>> distinctDimensions = ImmutableSet.copyOf(dimensions); |
| 97 | + if (distinctDimensions.size() != dimensionality) { |
| 98 | + throw new IllegalArgumentException("The size of the set of the retrieved distinct dimensions (size=" + distinctDimensions.size() + ", dimensions=" + distinctDimensions + "), " + |
84 | 99 | "is not equal to the required number of dimensions (" + dimensionality + ") for the parametrized class " + coordinateParametrized + ".\n"
|
85 | 100 | + "Probably two dimensions are of the same type?");
|
86 | 101 | }
|
87 |
| - return dimensions; |
| 102 | + } |
88 | 103 |
|
| 104 | + public static Set<Class<?>> dimensionsFrom(Class<? extends DimtypedTensorbacked> tensorbackedClass) { |
| 105 | + return ImmutableSet.copyOf(dimensionListFrom(tensorbackedClass)); |
89 | 106 | }
|
90 | 107 |
|
91 | 108 | private static ImmutableSet<Class<? extends DimtypedTensorbacked>> allowedTypes() {
|
|
0 commit comments