Untitled

 avatar
unknown
java
3 years ago
1.7 kB
6
No Index
class Test {
    public static final String INVALID_STRING = new String();
    public static final String PERFECT_STRING = new String();
    
    static boolean test(Result s) {
        if (s.status() == ResultStatus.uninteresting) return false;
        if (s.status() == ResultStatus.perfect) return true;
        /* expensive calculation */
        return s.text().length() > 5;
    }
    
    public static Test instance = new Test();
}

class OtherClass {
    static <E> Function<E, Boolean> bundle(Test t, Function<E, Result> str) {
        return (a) -> Test.test(str.apply(a));
    }
}

class ThirdClass {
    static void main() {
        
        OtherClass.<Double>bundle(Test.instance, d -> {
            // We know that doubles less than 5 are not interesting,
            // but we cannot be certain that their String isn't maybe considered interesting anyways
            // and we do not even once try to calculate their interestingness if we already know it
            if (d < 5.0) {
                return Result.uninteresting();
            }
            return Result.interesting(Double.toString(d));
        });
    }
}

enum ResultStatus {
    uninteresting,
    interesting,
    perfect,
}

record Result(
    ResultStatus status,
    String text
) {
    public static Result uninteresting() {
        return new Result(ResultStatus.uninteresting, "");
    }
    
    public static Result interesting(String value) {
        return new Result(ResultStatus.perfect, value);
    }
    
    public static Result perfect() {
        return new Result(ResultStatus.perfect, "");
    }
}
Editor is loading...