Validator Methods erwarten nun einen Parameter

Die Methoden in `Validator` erwarten nun als zweiten Parameter den Namen des Parameters, der geprüft wird. Dadurch sollen die Methoden hilfreicher werden, indem geneuere Fehlermeldungen generiert werden können.
This commit is contained in:
ngb
2022-12-11 13:33:19 +01:00
parent 1275af55f3
commit eaaca6b90f
10 changed files with 45 additions and 54 deletions

View File

@@ -156,7 +156,7 @@ public class Zeichenfenster extends JFrame {
* @param displayDevice Das Anzeigegerät für das Fenster. * @param displayDevice Das Anzeigegerät für das Fenster.
*/ */
public Zeichenfenster( Zeichenleinwand canvas, String title, GraphicsDevice displayDevice ) { public Zeichenfenster( Zeichenleinwand canvas, String title, GraphicsDevice displayDevice ) {
super(Validator.requireNotNull(displayDevice).getDefaultConfiguration()); super(Validator.requireNotNull(displayDevice, "displayDevice").getDefaultConfiguration());
this.displayDevice = displayDevice; this.displayDevice = displayDevice;
Validator.requireNotNull(canvas, "Every Zeichenfenster needs a Zeichenleinwand, but got <null>."); Validator.requireNotNull(canvas, "Every Zeichenfenster needs a Zeichenleinwand, but got <null>.");

View File

@@ -24,7 +24,7 @@ public abstract class Animation<T> extends Constants implements Updatable {
public Animation( DoubleUnaryOperator easing ) { public Animation( DoubleUnaryOperator easing ) {
this.runtime = Constants.DEFAULT_ANIM_RUNTIME; this.runtime = Constants.DEFAULT_ANIM_RUNTIME;
this.easing = Validator.requireNotNull(easing); this.easing = Validator.requireNotNull(easing, "easing");
} }
public Animation( int runtime ) { public Animation( int runtime ) {
@@ -34,7 +34,7 @@ public abstract class Animation<T> extends Constants implements Updatable {
public Animation( int runtime, DoubleUnaryOperator easing ) { public Animation( int runtime, DoubleUnaryOperator easing ) {
this.runtime = runtime; this.runtime = runtime;
this.easing = Validator.requireNotNull(easing); this.easing = Validator.requireNotNull(easing, "easing");
} }
public int getRuntime() { public int getRuntime() {

View File

@@ -10,7 +10,7 @@ public class AnimationFacade<S> extends Animation<S> {
public AnimationFacade( Animation<S> anim, int runtime, DoubleUnaryOperator easing ) { public AnimationFacade( Animation<S> anim, int runtime, DoubleUnaryOperator easing ) {
super(runtime, easing); super(runtime, easing);
this.anim = Validator.requireNotNull(anim); this.anim = Validator.requireNotNull(anim, "anim");
} }
@Override @Override

View File

@@ -123,8 +123,8 @@ public class Animations {
} }
public static final <T> Future<T> animateProperty( T target, final double from, final double to, int runtime, DoubleUnaryOperator easing, DoubleConsumer propSetter ) { public static final <T> Future<T> animateProperty( T target, final double from, final double to, int runtime, DoubleUnaryOperator easing, DoubleConsumer propSetter ) {
Validator.requireNotNull(target); Validator.requireNotNull(target, "target");
Validator.requireNotNull(propSetter); Validator.requireNotNull(propSetter, "propSetter");
return play(target, runtime, easing, ( e ) -> propSetter.accept(Constants.interpolate(from, to, e))); return play(target, runtime, easing, ( e ) -> propSetter.accept(Constants.interpolate(from, to, e)));
} }

View File

@@ -84,7 +84,7 @@ public class Music implements Audio {
* @see ResourceStreamProvider#getResourceURL(String) * @see ResourceStreamProvider#getResourceURL(String)
*/ */
public Music( String audioSource ) { public Music( String audioSource ) {
Validator.requireNotNull(audioSource); Validator.requireNotNull(audioSource, "audioSource");
this.audioSource = audioSource; this.audioSource = audioSource;
} }

View File

@@ -76,7 +76,7 @@ public class Sound implements Audio {
* @see ResourceStreamProvider#getResourceURL(String) * @see ResourceStreamProvider#getResourceURL(String)
*/ */
public Sound( String source ) { public Sound( String source ) {
Validator.requireNotNull(source); Validator.requireNotNull(source, "source");
this.audioSource = source; this.audioSource = source;
} }

View File

@@ -77,7 +77,7 @@ public final class Log {
* mindestens herabgesenkt werden sollen. * mindestens herabgesenkt werden sollen.
*/ */
public static void enableGlobalLevel( Level level ) { public static void enableGlobalLevel( Level level ) {
int lvl = Validator.requireNotNull(level).intValue(); int lvl = Validator.requireNotNull(level, "level").intValue();
ensureRootLoggerInitialized(); ensureRootLoggerInitialized();
// Decrease level of root level ConsoleHandlers for output // Decrease level of root level ConsoleHandlers for output

View File

@@ -6,35 +6,55 @@ import java.util.function.Supplier;
/** /**
* Statische Methoden, um Methodenparameter auf Gültigkeit zu prüfen. * Statische Methoden, um Methodenparameter auf Gültigkeit zu prüfen.
*/ */
@SuppressWarnings( "UnusedReturnValue" ) @SuppressWarnings( {"unused", "UnusedReturnValue"} )
public class Validator { public class Validator {
public static final <T> T requireNotNull( T obj ) { public static final <T> T requireNotNull( T obj, CharSequence paramName ) {
return Objects.requireNonNull(obj); return requireNotNull(obj, () -> "Parameter <%s> may not be null.".format(paramName.toString()));
} }
public static final <T> T requireNotNull( T obj, CharSequence msg ) { public static final <T> T requireNotNull( T obj, CharSequence paramName, CharSequence msg ) {
return Objects.requireNonNull(obj, msg::toString); return requireNotNull(obj, () -> msg.toString().format(paramName.toString()));
} }
public static final <T> T requireNotNull( T obj, Supplier<String> msg ) { public static final <T> T requireNotNull( T obj, Supplier<String> msg ) {
return Objects.requireNonNull(obj, msg); if( obj == null ) {
throw new NullPointerException(msg == null ? "Parameter may nut be null." : msg.get());
}
return obj;
} }
public static final String requireNotEmpty( String str ) { public static final String requireNotEmpty( String str, CharSequence paramName ) {
return requireNotEmpty(str, (Supplier<String>) null); return requireNotEmpty(str, () -> String.format("Parameter <%s> may not be empty string (<%s> provided)", paramName, str));
} }
public static final String requireNotEmpty( String str, CharSequence msg ) { public static final String requireNotEmpty( String str, CharSequence paramName, CharSequence msg ) {
return requireNotEmpty(str, msg::toString); return requireNotEmpty(str, () -> msg.toString().format(paramName.toString()));
} }
public static final String requireNotEmpty( String str, Supplier<String> msg ) { public static final String requireNotEmpty( String str, Supplier<String> msg ) {
if( str.isEmpty() ) if( str.isEmpty() ) {
throw new IllegalArgumentException(msg == null ? String.format("Parameter may not be empty string (<%s> provided)", str) : msg.get()); throw new IllegalArgumentException(msg == null ? String.format("Parameter may not be empty string (<%s> provided)", str) : msg.get());
}
return str; return str;
} }
public static final <T> T[] requireNotEmpty( T[] arr, CharSequence paramName ) {
return requireNotEmpty(arr, () -> String.format("Parameter <%s> may not be empty", paramName));
}
public static final <T> T[] requireNotEmpty( T[] arr, CharSequence paramName, CharSequence msg ) {
return requireNotEmpty(arr, () -> msg.toString().format(paramName.toString()));
}
public static final <T> T[] requireNotEmpty( T[] arr, Supplier<String> msg ) {
if( arr.length == 0 )
throw new IllegalArgumentException(msg == null ? "Parameter array may not be empty" : msg.get());
return arr;
}
public static final int requirePositive( int i ) { public static final int requirePositive( int i ) {
return requirePositive(i, (Supplier<String>) null); return requirePositive(i, (Supplier<String>) null);
} }
@@ -139,21 +159,6 @@ public class Validator {
} }
*/ */
public static final <T> T[] requireNotEmpty( T[] arr ) {
return requireNotEmpty(arr, (Supplier<String>) null);
}
public static final <T> T[] requireNotEmpty( T[] arr, CharSequence msg ) {
return requireNotEmpty(arr, msg::toString);
}
public static final <T> T[] requireNotEmpty( T[] arr, Supplier<String> msg ) {
if( arr.length == 0 )
throw new IllegalArgumentException(msg == null ? "Parameter array may not be empty" : msg.get());
return arr;
}
public static final <T> T[] requireSize( T[] arr, int size ) { public static final <T> T[] requireSize( T[] arr, int size ) {
return requireSize(arr, size, (Supplier<String>) null); return requireSize(arr, size, (Supplier<String>) null);
} }
@@ -168,20 +173,6 @@ public class Validator {
return arr; return arr;
} }
public static final <T> T[] requireValid( T[] arr ) {
return requireValid(arr, (Supplier<String>) null);
}
public static final <T> T[] requireValid( T[] arr, CharSequence msg ) {
return requireValid(arr, msg::toString);
}
public static final <T> T[] requireValid( T[] arr, Supplier<String> msg ) {
if( arr == null || arr.length > 0 )
throw new IllegalArgumentException(msg == null ? "Parameter array may not be null or empty" : msg.get());
return arr;
}
private Validator() { private Validator() {
} }

View File

@@ -76,8 +76,8 @@ public class EventDispatcher<E, L extends Listener<E>> {
} }
public void registerEventType( String eventKey, BiConsumer<E, L> dispatcher ) { public void registerEventType( String eventKey, BiConsumer<E, L> dispatcher ) {
Validator.requireNotNull(eventKey); Validator.requireNotNull(eventKey, "eventKey");
Validator.requireNotNull(dispatcher); Validator.requireNotNull(dispatcher, "dispatcher");
if( !eventRegistered(eventKey) ) { if( !eventRegistered(eventKey) ) {
eventRegistry.put(eventKey, dispatcher); eventRegistry.put(eventKey, dispatcher);
@@ -102,8 +102,8 @@ public class EventDispatcher<E, L extends Listener<E>> {
} }
public void dispatchEvent( String eventKey, final E event ) { public void dispatchEvent( String eventKey, final E event ) {
Validator.requireNotNull(eventKey); Validator.requireNotNull(eventKey, "eventKey");
Validator.requireNotNull(event); Validator.requireNotNull(event, "event");
if( eventRegistered(eventKey) ) { if( eventRegistered(eventKey) ) {
final BiConsumer<E, L> dispatcher = eventRegistry.get(eventKey); final BiConsumer<E, L> dispatcher = eventRegistry.get(eventKey);

View File

@@ -53,7 +53,7 @@ public class FontLoader {
} }
public static Font loadFont( String name, String source ) { public static Font loadFont( String name, String source ) {
Validator.requireNotNull(source,"Font source may not be null"); Validator.requireNotNull(source, "Font source may not be null");
Validator.requireNotEmpty(source, "Font source may not be empty."); Validator.requireNotEmpty(source, "Font source may not be empty.");
if( fontCache.containsKey(name) ) { if( fontCache.containsKey(name) ) {