package ru.job4j;

import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * Task 5.3.4.
 * Create check for cyclicity
 *
 * Created by ANTON VASILYUK on 16.06.2017.
 * @version 1.0
 */

public class CycleList<T> {

    private Node<T> hadNode;
    private Node<T> previos;
    private Node<T> nowNode;
    private int size = 0;

    public void add(T value) {
        if (value == null) {throw new NullPointerException("Element is null");}
        if (hadNode == null) {
            Node<T> node = new Node(value, null, null);
            hadNode = node;
            previos = node;
            nowNode = node;
            this.size++;
        } else {
            Node<T> node = new Node(value, previos, null);
            previos.setNext(node);
            nowNode = node;
            this.size++;
        }
    }

    public boolean hasCycle(){
        boolean result = false;
        List<T> list = new LinkedList<>();
        Node<T> temp = hadNode;
        int checkOfNum = 1;
        if (temp != null) {
            while(checkOfNum == 1) {
                checkOfNum = 0;
                list.add(temp.value);
                for(T t : list) {
                    if (t == temp.value) {checkOfNum++;}
                }

                if (checkOfNum > 1) {result = true;}

                if (temp.getNext() != null) {temp = temp.getNext();}
                else {break;}
            }
        } else {throw new NoSuchElementException("It list is empty");}
        if (size == 1) {throw new NoSuchElementException("Add more element!");}
        return result;
    }

    public void makeCycle() {
        nowNode.setNext(hadNode);
        hadNode.setPrevios(nowNode);
    }

    class Node<T> {

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

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

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

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

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

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

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

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

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


@@ -914,416 +914,202 @@
 t = 
-false;%0A        List%3CT%3E list = new LinkedList%3C%3E();%0A        Node%3CT%3E temp = hadNode;%0A        int checkOfNum = 1;%0A        if (temp != null) %7B%0A            while(checkOfNum == 1) %7B%0A                checkOfNum = 0;%0A                list.add(temp.value);%0A                for(T t : list) %7B%0A                    if (t == temp.value) %7BcheckOfNum++;%7D%0A                %7D%0A%0A                if (checkOfNum %3E 1) %7Bresult = true;%7D%0A
+true;%0A        Node%3CT%3E temp = hadNode;%0A        if (size == 1) %7Bthrow new NoSuchElementException(%22Add more element!%22);%7D%0A        if (temp != null) %7B%0A            for (int i = 0; i %3C size + 2; i++) %7B
 %0A   
@@ -1196,16 +1196,32 @@
   else %7B
+result = false; 
 break;%7D%0A
@@ -1309,88 +1309,8 @@
 );%7D%0A
-        if (size == 1) %7Bthrow new NoSuchElementException(%22Add more element!%22);%7D%0A
     

add file for chapter_003_5.3.4