view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/ParentIndex.java @ 157:f98f2704b154 untilIndex

minnerChange
author one
date Sun, 07 Dec 2014 18:43:32 +0900
parents c23f70bb791f
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index;

import java.util.Iterator;
import java.util.TreeMap;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeChildren;


public class ParentIndex {

  private final TreeMap<TreeNode, TreeNode> parentIndex;

  public ParentIndex() {
    parentIndex =  new TreeMap<TreeNode,TreeNode>();
  }

  public ParentIndex(TreeMap<TreeNode, TreeNode> parentIndex) {
    this.parentIndex = new TreeMap<TreeNode,TreeNode>(parentIndex);
  }

  public boolean isEmpty(){
    return parentIndex.isEmpty();
  }
  

  public TreeMap<TreeNode, TreeNode> getParentIndex() {
    return parentIndex;
  }

  public TreeNode get(TreeNode child) {
    TreeNode parent = parentIndex.get(child);
    if (parent != null)
      return parent;
    return null;
  }

  public ParentIndex set(TreeNode parent) {
    Iterator<TreeNode> childrenIterator = parent.getChildren().iterator();
    for (; childrenIterator.hasNext();) {
      TreeNode child = childrenIterator.next();
      parentIndex.put(child, parent);
    }
    return this;
  }

  public ParentIndex delete(TreeNode child) {
    parentIndex.remove(child);
    return this;
  }

  public ParentIndex deleteAllChildren(TreeNode parentNode) {
    TreeNodeChildren children = parentNode.getChildren();
    Iterator<TreeNode> childrenIterator = children.iterator();
    for (; childrenIterator.hasNext();) {
      TreeNode child = childrenIterator.next();
      parentIndex.remove(child);
    }
    return this;
  }

  public ParentIndex addAllChildren(TreeNode parentNode) {
    TreeNodeChildren children = parentNode.getChildren();
    Iterator<TreeNode> childrenIterator = children.iterator();
    for (; childrenIterator.hasNext();) {
      TreeNode child = childrenIterator.next();
      parentIndex.put(child, parentNode);
    }
    return this;
  }

}