DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Functions<T, Optional<T>> from Predicate<? super T> and Optional.orElseThrow(Supplier<? extends Throwable>) #1449

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Three or four months back, I recall a few users of the library submitting 
issues concerning throwing exceptions when an object does not meet a condition.

Would an alternative method to Functions.forPredicate and an addition method to 
Optional not suffice the issue?

----

static enum Functions {
   ;

   public static <T> Function<T, Optional<T>> guard(
         final Predicate<? super T> p) {

      // more likely defined as a static class member
      return new Function<>() {
         public Optional<T> apply(T t) {
            return p.apply(t) ? of(t) : absent();
         }
      };
   }

}

static class Optional<T> {
   public abstract <X extends Throwable> T
   orElseThrow(Supplier<? extends X> factory) throws X;
}

final Class<T> clas;
final Object o;
...
T t = guard(isInstanceOf(clas)).apply(o).transform(cast())
      .orElseThrow(Exceptions.Suppliers.classCast("%s not of %s", o, clas));

Having a higher-order function that returns an Optional allows for much more 
aside from merely throwing an exception when the predicate tests false.

Despite more flexibility, the above example was fairly function-oriented; here 
is the example without guard():

T t;
if (!clas.isInstance(clas))
   throw Exceptions.classCast(format("%s not of %s", o, clas));
t = (T)o;

Original issue reported on code.google.com by jamie.sp...@gmail.com on 17 Jun 2013 at 12:30

GoogleCodeExporter commented 9 years ago
Why wouldn't you just write

T t = clas.cast(o);

...since the Class.cast method would throw the ClassCastException for you?

Original comment by wasserman.louis on 17 Jun 2013 at 12:55

GoogleCodeExporter commented 9 years ago
Normally, you would, but for the sake of showing some example, I would write it 
the other way.

Original comment by jamie.sp...@gmail.com on 17 Jun 2013 at 1:08

GoogleCodeExporter commented 9 years ago
Generally:

T t = guard(predicate).apply(o).transform(function)
      .orElseThrow(throwableSupplier);

vs

T t;
if (!predicate.apply(o))
   throw x;
t = function.apply(o);

Original comment by jamie.sp...@gmail.com on 17 Jun 2013 at 1:12

GoogleCodeExporter commented 9 years ago
This seems like a duplicate of issue 856 in many ways...

As usual, I'm leery of adding lots more Function and Predicate sugar.  In all 
honesty, I find the second example you give more readable than the first -- 
except I wouldn't bother with the Predicate and the Function objects, I'd just 
make them normal method invocations:

if (!good(o)) {
  throw new FooException();
}
T t = bar(o);

Original comment by lowas...@google.com on 17 Jun 2013 at 4:59

GoogleCodeExporter commented 9 years ago
Honestly, I'm more concerned with the fact that I can do a lot more with an 
Optional, in general.  I'd just like to have a method for throwing an exception 
given that there are methods for providing default values from instances and 
Suppliers, and even passing other Optional.  I don't care as much for guava 
providing Functions.guard(); it's just the means in which to provide a test; 
just so long as the result yields an Optional.

I'd feel as if Optional.or(Supplier<? extends T> s) does just as much as 
Optional.orElseThrow(Supplier<? extends X> x) throws X would.

imperative:

return optional.isPresent() ? optional.get() : s.get();

if (!optional.isPresent())
   throw x.get();
return optional.get();

functional:

optional.or(s);
optional.orElseThrow(x);

I feel as if I'd use it a lot, as I like using Suppliers when I can, and I like 
having objects around more so than static utilities.

Original comment by jamie.sp...@gmail.com on 17 Jun 2013 at 7:02

GoogleCodeExporter commented 9 years ago
Optional.orElseThrow was rejected in issue #1121.

On the other hand it's part of 
[http://download.java.net/jdk8/docs/api/java/util/Optional.html#orElseThrow(java
.util.function.Supplier) Java 8's Optional].

Original comment by xaerx...@gmail.com on 6 Sep 2013 at 10:15

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08