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

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);
}
}

Related

How do I cleanly drill down through nested object in Java?

I have the following Java class:
public class MyClass{
private List<Settings> settings;
public static class Settings {
private String name;
private List<Features> features;
}
public static class Features {
private String name;
private Boolean isActive;
}
}
What I want to do is first check that settings is not null or empty. If not, then I want to find the Settings object that has the name "reliability", and then find its Features objects that have the names "logs" and "score" and get the isActive from these two objects.
This is what I've tried:
MyClass myClass = new MyClass ();
Boolean logs = false;
Boolean score = false;
if (myClass.getSettings != null) {
for (Settings setting: myClass.getSettings) {
if (setting.getName().equals("reliability")) {
for (Features features : setting.getFeatures) {
if (features.getName().equals("logs")) {
logs = features.getIsActive;
} else if (features.getName().equals("score")) {
score = features.getIsActive;
}
}
}
}
}
How do I do this in a clean way? I can only do it with countless nested if and for loops, and it is not pretty.
Here is the possible solution with Streams.
I assume that there would be no duplicated Features (i.e. having the same name) objects.
By the way, class names are usually singular nouns. Class Features is meant to represent a single object with a distinct name and a single property isActive. Therefore, the name Feature might` be more suitable.
The method below expects an argument of type MyClass, settings name and varargs of names of target features. The result it produces is Map with feature names as keys and corresponding isActive properties as values.
public static Map<String, Boolean> getFeaturesByName(MyClass myClass,
String settingName,
String... featureNames) {
if (myClass.getSettings() == null) return Collections.emptyMap();
Set<String> featureSet = Set.of(featureNames);
return myClass.getSettings().stream()
.filter(settings -> settings.getName().equals(settingName))
.flatMap(settings -> settings.getFeatures().stream())
.filter(features -> featureSet.contains(features.getName()))
.collect(Collectors.toMap(
MyClass.Features::getName,
MyClass.Features::getActive
));
}
Delegate the "drilling" to your classes:
add to MyClass
public boolean hasSettings() {
return settings != null && !settings.isEmpty();
}
public Settings getSetting(String name) {
return settings.stream()
.filter(s -> s.hasName(name))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No settings with name " + name));
}
add to Settings
public boolean hasName(String name) {
return this.name.equals(name);
}
public Features getFeature(String name) {
return features.stream()
.filter(f -> f.hasName(name))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No feature with name " + name));
}
add to Features
public boolean hasName(String name) {
return this.name.equals(name);
}
Then you can do
if (myClass.hasSettings()) {
Settings reliabilitySetting = myClass.getSetting("reliability");
logs = reliabilitySetting.getFeature("logs").isActive();
score = reliabilitySetting.getFeature("score").isActive();
}
NOTE: You can return Optionals if you don't want to throw exceptions.
Well to check if the object is nullable or not you can use the optional class then regarding of find the setting object you have an option to use Java Stream Filter to filter the result.
You can stream API to reduce the code
private static final String SETTINGS_NAME_FOR_CHECK = "reliability";
private static final List<String> FEATURES_NAME_FOR_CHECK = Arrays.asList("logs", "score");
public static void main(String[] args) {
MyClass myClass = new MyClass();
if(myClass.getSettings() != null) {
Settings correctSettings = myClass.getSettings().stream()
.filter(setting -> SETTINGS_NAME_FOR_CHECK.equals(setting.getName()))
.findFirst().orElse(null);
if(correctSettings.getFeatures() != null) {
List<Features> features = correctSettings.getFeatures().stream()
.filter(feature -> (FEATURES_NAME_FOR_CHECK.contains(feature.getName()))).collect(Collectors.toList());
System.out.println(features);
}
}
}
Once you get the features (which will be only having logs and shares), you can check for the boolean value.
To make your code cleaner you can move logic into support methods. This also makes it more testable. In your code you are inspecting an instance of MyClass to determine if it certain features which are identified by name. You could write a method that does just that. Your original code could be re-written as:
MyClass myClass = ...
boolean hasLogs = hasSettingFeature(myClass, "reliability", "logs");
boolean hasScore = hasSettingFeature(myClass, "reliability", "score");
You can iterate through the given instance within the support model. You can do with this for-loops.
public boolean hasSettingFeature(MyClass myClass, String settingName, String featureName) {
if (null == myClass || null == myClass.getSettings()) {
return false;
}
for (Settings settings : myClass.getSettings()) {
if (settingName.equals(settings.getName()) {
for (Features features : settings.getFeatures()) {
if (featureName.equals(features.getName()) {
return true;
}
}
}
}
return false;
}
You may also use the Stream API to filter to determine the state:
public boolean hasSettingFeature(MyClass myClass, String settingName, String featureName) {
if (null == myClass || null == myClass.getSettings()) {
return false;
}
return myClass.getSettings().stream()
.filter(setting -> settingName.equals(setting.getName()))
.flatMap(setting -> setting.getFeatures())
.filter(features -> featureName.equals(features.getName()))
.findAny()
.isPresent();
}
I think, a more fitting datastructure would be the Map. I presume every Features exists only once?
public class MyClass {
private Map<String, Settings> settings;
public static class Settings {
private String name;
private Map<String, Features> features;
}
public static class Features {
private String name;
private Boolean isActive;
}
public static void main(String[] args) {
MyClass t = new MyClass();
Boolean logs = null, score = null;
Settings s = t.settings.get("reliability");
if (s != null) {
Features f;
if ((f = s.features.get("logs")) != null) // Is "features" null-save?
logs = f.isActive;
else if ((f = s.features.get("score")) != null)
score = f.isActive;
}
System.out.println("Logs: " + logs + ", Score: " + score);
}
}
(Also it is very bad practice to use the nullable wrapperclass Boolean. You might wanna do something about that.)
If you have problems turning your existing lists into maps, use this method:
public static void main(String[] args) {
Map<String, Settings> map = toMap(List.of(new Settings()), s -> s.name);
}
static <K, V> Map<K, V> toMap(Collection<V> c, Function<V, K> func) {
return c.stream().collect(Collectors.toMap(func, v -> v));
}

predicate filter rxjava2 - How to pass dynamic filter arguments

I have built some basic filter with rxjava2 which works as expected. I was wondering how I could pass values/pass arguments to the filter (return td.getTypeId() == **<value>**;). Also if somebody has ideas/clues/examples on how to build a (semi-)dynamic query (Object.<field> == <value>) with rxjava2 / filter that would be appreciated.
Predicate<TradeDetailed> testfilter;
Flowable<List<TradeDetailed>> td = tr.getTradesDetailedFlowable();
testfilter = new Predicate<TradeDetailed>() {
#Override
public boolean test(#NonNull TradeDetailed td) throws Exception {
return td.getTypeId() == 0;
}
};
Disposable d = td
.flatMapIterable(e -> e)
.filter(e-> testfilter.test(e))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(t -> {
System.out.println("filtered "+t.getReference()));
},
err -> {
System.out.println("error");
}
);
you can create a separate class for the Predicate:
class YourPredicate implements Predicate<TradeDetailed>(){
private final int compareAgainst;
public YourPredicate(int compareAgainst){
this.compareAgainst = compareAgainst;
}
#Override
public boolean test(#NonNull TradeDetailed td) throws Exception {
return td.getTypeId() == compareAgainst;
}
}

What does this error mean and what is wrong with my code?

I have the following function:
public CompletableFuture<List<String>> getUsers(final String users) {
final CompletionStage<List<String>> cacheFuture = cache.read(users.toString());
return cacheFuture.thenCompose((List<String> userList) -> {
if (userList != null) {
return CompletableFuture.completedFuture(userList);
}
return service.getUsers(users).thenApply((List<String> usersFresh) -> {
cache.write(users.toString(), usersFresh);
return usersFresh;
});
});
}
The compiler error I am getting is:
Bad return type in lambda expression: List cannot be converted
to U
on the line return usersFresh
The method signature for service.getUsers is:
CompletableFuture<List<String>> getUsers(String users);
I don't understand what is wrong with my code and why it won't compile.
You need to convert CompletionStage to CompletableFuture by using toCompletableFuture, like:
public CompletableFuture<List<String>> getUsers(final String users) {
final CompletionStage<List<String>> cacheFuture = cache.read(users.toString());
return cacheFuture.thenCompose((List<String> userList) -> {
if (userList != null) {
return CompletableFuture.completedFuture(userList);
}
return service.getUsers(users).thenApply((List<String> usersFresh) -> {
cache.write(users.toString(), usersFresh);
return usersFresh;
});
}).toCompletableFuture(); //!!! convert `CompletionStage` to `CompletableFuture` in here
}
it is similar with your method want to return ArrayList but in method return List.
Let's see:
CompletableFuture<T> implements CompletionStage
Your expectation return :
public CompletableFuture<List<String>>
But cacheFuture.thenCompose return CompletionStage
You should change:
public CompletableFuture<List<String>> to public CompletionStage<List<String>>

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

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);
}
}
}
}

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