import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        StringUtil.indexOf("hello, world", 'l')
            .DoIfSuccess(foundIndex -> {
                System.out.println(String.format("index=%d", 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);
    }
}