Java Optionals - how to write in functional style? [duplicate] - java

I want to replace the following code using java8 Optional:
public Obj getObjectFromDB() {
Obj obj = dao.find();
if (obj != null) {
obj.setAvailable(true);
} else {
logger.fatal("Object not available");
}
return obj;
}
The following pseudocode does not work as there is no orElseRun method, but anyways it illustrates my purpose:
public Optional<Obj> getObjectFromDB() {
Optional<Obj> obj = dao.find();
return obj.ifPresent(obj.setAvailable(true)).orElseRun(logger.fatal("Object not available"));
}

With Java 9 or higher, ifPresentOrElse is most likely what you want:
Optional<> opt = dao.find();
opt.ifPresentOrElse(obj -> obj.setAvailable(true),
() -> logger.error("…"));
Currying using vavr or alike might get even neater code, but I haven't tried yet.

I don't think you can do it in a single statement. Better do:
if (!obj.isPresent()) {
logger.fatal(...);
} else {
obj.get().setAvailable(true);
}
return obj;

For Java 8 Spring Data offers ifPresentOrElse from "Utility methods to work with Optionals" to achieve what you want.
Example would be:
import static org.springframework.data.util.Optionals.ifPresentOrElse;
ifPresentOrElse(dao.find(), obj -> obj.setAvailable(true), () -> logger.fatal("Object not available"));

You will have to split this into multiple statements. Here is one way to do that:
if (!obj.isPresent()) {
logger.fatal("Object not available");
}
obj.ifPresent(o -> o.setAvailable(true));
return obj;
Another way (possibly over-engineered) is to use map:
if (!obj.isPresent()) {
logger.fatal("Object not available");
}
return obj.map(o -> {o.setAvailable(true); return o;});
If obj.setAvailable conveniently returns obj, then you can simply the second example to:
if (!obj.isPresent()) {
logger.fatal("Object not available");
}
return obj.map(o -> o.setAvailable(true));

There is an .orElseRun method, but it is called .orElseGet.
The main problem with your pseudocode is that .isPresent doesn't return an Optional<>. But .map returns an Optional<> which has the orElseGet method.
If you really want to do this in one statement this is possible:
public Optional<Obj> getObjectFromDB() {
return dao.find()
.map( obj -> {
obj.setAvailable(true);
return Optional.of(obj);
})
.orElseGet( () -> {
logger.fatal("Object not available");
return Optional.empty();
});
}
But this is even clunkier than what you had before.

First of all, your dao.find() should either return an Optional<Obj> or you will have to create one.
e.g.
Optional<Obj> = dao.find();
or you can do it yourself like:
Optional<Obj> = Optional.ofNullable(dao.find());
this one will return Optional<Obj> if present or Optional.empty() if not present.
So now let's get to the solution,
public Obj getObjectFromDB() {
return Optional.ofNullable(dao.find()).flatMap(ob -> {
ob.setAvailable(true);
return Optional.of(ob);
}).orElseGet(() -> {
logger.fatal("Object not available");
return null;
});
}
This is the one liner you're looking for :)

For those of you who want to execute a side-effect only if an optional is absent
i.e. an equivalent of ifAbsent() or ifNotPresent() here is a slight modification to the great answers already provided.
myOptional.ifPresentOrElse(x -> {}, () -> {
// logic goes here
})

Title: "How to execute logic on Optional if not present?"
Answer:
Use orElseGet() as a workaround for the missing ifNotPresent(). And since it expects us to return something just return
null.
Optional.empty().orElseGet(() -> {
System.out.println("The object is not present");
return null;
});
//output: The object is not present
or
Optional.ofNullable(null).orElseGet(() -> {
System.out.println("The object is not present");
return null;
});
//output: The object is not present
I also use it to easily implement the singleton pattern with lazy initialization.
public class Settings {
private Settings(){}
private static Settings instance;
public static synchronized Settings getInstance(){
Optional.ofNullable(instance).orElseGet(() -> instance = new Settings());
return instance;
}
}
Of course the getInstance() content can be written in one line by directly returning the first statement, but I wanted to demonstrate the use of orElseGet() as an ifNotPresent().

I was able to came up with a couple of "one line" solutions, for example:
obj.map(o -> (Runnable) () -> o.setAvailable(true))
.orElse(() -> logger.fatal("Object not available"))
.run();
or
obj.map(o -> (Consumer<Object>) c -> o.setAvailable(true))
.orElse(o -> logger.fatal("Object not available"))
.accept(null);
or
obj.map(o -> (Supplier<Object>) () -> {
o.setAvailable(true);
return null;
}).orElse(() () -> {
logger.fatal("Object not available")
return null;
}).get();
It doesn't look very nice, something like orElseRun would be much better, but I think that option with Runnable is acceptable if you really want one line solution.

With Java 8 Optional it can be done with:
Optional<Obj> obj = dao.find();
obj.map(obj.setAvailable(true)).orElseGet(() -> {
logger.fatal("Object not available");
return null;
});

In order to get the value from one call, or do an extra call if the previous returned an empty value, you can chain the commands.
public Optional<Obj> getObjectFromDB() {
return dao.find().or(() -> dao.findBySomethingElse());
}

You need Optional.isPresent() and orElse(). Your snippet won;t work because it doesn't return anything if not present.
The point of Optional is to return it from the method.

ifPresentOrElse can handle cases of nullpointers as well. Easy approach.
Optional.ofNullable(null)
.ifPresentOrElse(name -> System.out.println("my name is "+ name),
()->System.out.println("no name or was a null pointer"));

I suppose you cannot change the dao.find() method to return an instance of Optional<Obj>, so you have to create the appropriate one yourself.
The following code should help you out. I've create the class OptionalAction,
which provides the if-else mechanism for you.
public class OptionalTest
{
public static Optional<DbObject> getObjectFromDb()
{
// doa.find()
DbObject v = find();
// create appropriate Optional
Optional<DbObject> object = Optional.ofNullable(v);
// #formatter:off
OptionalAction.
ifPresent(object)
.then(o -> o.setAvailable(true))
.elseDo(o -> System.out.println("Fatal! Object not available!"));
// #formatter:on
return object;
}
public static void main(String[] args)
{
Optional<DbObject> object = getObjectFromDb();
if (object.isPresent())
System.out.println(object.get());
else
System.out.println("There is no object!");
}
// find may return null
public static DbObject find()
{
return (Math.random() > 0.5) ? null : new DbObject();
}
static class DbObject
{
private boolean available = false;
public boolean isAvailable()
{
return available;
}
public void setAvailable(boolean available)
{
this.available = available;
}
#Override
public String toString()
{
return "DbObject [available=" + available + "]";
}
}
static class OptionalAction
{
public static <T> IfAction<T> ifPresent(Optional<T> optional)
{
return new IfAction<>(optional);
}
private static class IfAction<T>
{
private final Optional<T> optional;
public IfAction(Optional<T> optional)
{
this.optional = optional;
}
public ElseAction<T> then(Consumer<? super T> consumer)
{
if (optional.isPresent())
consumer.accept(optional.get());
return new ElseAction<>(optional);
}
}
private static class ElseAction<T>
{
private final Optional<T> optional;
public ElseAction(Optional<T> optional)
{
this.optional = optional;
}
public void elseDo(Consumer<? super T> consumer)
{
if (!optional.isPresent())
consumer.accept(null);
}
}
}
}

Related

Java Lambda - How to insert a conditional within a complicated nested lamda

I have some existing code where I need to add a conditional. In the below, if the value of "at" is "FOOBAR", then instead of "accessTypeHit.right.getSortValues()" I need "accessTypeHit.right.getSourceAsString()". Otherwise, it should still be "accessTypeHit.right.getSortValues()".
Due to the complexity of the nested lambda, I am having difficulty figuring out how to address this. I would be grateful for any ideas. thanks.
public class DSHTAFunction implements Function<SearchHits, List<ImmutablePair<String, Set<AObject>>>> {
#Override
public List<ImmutablePair<String, Set<AObject>>> apply(#NonNull SearchHits searchHits) {
return StreamSupport.stream(searchHits.spliterator(), false).map(searchHit -> {
String id = searchHit.getFields().get(ID_FIELD).getValue();
Set<AObject> AObjects = Sets.newHashSet();
AObjects.addAll(ATM.INSTANCE.getTypes().stream()
.flatMap(at -> {
if (searchHit.getInnerHits() == null) {
return Stream.empty();
}
return Arrays.stream(searchHit.getInnerHits().getOrDefault(at.getIFName(), SearchHits.empty())
.getHits()).map(h -> ImmutablePair.of(at, h));})
.flatMap(accessTypeHit ->
Arrays.stream(accessTypeHit.right.getSortValues())
.filter(sv -> sv != null)
.map(sv -> new AObject(accessTypeHit.left, sv.toString())))
.filter(AObject::isNonDefault).collect(Collectors.toSet()));
return ImmutablePair.of(id, AObjects);
}).collect(Collectors.toList());
}
Extract every nested flatMap function into its own method. Then your work will become much simpler
public class DSHTAFunction implements Function<SearchHits, List<ImmutablePair<String, Set<AObject>>>> {
#Override
public List<ImmutablePair<String, Set<AObject>>> apply(#NonNull SearchHits searchHits) {
return StreamSupport.stream(searchHits.spliterator(), false).map(this::hitsToPairs).collect(Collectors.toList());
}
private ImmutablePair<String, Set<AObject>> hitsToPairs(SearchHit searchHit) {
String id = searchHit.getFields().get(ID_FIELD).getValue();
Set<AObject> AObjects = ATM.INSTANCE.getTypes().stream()
.flatMap(at -> accessTypeHits(searchHit, at))
.flatMap(this::toAObject)
.collect(Collectors.toSet());
return ImmutablePair.of(id, AObjects);
}
private Stream<ImmutablePair<AccessType, Hit>> accessTypeHits(SearchHit searchHit, AccessType at) {
if (searchHit.getInnerHits() == null) {
return Stream.empty();
}
return Arrays.stream(searchHit.getInnerHits().getOrDefault(at.getIFName(), SearchHits.empty()).getHits())
.map(h -> ImmutablePair.of(at, h));
}
private Stream<AObject> toAObject(ImmutablePair<AccessType, Hit> accessTypeHit) {
return Arrays.stream(accessTypeHit.right.getSortValues())
.filter(Objects::nonNull)
.map(sv -> new AObject(accessTypeHit.left, sv.toString())))
.filter(AObject::isNonDefault);
}
}

How to chain observables?

I need to chain two observables, the second depends on the first.
So what I have is:
Observable 1 -> petition returns Observable<TvShow>
//kotlin
fun getTvShow(): Observable<TvShow> {
return retrofitPetitionGetShow()...
}
//java
Observable<TvShow> getTvShow(){
return retrofitPetitionGetShow()...
}
Observable 2 -> returns Single<List<Season>>
Observable.range(1, TvShow.totalSeasons)
.flatMap { seasonNumber: Int ->
retrofitPetitionGetSeason(seasonNumber)....
}.toList()
What I need is the result of the second observable (List<Season>) to be added to the TvShow object TvShow.setList(List<Season>) and then return it.
Thank you in advance
Basing on provided information in comments you can try with the following code (it is Java code but it should be easy to convert that to Kotlin):
private Observable<TvShow> getTvShow() {
return retrofitPetitionGetShow();
}
private Single<List<Season>> getSeasons(TvShow tvShow) {
return Observable.range(1, tvShow.getTotalSeasons())
.flatMap(seasonNumber -> retrofitPetitionGetSeason(seasonNumber))
.toList();
}
public Observable<TvShow> chainObservables() {
return getTvShow()
.flatMap(tvShow -> getSeasons(tvShow).map(tvShow::withSeasons).toObservable());
}
IMPORTANT!
In reactive/functional way you should not modify objects, but create the new one (in your case, there is an update of tvShow with seasons list). There is a tvShow::withSeasons method reference which is implemented in this way:
public TvShow withSeasons(List<Season> seasons) {
return this.seasons == seasons ? this : new TvShow(this.name, this.totalSeasons, seasons);
}

Generic Filter for enum

Following is my class
public final class Test {
enum Animal {DOG,CAT};
enum COLOR {RED,YELLOW};
class Meaningless {
String animal,color;
}
public void filter(List<Meaningless> meaninglesses){
meaninglesses.stream()
.filter(meaningless -> {
try {
Animal.valueOf(meaningless.animal);
return true;
}catch(Exception e){
return false;
}
})
.filter(meaningless -> {
try {
COLOR.valueOf(meaningless.color);
return true;
}catch(Exception e){
return false;
}
})
.collect(Collectors.toList());
}
}
The 2 iterations of filter methods essentially filters out the invalid enum types. How can I remove the code duplication from this ? The check should be generic enough so that I dont have to change the isValidEnum when there is a new enum introduced.
Ideally I would like to do something like
meaninglesses.stream()
.filter(meaningless -> isValidEnum(meaningless.animal,Animal.class))
.filter(meaningless -> isValidEnum(meaningless.color,COLOR.class))
The following utility method should do the trick here,
public static <E extends Enum<E>> boolean validateEnum(Class<E> clazz, String s) {
return EnumSet.allOf(clazz).stream().anyMatch(e -> e.name().equals(s));
}
And here's how your client code looks in practice,
boolean isValid = validateEnum(Animal.class, "DOG");
Finally, putting it back to your context, it should be something like this.
meaninglesses.stream()
.filter(meaningless -> validateEnum(Animal.class, meaningless.animal))
.filter(meaningless -> validateEnum(COLOR.class, meaningless.color))
.collect(Collectors.toList());
Instead of reinventing the wheel, you can simply go with Apache Common EnumUtils isValidEnum(Class<E> enumClass,String enumName)
Also, isValidEnumIgnoreCase(Class<E> enumClass,String enumName) can be used to check if you need case-insensitivity.
Docs here

function name as a string

I am trying to wrap my head around generic and functions... what I am trying to achieve: Passing function name as a string to get it executed:
I want to do Wrapper.useFunction("eleven") or Wrapper.useFunction("ten")
public class Wrapper<T> {
public F useFunction(Function<F, F> function) {
return function.apply(F);
}
Function<F, String> ten = s -> "10";
Function<F, String> eleven = s -> "11";
}
But this code not even close to compiling. Maybe it doesn't make any sense. Any suggestions?
If you have a finite set of functions which you would like to be able to call I would recommend building a Map which maps Strings to instances of Runnable (or similar functional interfaces). Your useFunction method may then look up the function implementation in the Map and call it if it exists.
Example:
public class SomeClass {
private final Map<String, Runnable> methods = new HashMap<>();
{
methods.put("helloworld", () -> {
System.out.println("Hello World!");
});
methods.put("test", () -> {
System.out.println("test!");
});
methods.put("doStuff", () -> {
System.out.println("doStuff!");
});
}
public void perform(String code) {
methods.getOrDefault(code,
() -> {
System.err.println("No such Method: "+code);
})
.run();
}
}
If you want to call arbitrary methods you should probably use Reflection as stated by others.

Mapping a Nested Optional?

I'm kind of running into a tedious issue with the Java 8 "Optional" container. I cannot map an Optional to "bubble up" another optional.
Let's say I have a RussianNestingDoll class
public class NestedOptionalTest {
public static void main(String[] args) {
RussianNestingDoll doll = RussianNestingDoll.createInstance(RussianNestingDoll.createInstance(RussianNestingDoll.createInstance()));
Optional<Optional<RussianNestingDoll>> thirdDollContents = doll.getInnerDoll().map(d -> d.getInnerDoll());
if (thirdDollContents.isPresent() && thirdDollContents.get().isPresent()) {
System.out.println(thirdDollContents.get().get());
}
else {
System.out.println("empty");
}
}
private static final class RussianNestingDoll {
private final Optional<RussianNestingDoll> innerDoll;
public Optional<RussianNestingDoll> getInnerDoll() {
return innerDoll;
}
private RussianNestingDoll(Optional<RussianNestingDoll> innerDoll) {
this.innerDoll = innerDoll;
}
public static RussianNestingDoll createInstance() {
return new RussianNestingDoll(Optional.empty());
}
public static RussianNestingDoll createInstance(RussianNestingDoll innerDoll) {
return new RussianNestingDoll(Optional.of(innerDoll));
}
}
}
It would be nice to not have to use nested optionals, and instead just have the optional "bubble up". That way I can call "isPresent()" and "get()" just once, rather than calling them both twice. Is there a way I can accomplish this?
I'm not really sure what you want, but you can rewrite your code like this:
RussianNestingDoll doll = RussianNestingDoll.get(RussianNestingDoll.get(RussianNestingDoll.get()));
String content = doll.getInnerDoll()
.flatMap(d -> d.getInnerDoll())
.map(d -> d.get().toString())
.orElse("empty");
System.out.println(content);
In case you want to use the doll afterwards:
Optional<RussianNestingDoll> thirdDoll = doll.getInnerDoll()
.flatMap(d -> d.getInnerDoll());
if (thirdDoll.isPresent()) {
System.out.println(thirdDoll.get());
}
else {
System.out.println("empty");
}
Do you want to flatMap?
thirdDollContents
.flatMap(Function.identity()) // un-nest, get back an Optional<RussianNestingDoll>
.get() // or isPresent()
The flatMap will return an empty Optional if thirdDollContents is empty.

Categories