view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/DefaultNode.java @ 212:bdfd1e7c8bb7

minner change
author tatsuki
date Tue, 04 Aug 2015 09:54:01 +0900
parents 74648c746dd4
children 7da9056e9357
line wrap: on
line source

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

/**
 * Created by e115731 on 15/05/16.
 */
public class DefaultNode<T> implements Node<T> {
    private final T attribute;
    private final Node next;

    //最終的に消す
    int num = 0;

    public DefaultNode(T attribute, int num, Node next) {
        this.attribute = attribute;
        this.next = next;
    }
//ここまで

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

    public int getNum() {
        return num;
    }

    public Node getNext() {
        return next;
    }

    public T getAttribute() {
        return attribute;
    }

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

    public Node<T> add(int currentNum, int num, T attribute) {
        if (currentNum == num) {
            Node<T> newNode = new DefaultNode(attribute, num, this.next);
            return new DefaultNode<T>(this.attribute, this.num + 1, 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;
    }
}