view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/Index.java @ 168:1749338f2366 util index

until index
author one
date Wed, 24 Dec 2014 16:14:42 +0900
parents 89ed172137ab
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.NulIterator;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
import fj.Ord;
import fj.P2;
import fj.data.List;
import fj.data.Option;

public class Index {

  TreeMap<String, TreeMap<String, List<TreeNode>>> indexList;

  public Index() {
    this.indexList = new TreeMap<String, TreeMap<String, List<TreeNode>>>();
  }

  public Index(TreeMap<String, TreeMap<String, List<TreeNode>>> indexList) {
    this.indexList = indexList;
  }

  public Index set(String key, String value, TreeNode node) {
    TreeMap<String, List<TreeNode>> index = indexList.get(key);
    if (index == null) {
      index = new TreeMap<String, List<TreeNode>>();
      List<TreeNode> nodeList = List.nil();
      List<TreeNode> newNodeList = nodeList.cons(node);
      index.put(value, newNodeList);
      indexList.put(key, index);
      return this;
    }

    List<TreeNode> nodeList = index.get(value);
    List<TreeNode> newNodeList;
    if (nodeList != null) {
      newNodeList = nodeList.cons(node);
    } else {
      nodeList = List.nil();
      newNodeList = nodeList.cons(node);
    }
    index.put(value, newNodeList);
    indexList.put(key, index);

    return this;
  }

  // public Index delete(String key, String value, TreeNode node) {
  // Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
  // if (indexOp.isNone())
  // return this;
  //
  // TreeMap<String, List<TreeNode>> index = indexOp.some();
  // TreeMap<String, List<TreeNode>> newIndex = index;
  // Option<List<TreeNode>> nodeListOp = index.get(value);
  // if (nodeListOp.isSome()) {
  // List<TreeNode> nodeList = nodeListOp.some();
  // List<TreeNode> newNodeList = List.nil();
  // for (TreeNode indexingNode : nodeList) {
  // if (indexingNode.equals(node))
  // newNodeList = newNodeList.cons(indexingNode);
  // }
  //
  // newIndex = index.set(value, newNodeList);
  // } else {
  // return this;
  // }
  // TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList =
  // indexList.set(key, newIndex);
  // return new Index(newIndexList);
  // }

  public List<TreeNode> get(String key, String value) {

    TreeMap<String, List<TreeNode>> index = indexList.get(key);
    if (index == null)
      return null;

    List<TreeNode> nodeList = index.get(value);

    return nodeList;
  }

  public Iterator<TreeNode> getAll(String key) {

    TreeMap<String, List<TreeNode>> index = indexList.get(key);
    if (index == null)
      return null;

    if (!index.isEmpty())
      return new NulIterator<TreeNode>();

    return new Iterator<TreeNode>() {

      Iterator<TreeNode> treeMapIterator = index.get(key).iterator();
      TreeNode node;

      @Override
      public boolean hasNext() {
        if (treeMapIterator.hasNext()) {
          node = treeMapIterator.next();
          return true;
        }
        return false;
      }

      @Override
      public TreeNode next() {
        return node;
      }

    };

  }

  public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
    return indexList;
  }

}