import java.util.Optional;
public final class Main {
public static void main
(String[] args
) { var yellow
= Color.
findByName("YELLOW"); yellow.ifPresent(c -> {
var rgb = c.getRgb();
System.
out.
println("yellow: rgb=" + rgb
); });
// Alternatively, we may write as follows, but...
var blue
= Color.
findByName("BLUE"); if (blue.isPresent()) {
var rgb = blue.get().getRgb();
System.
out.
println("blue: rgb=" + rgb
); }
}
/** Represents blue. */
static final Color BLUE
= () -> 0xff
;
/**
Returns the Color instance that matches with the specified name.
@param name
...
@return
An {@link Optional<T> Optional} object containing the Color
instance that matches with {@code name}, or an empty Optional
object (if nothing matches).
*/
static Optional
<Color
> findByName
(String name
) { return name.equals("BLUE")
? Optional.
of(Color.
BLUE) : Optional.empty();
}
/**
Returns the 24-bit integer value representing RGB.
@return
...
*/
int getRgb();
}
}
aW1wb3J0IGphdmEudXRpbC5PcHRpb25hbDsKCnB1YmxpYyBmaW5hbCBjbGFzcyBNYWluIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgewogICAgICAgIHZhciB5ZWxsb3cgPSBDb2xvci5maW5kQnlOYW1lKCJZRUxMT1ciKTsKICAgICAgICB5ZWxsb3cuaWZQcmVzZW50KGMgLT4gewogICAgICAgICAgICB2YXIgcmdiID0gYy5nZXRSZ2IoKTsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ5ZWxsb3c6IHJnYj0iICsgcmdiKTsKICAgICAgICB9KTsKCiAgICAgICAgLy8gQWx0ZXJuYXRpdmVseSwgd2UgbWF5IHdyaXRlIGFzIGZvbGxvd3MsIGJ1dC4uLgogICAgICAgIHZhciBibHVlID0gQ29sb3IuZmluZEJ5TmFtZSgiQkxVRSIpOwogICAgICAgIGlmIChibHVlLmlzUHJlc2VudCgpKSB7CiAgICAgICAgICAgIHZhciByZ2IgPSBibHVlLmdldCgpLmdldFJnYigpOwogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oImJsdWU6IHJnYj0iICsgcmdiKTsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIGludGVyZmFjZSBDb2xvciB7CgogICAgICAgIC8qKiBSZXByZXNlbnRzIGJsdWUuICovCiAgICAgICAgc3RhdGljIGZpbmFsIENvbG9yIEJMVUUgPSAoKSAtPiAweGZmOwoKCSAgICAvKioKCSAgICAgICAgUmV0dXJucyB0aGUgQ29sb3IgaW5zdGFuY2UgdGhhdCBtYXRjaGVzIHdpdGggdGhlIHNwZWNpZmllZCBuYW1lLgoJCgkgICAgICAgIEBwYXJhbSBuYW1lCgkgICAgICAgICAgICAuLi4KCSAgICAgICAgQHJldHVybgoJICAgICAgICAgICAgQW4ge0BsaW5rIE9wdGlvbmFsPFQ+IE9wdGlvbmFsfSBvYmplY3QgY29udGFpbmluZyB0aGUgQ29sb3IKCSAgICAgICAgICAgIGluc3RhbmNlIHRoYXQgbWF0Y2hlcyB3aXRoIHtAY29kZSBuYW1lfSwgb3IgYW4gZW1wdHkgT3B0aW9uYWwKCSAgICAgICAgICAgIG9iamVjdCAoaWYgbm90aGluZyBtYXRjaGVzKS4KCSAgICAqLwogICAgICAgIHN0YXRpYyBPcHRpb25hbDxDb2xvcj4gZmluZEJ5TmFtZShTdHJpbmcgbmFtZSkgewogICAgICAgICAgICByZXR1cm4gbmFtZS5lcXVhbHMoIkJMVUUiKQogICAgICAgICAgICAgICAgPyBPcHRpb25hbC5vZihDb2xvci5CTFVFKQogICAgICAgICAgICAgICAgOiBPcHRpb25hbC5lbXB0eSgpOwogICAgICAgIH0KCiAgICAgICAgLyoqCgkgICAgICAgIFJldHVybnMgdGhlIDI0LWJpdCBpbnRlZ2VyIHZhbHVlIHJlcHJlc2VudGluZyBSR0IuCgogICAgICAgICAgICBAcmV0dXJuCiAgICAgICAgICAgICAgICAuLi4KICAgICAgICAqLwogICAgICAgIGludCBnZXRSZ2IoKTsKICAgIH0KfQo=