protected Token getOrAddTerminalNode(int index) throws MaltChainedException {
    Token node = null;
    if (!terminalNodes.containsKey(index)) {
      if (index > 0) {
        node = terminalPool.checkOut();
        node.setIndex(index);
        node.setBelongsToGraph(this);

        if (index > 1) {
          Token prev = terminalNodes.get(index - 1);
          if (prev == null) {
            try {
              prev = terminalNodes.get(terminalNodes.headMap(index).lastKey());
            } catch (NoSuchElementException e) {

            }
          }
          if (prev != null) {
            prev.setSuccessor(node);
            node.setPredecessor(prev);
          }

          if (terminalNodes.lastKey() > index) {
            Token succ = terminalNodes.get(index + 1);
            if (succ == null) {
              try {
                succ = terminalNodes.get(terminalNodes.tailMap(index).firstKey());
              } catch (NoSuchElementException e) {

              }
            }
            if (succ != null) {
              succ.setPredecessor(node);
              node.setSuccessor(succ);
            }
          }
        }
      }
      terminalNodes.put(index, node);
      numberOfComponents++;
    } else {
      node = terminalNodes.get(index);
    }
    return node;
  }
 public void clear() throws MaltChainedException {
   terminalPool.checkInAll();
   terminalNodes.clear();
   sentenceID = 0;
   super.clear();
 }