view src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java @ 19:703f0be5368a

added attribute cache
author Shoshi TAMAKI
date Thu, 20 Dec 2012 18:09:17 +0900
parents a46ff0114a9e
children 848f73545c4d
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl;

import java.util.Iterator;

import fj.P2;
import fj.data.List;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableWrapper;

public class DefaultChildren implements Children
{
	private final List<DefaultNode> children;
	private static final List<DefaultNode> EMPTY = List.nil();
	
	public DefaultChildren()
	{
		this(EMPTY);
	}
	
	public DefaultChildren(List<DefaultNode> _children)
	{
		children = _children;
	}
	
	@Override
	public int size()
	{
		return children.length();
	}
	
	public DefaultChildren add(DefaultNode _newNode)
	{
		return new DefaultChildren(children.snoc(_newNode));
	}
	
	public DefaultChildren insert(DefaultNode _newNode,int _pos) 
	{
		check(_pos);
		
		P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos);
		List<DefaultNode> newChildren = split._1().snoc(_newNode).append(split._2());
		
		return new DefaultChildren(newChildren);
	}
	
	public DefaultChildren delete(int _pos)
	{
		check(_pos);
		
		P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos);
		List<DefaultNode> newChildren = split._1().init().append(split._2());
		
		return new DefaultChildren(newChildren);
	}
	
	public void check(int _pos)
	{
		if(children.length() < _pos){
			throw new IllegalArgumentException("_pos > length");
		}
	}

	@Override
	public Iterator<Node> iterator()
	{
		return (new IterableWrapper<Node>(children)).iterator();
	}
}