1

Can you please give an example how to transform this using Java 8 Optional.

          Motion motion = new Motion();

           if (null!= site.getLocation() && null != site.getLocation().getLatitude() && null != site.getLocation().getLongitude()) {
                Point p = GeoJson.point(site.getLocation().getLatitude(), site.getLocation().getLongitude());
                motion.setLocation(p);
            }

Up till now I do this

    Motion motion = new Motion();
    Optional<Location> locationOptional = Optional.ofNullable(site.getLocation());
    Point p = locationOptional
          .map(location -> {
                    if (Optional.ofNullable(location.getLatitude()).isPresent() && Optional.ofNullable(location.getLongitude()).isPresent()) {
                        return GeoJson.point(location.getLatitude(), location.getLongitude());
                    }
                    return null;
                })
      .orElse(null);
     motion.setLocation(p);
Tim Schwalbe
  • 1,588
  • 4
  • 19
  • 37
  • 5
    My humble opinion is that `Optional.ofNullable(foo).isPresent()` is worse than `foo != null` and `orElse(null)` is simply breaking the purpose of an `Optional`. I hope you just presented your question like that for simplicity and isn't real code, because you can clearly see that the second option is more verbose and uselessly complex than the first one. – Jeremy Grand Jun 29 '17 at 11:29
  • I see not an advantage over a vanilla null check. Can someone explain the benefits and use-cases for Optional? – Tim Schwalbe Jun 29 '17 at 11:36
  • 3
    I see benefits in using `Optional` as a return parameter. You state in your interfaces/api that you return `Optional` rather than `Object` to politely inform the following developers that interface with that API that they need to do a null check. `Optional`s are also neater when dealing with chained methods. But IMO, there should be no reason at all to ever declare a local `Optional`. – Jeremy Grand Jun 29 '17 at 11:40
  • So it makes sense that every getter returns an Optional? – Tim Schwalbe Jun 29 '17 at 11:45
  • It depends. you can't label *every* getter – Eugene Jun 29 '17 at 11:52
  • 1
    I second @Eugene on "it depends". I wouldn't have a getter returning an `Optional` as I don't like having intelligence in a getter and that a getter is pretty much the same thing as an `Optional` (meaning a mere `Supplier`). What I think is legit would be a `findOneById` in a DAL service. There, you can return `Optional` to remind every user that, sometimes, the findOne doesn't find anything. If you want a more detailed discussion on the matter, you can see https://stackoverflow.com/questions/23454952/uses-for-optional?rq=1 – Jeremy Grand Jun 29 '17 at 11:58

1 Answers1

8
GeoJson geoJson = 
    Optional.ofNullable(s.getLocation())
            .filter(l -> l.getLatitude() != null)
            .filter(l -> l.getLongitude() != null)
            .map(l -> GeoJson.point(l.getLatitude(), l.getLongitude()))
            .orElse(null);
Eugene
  • 117,005
  • 15
  • 201
  • 306