view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/DefaultNode.java @ 0:44465893e8b8

first Commit
author Kazuma
date Wed, 30 Nov 2016 01:47:55 +0900
parents
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.jungle.data.list;


public class DefaultNode<T> implements Node<T> {
    private final T attribute;
    private final Node<T> next;

    public DefaultNode(T attribute, Node<T> next) {
        this.attribute = attribute;
        this.next = next;
    }


    public Node<T> getNext() {
        return next;
    }

    public T getAttribute() {
        return attribute;
    }

    public Node<T> addLast(T attribute) {
        Node<T> node = next.addLast(attribute);
        return new DefaultNode<>(this.attribute, node);
    }

    public Node<T> add(int currentNum, int num, T attribute) {
        if (currentNum == num) {
            Node<T> newNode = new DefaultNode<>(attribute, this.next);
            return new DefaultNode<>(this.attribute,  newNode);
        }

        Node<T> newNode = next.add(currentNum + 1, num, attribute);
        if (newNode == null)
            return null;

        return new DefaultNode<>(this.attribute, newNode);
    }

    @Override
    public Node<T> delete(int currentNum, int deleteNum) {
        if (currentNum == deleteNum) {
            return new DefaultNode<>(this.attribute, this.next.getNext());
        }

        Node<T> newNode = next.delete(currentNum + 1, deleteNum);
        if (newNode == null)
            return null;

        return new DefaultNode<>(this.attribute, newNode);
    }

    @Override
    public Node<T> replaceNode(int currentNum, int num, T attribute) {
        if (currentNum == num) {
            return new DefaultNode<>(attribute, this.getNext());
        }

        Node<T> newNode = next.replaceNode(currentNum + 1, num, attribute);
        if (newNode == null)
            return null;

        return new DefaultNode<>(this.attribute, newNode);
    }

    @Override
    public int length() {
        return next.length() + 1;
    }
}