osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] brooklyn-server pull request #999: Constraints - serialization and {forbidde...


Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/999#discussion_r219567021
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/config/ConfigConstraints.java ---
    @@ -218,4 +228,88 @@ public LocationConfigConstraints(Location brooklynObject) {
             }
         }
     
    +    public static <T> Predicate<T> required() {
    +        return new RequiredPredicate<T>();
    +    }
    +    
    +    /** Predicate indicating a field is required:  it must not be null and if a string it must not be empty */
    +    public static class RequiredPredicate<T> implements Predicate<T> {
    +        @Override
    +        public boolean apply(T input) {
    +            if (input==null) return false;
    +            if (input instanceof CharSequence && ((CharSequence)input).length()==0) return false;
    +            return true;
    +        }
    +        @Override
    +        public String toString() {
    +            return "required()";
    +        }
    +    }
    +    
    +    private static abstract class OtherKeyPredicate implements BrooklynObjectPredicate<Object> {
    +        private String otherKeyName;
    +
    +        public OtherKeyPredicate(String otherKeyName) {
    +            this.otherKeyName = otherKeyName;
    +        }
    +
    +        public abstract String predicateName();
    +        
    +        @Override
    +        public String toString() {
    +            return predicateName()+"("+JavaStringEscapes.wrapJavaString(otherKeyName)+")";
    +        }
    +        
    +        @Override
    +        public boolean apply(Object input) {
    +            return apply(input, BrooklynTaskTags.getContextEntity(Tasks.current()));
    +        }
    +
    +        @Override
    +        public boolean apply(Object input, BrooklynObject context) {
    +            if (context==null) return true;
    +            // would be nice to offer an explanation, but that will need a richer API or a thread local
    +            return test(input, context.config().get(ConfigKeys.newConfigKey(Object.class, otherKeyName)));
    +        }
    +        
    +        public abstract boolean test(Object thisValue, Object otherValue);
    +        
    +    }
    +    
    +    public static Predicate<Object> forbiddenIf(String otherKeyName) { return new ForbiddenIfPredicate(otherKeyName); }
    +    public static class ForbiddenIfPredicate extends OtherKeyPredicate {
    +        public ForbiddenIfPredicate(String otherKeyName) { super(otherKeyName); }
    +        @Override public String predicateName() { return "forbiddenIf"; }
    +        @Override public boolean test(Object thisValue, Object otherValue) { 
    +            return (thisValue==null) || (otherValue==null);
    +        } 
    +    }
    +    
    +    public static Predicate<Object> forbiddenUnless(String otherKeyName) { return new ForbiddenUnlessPredicate(otherKeyName); }
    +    public static class ForbiddenUnlessPredicate extends OtherKeyPredicate {
    --- End diff --
    
    Make the classes protected at the very most. Otherwise someone will call the class' constructor directly, and we'll be stuck with it in our public API for ages to come.


---