{"action":"created","issue":{"url":"https://api.github.com/repos/immutables/immutables/issues/512","repository_url":"https://api.github.com/repos/immutables/immutables","labels_url":"https://api.github.com/repos/immutables/immutables/issues/512/labels{/name}","comments_url":"https://api.github.com/repos/immutables/immutables/issues/512/comments","events_url":"https://api.github.com/repos/immutables/immutables/issues/512/events","html_url":"https://github.com/immutables/immutables/issues/512","id":197519688,"number":512,"title":"Builder as Implementation","user":{"login":"xenoterracide","id":5517,"avatar_url":"https://avatars.githubusercontent.com/u/5517?v=3","gravatar_id":"","url":"https://api.github.com/users/xenoterracide","html_url":"https://github.com/xenoterracide","followers_url":"https://api.github.com/users/xenoterracide/followers","following_url":"https://api.github.com/users/xenoterracide/following{/other_user}","gists_url":"https://api.github.com/users/xenoterracide/gists{/gist_id}","starred_url":"https://api.github.com/users/xenoterracide/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xenoterracide/subscriptions","organizations_url":"https://api.github.com/users/xenoterracide/orgs","repos_url":"https://api.github.com/users/xenoterracide/repos","events_url":"https://api.github.com/users/xenoterracide/events{/privacy}","received_events_url":"https://api.github.com/users/xenoterracide/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-12-26T00:05:55Z","updated_at":"2016-12-26T07:02:38Z","closed_at":null,"body":"Maybe this is possible, but from the docs I'm having trouble trying to see how. My basic goal is to have the `Builder` implement a `@FunctionalInterface`, and that the implementation of that will call build, hopefully my test/implementation below makes it obvious what I'm trying to do.\r\n\r\n```\r\npublic class Try {\r\n\r\n\r\n    static <T> PredicateBuilder<T> pred( final PredicateThrower<T> onSuccess ) {\r\n        Objects.requireNonNull( onSuccess );\r\n        return new PredicateBuilder<>( onSuccess );\r\n    }\r\n\r\n\r\n    @Builder.Factory\r\n    static <T> Predicate<T> tryPredicate(\r\n        final PredicateThrower<T> onSuccess,\r\n        final Optional<Predicate<T>> onCatch,\r\n        final Optional<Boolean> orElse\r\n    ) {\r\n        Objects.requireNonNull( onSuccess );\r\n        return t -> {\r\n            try {\r\n                return onSuccess.test( t );\r\n            }\r\n            catch ( final Throwable e ) {\r\n                return onCatch.orElse( ( t1 ) -> orElse.orElse( false ) ).test( t );\r\n            }\r\n        };\r\n    }\r\n\r\n    @FunctionalInterface\r\n    public interface PredicateThrower<I> {\r\n        boolean test( I input ) throws Throwable;\r\n    }\r\n\r\n    @FunctionalInterface\r\n    public interface FunctionThrower<I, R> {\r\n        R tryIt( I input ) throws Throwable;\r\n    }\r\n\r\n    public static class PredicateBuilder<T> implements Predicate<T> {\r\n        private final com.xenoterracide.util.TryPredicateBuilder<T> builder;\r\n\r\n        PredicateBuilder( final PredicateThrower<T> onSuccess ) {\r\n            this.builder = new com.xenoterracide.util.TryPredicateBuilder<T>().onSuccess( onSuccess );\r\n        }\r\n\r\n        public PredicateBuilder<T> onSuccess( final PredicateThrower<T> onSuccess ) {\r\n            builder.onSuccess( onSuccess );\r\n            return this;\r\n        }\r\n\r\n        public PredicateBuilder<T> onCatch( final Predicate<T> onCatch ) {\r\n            builder.onCatch( onCatch );\r\n            return this;\r\n        }\r\n\r\n        public PredicateBuilder<T> orElse( final boolean orElse ) {\r\n            builder.orElse( orElse );\r\n            return this;\r\n        }\r\n\r\n        @Override\r\n        public boolean test( final T t ) {\r\n            return builder.build().test( t );\r\n        }\r\n\r\n    }\r\n\r\n}\r\n```\r\n\r\nhere's my test\r\n\r\n```\r\npackage com.xenoterracide.util;\r\n\r\n\r\nimport java.util.stream.Stream;\r\n\r\nimport org.junit.Test;\r\n\r\nimport static org.assertj.core.api.Assertions.assertThat;\r\n\r\npublic class TryTest {\r\n\r\n    private boolean fromStringThrows( final String string ) {\r\n        throw new RuntimeException();\r\n    }\r\n\r\n    private boolean fromString( final String string ) {\r\n        return Boolean.valueOf( string );\r\n    }\r\n\r\n\r\n    @Test\r\n    public void predicateJustOnSuccess() {\r\n        assertThat( Try.pred( this::fromString ).test( \"true\" ) ).isTrue();\r\n    }\r\n\r\n    @Test\r\n    public void predicateOnCatchDefault() {\r\n        assertThat( Try.pred( this::fromStringThrows ).test( \"true\" ) ).isFalse();\r\n    }\r\n\r\n    @Test\r\n    public void predicateOnCatch() {\r\n        assertThat( Try.pred( this::fromStringThrows ).onCatch( String::isEmpty ).test( \"true\" ) ).isFalse();\r\n    }\r\n\r\n    @Test\r\n    public void predicateStreamFilter() {\r\n        assertThat( Stream.of( \"true\" ).filter( Try.pred( this::fromString ) ) ).containsExactly( \"true\" );\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/immutables/immutables/issues/comments/269173627","html_url":"https://github.com/immutables/immutables/issues/512#issuecomment-269173627","issue_url":"https://api.github.com/repos/immutables/immutables/issues/512","id":269173627,"user":{"login":"xenoterracide","id":5517,"avatar_url":"https://avatars.githubusercontent.com/u/5517?v=3","gravatar_id":"","url":"https://api.github.com/users/xenoterracide","html_url":"https://github.com/xenoterracide","followers_url":"https://api.github.com/users/xenoterracide/followers","following_url":"https://api.github.com/users/xenoterracide/following{/other_user}","gists_url":"https://api.github.com/users/xenoterracide/gists{/gist_id}","starred_url":"https://api.github.com/users/xenoterracide/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xenoterracide/subscriptions","organizations_url":"https://api.github.com/users/xenoterracide/orgs","repos_url":"https://api.github.com/users/xenoterracide/repos","events_url":"https://api.github.com/users/xenoterracide/events{/privacy}","received_events_url":"https://api.github.com/users/xenoterracide/received_events","type":"User","site_admin":false},"created_at":"2016-12-26T07:02:38Z","updated_at":"2016-12-26T07:02:38Z","body":"you know, it's a lot simpler if I take Immutables out of the equation\r\n\r\n```\r\npackage com.xenoterracide.util;\r\n\r\n\r\nimport java.util.Objects;\r\nimport java.util.Optional;\r\nimport java.util.function.Predicate;\r\n\r\npublic class Try {\r\n\r\n\r\n    static <T> PredicateBuilder<T> pred( final PredicateThrower<T> onSuccess ) {\r\n        Objects.requireNonNull( onSuccess );\r\n        return new PredicateBuilder<>( onSuccess );\r\n    }\r\n\r\n    @FunctionalInterface\r\n    public interface PredicateThrower<I> {\r\n        boolean test( I input ) throws Throwable;\r\n    }\r\n\r\n    @FunctionalInterface\r\n    public interface FunctionThrower<I, R> {\r\n        R tryIt( I input ) throws Throwable;\r\n    }\r\n\r\n    public static class PredicateBuilder<T> implements Predicate<T> {\r\n        private final PredicateThrower<T> onSuccess;\r\n        private Optional<Predicate<T>> onCatch = Optional.empty();\r\n        private Optional<Boolean> orElse = Optional.empty();\r\n\r\n        public PredicateBuilder( final PredicateThrower<T> onSuccess ) {\r\n            this.onSuccess = Objects.requireNonNull( onSuccess );\r\n        }\r\n\r\n\r\n        public PredicateBuilder<T> onCatch( final Predicate<T> onCatch ) {\r\n            this.onCatch = Optional.of( onCatch );\r\n            return this;\r\n        }\r\n\r\n        public PredicateBuilder<T> orElse( final boolean orElse ) {\r\n            this.orElse = Optional.of( orElse );\r\n            return this;\r\n        }\r\n\r\n        @Override\r\n        public boolean test( final T t ) {\r\n            try {\r\n                return onSuccess.test( t );\r\n            }\r\n            catch ( final Throwable e ) {\r\n                return onCatch.orElse( ( t1 ) -> orElse.orElse( false ) ).test( t );\r\n            }\r\n        }\r\n    }\r\n\r\n}```"}}
{"actor":{"display_login":"xenoterracide"}}