import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.*;
class Ideone
{
{
StringUtil.indexOf("hello, world", 'l')
.DoIfSuccess(foundIndex -> {
});
}
}
class StringUtil {
public static Maybe
<Integer
> indexOf
(String haystack,
char needle
) { for (int i = 0; i < haystack.length(); ++i)
if (haystack.charAt(i) == needle)
return Maybe.<Integer>just(i);
return Maybe.<Integer>nothing();
}
}
class Maybe<ResultType> {
public static <R> Maybe<R> nothing() {
return new Maybe(false, null);
}
public static <R> Maybe<R> just(R resultValue) {
return new Maybe(true, resultValue);
}
private final boolean isSucceeded;
private final ResultType resultValue;
private Maybe(boolean isSucceeded, ResultType resultValue) {
this.isSucceeded = isSucceeded;
this.resultValue = resultValue;
}
public void DoIfSuccess(Consumer<ResultType> postAction) {
if (isSucceeded && postAction != null)
postAction.accept(resultValue);
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuZnVuY3Rpb24uKjsKCmNsYXNzIElkZW9uZQp7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24KICAgIHsKICAgICAgICBTdHJpbmdVdGlsLmluZGV4T2YoImhlbGxvLCB3b3JsZCIsICdsJykKICAgICAgICAgICAgLkRvSWZTdWNjZXNzKGZvdW5kSW5kZXggLT4gewogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKFN0cmluZy5mb3JtYXQoImluZGV4PSVkIiwgZm91bmRJbmRleCkpOwogICAgICAgICAgICB9KTsKICAgIH0KfQoKY2xhc3MgU3RyaW5nVXRpbCB7CiAgICBwdWJsaWMgc3RhdGljIE1heWJlPEludGVnZXI+IGluZGV4T2YoU3RyaW5nIGhheXN0YWNrLCBjaGFyIG5lZWRsZSkgewogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgaGF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