DataTypes extends Registry<DataType> { @VisibleForTesting static Comparator<Class<?>> closestTo(Class<?> target) { return (o1, o2) -> { if (o1 == o2) { return 0; } if (o1 == null) { return -1; } else if (o2 == null) { return 1; } if (o1 == target) { return 1; } else if (o2 == target) { return -1; } return Integer.compare(distance(o2, target), distance(o1, target)); }; } DataTypes(); static DataTypes getDefault(); @VisibleForTesting static void setDefault(DataTypes instance); @Override void register(DataType dataType); @Override void unregister(DataType dataType); Optional<DataType> forName(String name); @SuppressWarnings("unchecked") Optional<DataType<T>> forJavaType(Class<T> type); Set<DataType> forJavaTypes(Class<?>... types); @SuppressWarnings("unchecked") Optional<D> forType(Class<D> clazz); Set<DataType> forTypes(Class<? extends DataType>... types); static boolean isCompatible(DataType type, Collection<? extends DataType> types); static final DataType None; static final DataType All; static final DataType Unknown; static final ComplexDataType<MapData> Map; static final DataType<Boolean> Boolean; static final DataType<boolean[]> BooleanArray; static final DataType<Number> Number; static final DataType<double[]> NumberArray; static final DataType<String> String; static final DataType<String[]> StringArray; static final DataType<byte[]> ByteArray; }
@Test public void testClosestTo() { final List<Class<?>> classes = Lists.newArrayList(C.class, B.class, D.class, A.class); classes.sort(closestTo(Object.class)); assertEquals(Lists.newArrayList(D.class, C.class, B.class, A.class), classes); classes.sort(closestTo(D.class)); assertEquals(Lists.newArrayList(A.class, B.class, C.class, D.class), classes); }