package ru.job4j;

import java.util.Iterator;

/**
 * Task 5.4.2.
 * Create my LinkedSet
 *
 * Created by ANTON on 17.06.2017.
 * @version 1.0
 */
public class SimpleSetLinked<E> implements Iterable<E>{


    /**.
     * @previosElement is previos element for this position
     */
    private Node<E> previosElement;

    /**.
     * @headNode is first position
     */
    private Node<E> headNode;

    /**.
     * @size is size list
     */
    private int size = 0;

    /**.
     * @cursorIter is location iterator
     */
    private int cursorIter = 0;

    /**.
     * @tempPrevios is link on back element for iterator
     */
    private Node<E> tempPrevios = null;

    /**.
     * @tempNext is link on next element for iterator
     */
    private Node<E> tempNext = null;

    /**.
     * @tempNow is link on now element for iterator
     */
    private Node<E> tempNow = null;

    /**.
     * Method for add element to link
     * @param value is elemnt for adding
     */
    public void add(E value) {
        if (value == null) {throw new NullPointerException("Element is null");}
        boolean check = true;
        Node<E> temp = headNode;

        if (size != 0) {
            for(int i = 0; i < size; i++) {
                E tempValue = temp.getElement();
                if(tempValue.equals(value)) {check = false;}
                temp = temp.getNext();
            }
        }

        if (check) {
            if (size == 0) {
                Node<E> node = new Node<E>(value, null, null);
                headNode = node;
                previosElement = node;
                size++;
            } else {
                Node<E> node = new Node<E>(value, previosElement, null);
                previosElement.setNext(node);
                previosElement = node;
                size++;
            }
        }
    }

    /**.
     * Realisation iterator
     * @return iterator
     */
    @Override
    public Iterator<E> iterator() {
        Iterator<E> it = (Iterator<E>) new Iterator<Object>() {

            @Override
            public boolean hasNext() {
                if (headNode == null) {throw new NullPointerException("It is empty container");}
                return tempNow.getNext() != null;
            }

            @Override
            public Object next() {
                Node<E> result = null;
                if (tempNow == null) {
                    tempNow = headNode;
                    result = tempNow;
                    cursorIter++;
                } else {
                    if (hasNext()) {
                        tempNow = tempNow.getNext();
                        result = tempNow;
                        if (tempNow.getNext() != null) {tempNext = tempNow.getNext();}
                        tempPrevios = tempNow.getPrevios();
                        cursorIter++;
                    }
                }
                return result.getElement();
            }
        };
        return it;
    }

    /**.
     * Class for create new container
     * @param <E> is element for storage
     */
    private class Node<E> {

        /**.
         * @value is element for storage
         */
        private E value;

        /**.
         * Link on previos node
         */
        private Node<E> first;

        /**.
         * Link on next node
         */
        private Node<E> last;

        /**.
         * Constructor for Node
         * @param value is element for storage
         * @param previos link on previos node
         * @param next link on next node
         */
        public Node(E value, Node<E> previos, Node<E> next) {
            this.value = value;
            this.first = previos;
            this.last = next;
        }

        /**.
         * Method set previos node
         * @param previos is previos node
         */
        public void setPrevios(Node<E> previos) {
            this.first = previos;
        }

        /**.
         * Method set next node
         * @param next is next node
         */
        public void setNext(Node<E> next) {
            this.last = next;
        }

        /**.
         * Method for get previos node
         * @return link previos node
         */
        public Node<E> getPrevios() {
            return this.first;
        }

        /**.
         * Method for get next node
         * @return link next node
         */
        public Node<E> getNext() {
            return this.last;
        }

        /**.
         * Method for get storage element
         * @return this element
         */
        public E getElement() {return this.value;}
    }
}

@@ -201,206 +201,8 @@
 %3E%7B%0A%0A
-%0A    /**.%0A     * @previosElement is previos element for this position%0A     */%0A    private Node%3CE%3E previosElement;%0A%0A    /**.%0A     * @headNode is first position%0A     */%0A    private Node%3CE%3E headNode;%0A%0A
     
@@ -365,333 +365,115 @@
     
-/**.%0A     * @tempPrevios is link on back element for iterator%0A     */%0A    private Node%3CE%3E tempPrevios = null;%0A%0A    /**.%0A     * @tempNext 
+private PseodoLinkedL
 is
+t
  link
- on next element for iterator%0A     */%0A    private Node%3CE%3E tempNext = null;%0A%0A    /**.%0A     * @tempNow is
+;%0A%0A    public SimpleSetLinked() %7B%0A       
  link 
-on now element for iterator%0A     */%0A    private Node%3CE%3E tempNow = null;%0A
+= new PseodoLinkedList();%0A    %7D
 %0A   
@@ -710,71 +710,8 @@
 ue;%0A
-        Node%3CE%3E temp = headNode;%0A%0A        if (size != 0) %7B%0A    
     
@@ -717,16 +717,17 @@
      for
+ 
 (int i =
@@ -763,40 +763,40 @@
     
-    
 E temp
-Value = temp.getElemen
+ = (E) link.iterator().nex
 t();
@@ -812,24 +812,16 @@
     
-    
 if
+ 
 (temp
-Value
 .equ
@@ -853,61 +853,8 @@
 e;%7D%0A
-                temp = temp.getNext();%0A            %7D%0A
     
@@ -851,33 +851,32 @@
 lse;%7D%0A        %7D%0A
-%0A
         if (chec
@@ -896,400 +896,44 @@
     
-if (size == 0) %7B%0A                Node%3CE%3E node = new Node%3CE%3E(value, null, null);%0A                headNode = node;%0A                previosElement = node;%0A                size++;%0A            %7D else %7B%0A                Node%3CE%3E node = new Node%3CE%3E(value, previosElement, null);%0A                previosElement.setNext(node);%0A                previosElement = node;%0A                size++;%0A            %7D
+link.add(value);%0A            size++;
 %0A   
@@ -1215,24 +1215,17 @@
 if (
-headNod
+siz
 e == 
-null
+0
 ) %7Bt
@@ -1308,33 +1308,25 @@
 urn 
-tempNow.getNext() != null
+cursorIter %3C size
 ;%0A  
@@ -1411,23 +1411,17 @@
         
-Node%3CE%3E
+E
  result 
@@ -1452,23 +1452,17 @@
 if (
-tempNow == null
+hasNext()
 ) %7B%0A
@@ -1485,64 +1485,43 @@
     
-tempNow = headNode;%0A                    result = tempNow
+result = (E) link.iterator().next()
 ;%0A  
@@ -1580,364 +1580,58 @@
 se %7B
-%0A                    if (hasNext()) %7B%0A                        tempNow = tempNow.getNext();%0A                        result = tempNow;%0A                        if (tempNow.getNext() != null) %7BtempNext = tempNow.getNext();%7D%0A                        tempPrevios = tempNow.getPrevios();%0A                        cursorIter++;%0A                    %7D%0A                
+throw new NullPointerException(%22No more element%22);
 %7D%0A  
@@ -1661,21 +1661,8 @@
 sult
-.getElement()
 ;%0A  

add file for chapter_003_5.4.2