Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.claude_martin.playground;
- import java.util.Optional;
- import java.util.function.UnaryOperator;
- public class SomeClass {
- static final class Node {
- private final int value;
- public Node(int value, Node prev) {
- this.value = value;
- this.prev = prev;
- if(prev != null)
- prev.next = this;
- }
- private Node next = null;
- private final Node prev;
- public Optional<Node> getNext() {
- return Optional.ofNullable(this.next);
- }
- public Optional<Node> getPrev() {
- return Optional.ofNullable(this.prev);
- }
- public int getValue() {
- return value;
- }
- }
- static UnaryOperator<Node> op = SomeClass::forward;
- static Node backward(Node n) {
- Optional<Node> prev = n.getPrev();
- if (!prev.isPresent()) {
- op = SomeClass::forward;
- return n;
- }
- return prev.get();
- }
- static Node forward(Node n) {
- Optional<Node> next = n.getNext();
- if (!next.isPresent()) {
- op = SomeClass::backward;
- return new Node(n.getValue()+1, n);
- }
- return next.get();
- }
- public static void main(String[] args) {
- Node n = new Node(1, null);
- while (n.getValue()<8) {
- System.out.format("%d ", n.getValue());
- Node n2 = op.apply(n);
- if(n2==n) System.out.format("%n");
- n = n2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement