SHARE
TWEET

Nitesh

DulcetAirman Apr 22nd, 2017 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin.playground;
  2.  
  3. import java.util.Optional;
  4. import java.util.function.UnaryOperator;
  5.  
  6. public class SomeClass {
  7.  
  8.     static final class Node {
  9.         private final int value;
  10.  
  11.         public Node(int value, Node prev) {
  12.             this.value = value;
  13.             this.prev = prev;
  14.             if(prev != null)
  15.                 prev.next = this;
  16.         }
  17.  
  18.         private Node next = null;
  19.         private final Node prev;
  20.  
  21.         public Optional<Node> getNext() {
  22.             return Optional.ofNullable(this.next);
  23.         }
  24.  
  25.         public Optional<Node> getPrev() {
  26.             return Optional.ofNullable(this.prev);
  27.         }
  28.  
  29.         public int getValue() {
  30.             return value;
  31.         }
  32.  
  33.     }
  34.  
  35.     static UnaryOperator<Node> op = SomeClass::forward;
  36.  
  37.     static Node backward(Node n) {
  38.         Optional<Node> prev = n.getPrev();
  39.         if (!prev.isPresent()) {
  40.             op = SomeClass::forward;
  41.             return n;
  42.         }
  43.         return prev.get();
  44.     }
  45.  
  46.     static Node forward(Node n) {
  47.         Optional<Node> next = n.getNext();
  48.         if (!next.isPresent()) {
  49.             op = SomeClass::backward;
  50.             return new Node(n.getValue()+1, n);
  51.         }
  52.         return next.get();
  53.     }
  54.  
  55.     public static void main(String[] args) {
  56.         Node n = new Node(1, null);
  57.         while (n.getValue()<8) {
  58.             System.out.format("%d ", n.getValue());
  59.             Node n2 = op.apply(n);
  60.             if(n2==n) System.out.format("%n");
  61.             n = n2;
  62.         }
  63.     }
  64.  
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top