/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.*;
interface Searchable {
public static <T extends Enum,N extends Comparable> Optional<T> getByCode(Supplier<T[]> v,Function<T,N> s,N code)
{
.filter(data -> s.apply(data).equals(code))
.findFirst();
}
}
enum TestEnum implements Searchable {
A(1),B(2),C(3);
private TestEnum (int id) { this.id = id;}
public Integer getId
() { return id
; }
public static Optional
<TestEnum
> getById
(Integer id
){ return Searchable.getByCode(TestEnum::values,TestEnum::getId,id);
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
TestEnum.
getById(2).
ifPresent(System.
out::println
); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuZnVuY3Rpb24uKjsKCmludGVyZmFjZSBTZWFyY2hhYmxlIHsKICAgIHB1YmxpYyBzdGF0aWMgPFQgZXh0ZW5kcyBFbnVtLE4gZXh0ZW5kcyBDb21wYXJhYmxlPiBPcHRpb25hbDxUPiBnZXRCeUNvZGUoU3VwcGxpZXI8VFtdPiB2LEZ1bmN0aW9uPFQsTj4gcyxOIGNvZGUpIAogICAgewogICAgICAgIHJldHVybiBBcnJheXMuc3RyZWFtKHYuZ2V0KCkpCiAgICAgICAgICAgICAgICAuZmlsdGVyKGRhdGEgLT4gcy5hcHBseShkYXRhKS5lcXVhbHMoY29kZSkpCiAgICAgICAgICAgICAgICAuZmluZEZpcnN0KCk7CiAgICB9Cn0KCmVudW0gVGVzdEVudW0gaW1wbGVtZW50cyBTZWFyY2hhYmxlIHsgCiAgQSgxKSxCKDIpLEMoMyk7CiAgcHJpdmF0ZSBmaW5hbCBJbnRlZ2VyIGlkOwogIHByaXZhdGUgVGVzdEVudW0gKGludCBpZCkgeyB0aGlzLmlkID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