view src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java @ 41:ed6737db637a

added tests
author Shoshi TAMAKI
date Tue, 29 Jan 2013 23:42:54 +0900
parents 35e327577b58
children 7aa195d5bdab 3a3622cf2eef
line wrap: on
line source

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


import java.util.Iterator;
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.store.trasnformer.NodeEditorError;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableWrapper;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;

public class DefaultChildren implements Children<Node>
{
	private final List<DefaultNode> children;
	
	public DefaultChildren(List<DefaultNode> _children)
	{
		children = _children;
	}
	
	/*
	public Either<Error,DefaultNode> replaceNode(DefaultNode _target,int _pos)
	{
		if(check(_pos)){
			return DefaultEither.newA(OUT_OF_RANGE);
		}
		
		P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos);
		List<DefaultNode> head = split._1();
		List<DefaultNode> tail = split._2().tail().cons(_target);
		List<DefaultNode> newList = head.append(tail);
		
		DefaultNode newNode = new DefaultNode(newList,attrs);
		return DefaultEither.newB(newNode);
	}
	*/
	
	@Override
	public int size()
	{
		return children.length();
	}
	
	/*
	
	public Either<Error,DefaultNode> addNewChildAt(int _pos)
	{
		if(!check(_pos)){
			return DefaultEither.newA(OUT_OF_RANGE);
		}
		
		P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos);
		List<DefaultNode> newChildren = split._1().snoc(new DefaultNode()).append(split._2());
		DefaultNode newNode = new DefaultNode(newChildren,attrs);
		
		return DefaultEither.newB(newNode);
	}
	
	public Either<Error,DefaultNode> deleteChildAt(int _pos)
	{
		if(!check(_pos)){
			return DefaultEither.newA(OUT_OF_RANGE);
		}
		
		P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos);
		List<DefaultNode> newChildren = split._1().init().append(split._2());
		
		DefaultNode newNode = new DefaultNode(newChildren,attrs);
		
		return DefaultEither.newB(newNode);
	}
	
	*/
	
	@Override
	public Either<Error,Node> at(int _pos)
	{
		DefaultNode target = children.index(_pos);
		if(target == null){
			return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); // TODO
		}
		Node ret = target;
		
		return DefaultEither.newB(ret);
	}
	
	public List<DefaultNode> getChildrenAsRawList()
	{
		return children;
	}

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