diff --git a/src/main/java/io/ljunggren/sanitizer/Sanitizer.java b/src/main/java/io/ljunggren/sanitizer/Sanitizer.java index d942774..40c4f23 100644 --- a/src/main/java/io/ljunggren/sanitizer/Sanitizer.java +++ b/src/main/java/io/ljunggren/sanitizer/Sanitizer.java @@ -9,16 +9,7 @@ import org.apache.commons.lang3.reflect.FieldUtils; import io.ljunggren.reflectionUtils.ReflectionUtils; -import io.ljunggren.sanitizer.sanitation.AccentSanitation; -import io.ljunggren.sanitizer.sanitation.CatchAllSanitation; -import io.ljunggren.sanitizer.sanitation.ConcurrentSpaceSanitation; -import io.ljunggren.sanitizer.sanitation.CustomSanitation; -import io.ljunggren.sanitizer.sanitation.LowercaseSanitation; -import io.ljunggren.sanitizer.sanitation.RemoveSanitation; -import io.ljunggren.sanitizer.sanitation.SanitationChain; -import io.ljunggren.sanitizer.sanitation.TrimSanitation; -import io.ljunggren.sanitizer.sanitation.TrimToNullSanitation; -import io.ljunggren.sanitizer.sanitation.UppercaseSanitation; +import io.ljunggren.sanitizer.sanitation.SanitationRegistry; import lombok.Getter; /** @@ -129,28 +120,15 @@ } private void sanitizeItems(List items) { - SanitationChain chain = buildChain(); - items.forEach(item -> sanitizeItem(item, chain)); + SanitationRegistry registry = SanitationRegistry.defaultRegistry(); + items.forEach(item -> sanitizeItem(item, registry)); } - private void sanitizeItem(Item item, SanitationChain chain) { + private void sanitizeItem(Item item, SanitationRegistry registry) { Annotation[] annotations = item.getField().getAnnotations(); for (Annotation annotation : annotations) { - chain.sanitize(annotation, item); + registry.sanitize(annotation, item); } } - private SanitationChain buildChain() { - return new ConcurrentSpaceSanitation() - .nextChain(new LowercaseSanitation() - .nextChain(new RemoveSanitation() - .nextChain(new TrimSanitation() - .nextChain(new TrimToNullSanitation() - .nextChain(new UppercaseSanitation() - .nextChain(new CustomSanitation() - .nextChain(new AccentSanitation() - .nextChain(new CatchAllSanitation() - )))))))); - } - } diff --git a/src/main/java/io/ljunggren/sanitizer/sanitation/SanitationRegistry.java b/src/main/java/io/ljunggren/sanitizer/sanitation/SanitationRegistry.java new file mode 100755 index 0000000..91f9c7c --- /dev/null +++ b/src/main/java/io/ljunggren/sanitizer/sanitation/SanitationRegistry.java @@ -0,0 +1,97 @@ +package io.ljunggren.sanitizer.sanitation; + +import java.lang.annotation.Annotation; +import java.util.HashMap; +import java.util.Map; + +import io.ljunggren.sanitizer.Item; +import io.ljunggren.sanitizer.annotation.Accent; +import io.ljunggren.sanitizer.annotation.ConcurrentSpace; +import io.ljunggren.sanitizer.annotation.CustomSanitizer; +import io.ljunggren.sanitizer.annotation.Lowercase; +import io.ljunggren.sanitizer.annotation.Remove; +import io.ljunggren.sanitizer.annotation.Trim; +import io.ljunggren.sanitizer.annotation.TrimToNull; +import io.ljunggren.sanitizer.annotation.Uppercase; + +/** + * Registry that maps annotation types to their {@link SanitationChain} + * implementations. + * + *

Replaces the previous fluent chain-of-responsibility wiring with a + * direct lookup. Adding a new sanitation type now only requires registering + * the annotation/handler pair — no changes to dispatch logic are needed. + * Annotations that have no registered handler are routed to a fallback + * (defaulting to {@link CatchAllSanitation}).

+ */ +public class SanitationRegistry { + + private final Map, SanitationChain> handlers = + new HashMap<>(); + private final SanitationChain fallback; + + /** + * Create an empty registry that uses {@link CatchAllSanitation} as its + * fallback handler. + */ + public SanitationRegistry() { + this(new CatchAllSanitation()); + } + + /** + * Create an empty registry with a caller-supplied fallback handler. + * + * @param fallback handler invoked when no registered handler matches + */ + public SanitationRegistry(SanitationChain fallback) { + this.fallback = fallback; + } + + /** + * Associate the given annotation type with the given handler. Subsequent + * registrations for the same annotation type overwrite the previous one. + * + * @param annotationType the annotation class to dispatch on + * @param handler the handler responsible for that annotation + * @return this registry, for fluent chaining + */ + public SanitationRegistry register( + Class annotationType, + SanitationChain handler) { + handlers.put(annotationType, handler); + return this; + } + + /** + * Dispatch the given annotation to its registered handler, or to the + * fallback handler when no registration matches. + * + * @param annotation annotation found on the field + * @param item the item being sanitized + */ + public void sanitize(Annotation annotation, Item item) { + SanitationChain handler = handlers.get(annotation.annotationType()); + if (handler != null) { + handler.sanitize(annotation, item); + return; + } + fallback.sanitize(annotation, item); + } + + /** + * @return a registry pre-populated with handlers for every built-in + * sanitation annotation + */ + public static SanitationRegistry defaultRegistry() { + return new SanitationRegistry() + .register(ConcurrentSpace.class, new ConcurrentSpaceSanitation()) + .register(Lowercase.class, new LowercaseSanitation()) + .register(Remove.class, new RemoveSanitation()) + .register(Trim.class, new TrimSanitation()) + .register(TrimToNull.class, new TrimToNullSanitation()) + .register(Uppercase.class, new UppercaseSanitation()) + .register(CustomSanitizer.class, new CustomSanitation()) + .register(Accent.class, new AccentSanitation()); + } + +}