view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/Index.java @ 156:c23f70bb791f untilIndex

change Index fj → java.until
author one
date Sun, 07 Dec 2014 15:24:30 +0900
parents 20af7f25ef32
children f98f2704b154
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 fj.data.List;

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(Index indexList) {
    this.indexList = indexList.getIndex();
  }

  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);

    if (nodeList == null)
      return List.nil();

    return nodeList;
  }
  
  public Iterator<TreeNode> getAll(String key){

    TreeMap<String, List<TreeNode>> index = indexList.get(key);
    if (index == null)
      return null;
    
    return new Iterator<TreeNode>(){
      
      Iterator<String> treeMapKeys = index.keySet().iterator();  
      List<TreeNode> nodeList = List.nil();
      TreeNode node;
      @Override
      public boolean hasNext() {
        
        if (nodeList.isNotEmpty()) {
          node = nodeList.head();
          nodeList = nodeList.tail();
          return true;
        }
        
        for (;treeMapKeys.hasNext();) {
          String key = treeMapKeys.next();
          nodeList = index.get(key);
          node = nodeList.head();
          nodeList = nodeList.tail();
          return true;
        }
        return false;
      }

      @Override
      public TreeNode next() {
        return node;
      }
      
    };
    
  }
  
  private TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
    return indexList;
  }
  
}