Validator method before executing method

I am working wiht Handlebars and i want to implement validation process before executing Handlebar Helper method apply().

I have an interface that extends Helper interface from Handlebar that has validate() mehtod

public interface HandlebarHelperProcessor<C, I> extends Helper<C> {
    default void validate(Object context, Options options) {
      //some validation logic
    }
}

public interface Helper<T> {
    Object apply(T var1, Options var2) throws IOException;
}

For example i have this EndsWith Helper that basically checks if string ends with desired suffix

@Slf4j
@Service
@RequiredArgsConstructor
public class EndsWith implements HandlebarHelperProcessor<Object, String>{
    @SneakyThrows
    @Override
    public Object apply(Object context, Options options) {
        String str = (String) context;
        String suffix = (String) options.params[0];

        return StringUtils.endsWith(str, suffix);
    }
}

I want to execute this validate() method right at the start of apply method.
Easy way is to just call this validate method explicitly in method apply, but since i have many helpers like this, it would not be very efficient to write that one line of code in each of them and if i want to create new helper i would have to call this method in that helper also. I am wondering if I can do that with Wrapper or Proxy pattern.

I know that this can be implemented usign AOP, but i don`t need that

Leave a Comment