You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ControlFlowDriver.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //package org.acmsl.pocs.lambdafor;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.function.Function;
  6. public class ControlFlowDriver {
  7. private static boolean m__bUsed = false;
  8. public ControlFlowDriver() {
  9. }
  10. protected static void immutableSetUsed(final boolean used) {
  11. m__bUsed = used;
  12. }
  13. protected static void setUsed(final boolean used) {
  14. immutableSetUsed(used);
  15. }
  16. public static boolean isUsed() {
  17. return m__bUsed;
  18. }
  19. public <C extends Collection<I>, I, R> Collection<R> forloop(final C items,
  20. final Function<I, R> lambda) {
  21. setUsed(true);
  22. final List<R> result = new ArrayList<R>(items.size());
  23. final List<I> list = new ArrayList<I>(items);
  24. int position = -1;
  25. while (true) {
  26. ControlFlowCommand command = waitForCommand();
  27. switch (command) {
  28. case NEXT:
  29. position++;
  30. break;
  31. case PREVIOUS:
  32. position++;
  33. break;
  34. case RELOAD:
  35. break;
  36. default:
  37. break;
  38. }
  39. if (position < 0) {
  40. position = 0;
  41. } else if (position > list.size() - 1) {
  42. break;
  43. }
  44. result.set(position, lambda.apply(list.get(position)));
  45. }
  46. return result;
  47. }
  48. protected ControlFlowCommand waitForCommand() {
  49. try {
  50. Thread.sleep(1000);
  51. } catch (final InterruptedException interruptedException) {
  52. // whatever
  53. }
  54. return ControlFlowCommand.NEXT;
  55. }
  56. }