import java.lang.reflect.Modifier;
import java.util.Arrays;
import javax.annotation.concurrent.ThreadSafe;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Immutable;
@NonNullByDefault
@ThreadSafe
public abstract class DerivedString<R extends DerivedString<R>> implements Comparable<R>, Immutable, Serializable {
- private static final class Validator extends ClassValue<Boolean> {
- private static final Logger LOG = LoggerFactory.getLogger(Validator.class);
+ private abstract static class AbstractValidator extends ClassValue<Boolean> {
+ private static final Logger LOG = LoggerFactory.getLogger(AbstractValidator.class);
@Override
- protected Boolean computeValue(final @Nullable Class<?> type) {
+ protected final Boolean computeValue(final @Nullable Class<?> type) {
// Every DerivedString representation class must:
checkArgument(DerivedString.class.isAssignableFrom(type), "%s is not a DerivedString", type);
checkFinalMethod(type, "support");
checkFinalMethod(type, "hashCode");
checkFinalMethod(type, "equals", Object.class);
- checkFinalMethod(type, "compareTo", type);
} catch (SecurityException e) {
LOG.warn("Cannot completely validate {}", type, e);
return Boolean.FALSE;
return Boolean.TRUE;
}
+ abstract void checkCompareTo(Class<?> type);
+
private static void checkFinalMethod(final Class<?> type, final String name) {
try {
checkFinalMethod(type.getMethod(name).getModifiers(), type, name, "");
}
}
- private static void checkFinalMethod(final Class<?> type, final String name, final Class<?> arg) {
+ static void checkFinalMethod(final Class<?> type, final String name, final Class<?> arg) {
final String argName = arg.getSimpleName();
try {
checkFinalMethod(type.getMethod(name, arg).getModifiers(), type, name, argName);
}
}
- private static final ClassValue<Boolean> VALIDATED_REPRESENTATIONS = new Validator();
+ private static final ClassValue<Boolean> VALIDATED_REPRESENTATIONS = new AbstractValidator() {
+ @Override
+ void checkCompareTo(@NonNull final Class<?> type) {
+ checkFinalMethod(type, "compareTo", type);
+ }
+ };
+ private static final ClassValue<Boolean> VALIDATED_VALIDATIONS = new AbstractValidator() {
+ @Override
+ void checkCompareTo(@NonNull final Class<?> type) {
+ // Intentional no-op, as we'd need a type capture of the representation
+ }
+ };
private static final long serialVersionUID = 1L;
/**
VALIDATED_REPRESENTATIONS.get(representationClass);
return representationClass;
}
+
+ static <T extends DerivedString<?>> Class<T> validateValidationClass(final Class<T> representationClass) {
+ // Validation is reflective, cache its result
+ VALIDATED_VALIDATIONS.get(representationClass);
+ return representationClass;
+ }
}