fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.function.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. StringUtil.indexOf("hello, world", 'l')
  11. .DoIfSuccess(foundIndex -> {
  12. System.out.println(String.format("index=%d", foundIndex));
  13. });
  14. }
  15. }
  16.  
  17. class StringUtil {
  18. public static ResultOfIndexOf indexOf(String haystack, char needle) {
  19. for (int i = 0; i < haystack.length(); ++i)
  20. if (haystack.charAt(i) == needle)
  21. return new ResultOfIndexOf(i);
  22. return new ResultOfIndexOf();
  23. }
  24. }
  25.  
  26. class ResultOfIndexOf {
  27. private final boolean isFound;
  28. private final int foundIndex;
  29. ResultOfIndexOf() {
  30. this(false, 0);
  31. }
  32. ResultOfIndexOf(int foundIndex) {
  33. this(true, foundIndex);
  34. }
  35. private ResultOfIndexOf(boolean isFound, int foundIndex) {
  36. this.isFound = isFound;
  37. this.foundIndex = foundIndex;
  38. }
  39. public void DoIfSuccess(Consumer<Integer> postAction) {
  40. if (isFound && postAction != null)
  41. postAction.accept(foundIndex);
  42. }
  43. }
  44.  
Success #stdin #stdout 0.12s 2184192KB
stdin
Standard input is empty
stdout
index=2