001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.duplications.detector.suffixtree;
021
022import java.util.Map;
023import java.util.HashMap;
024import java.util.Collection;
025
026public final class Node {
027
028  private final SuffixTree suffixTree;
029  private final Map<Object, Edge> edges;
030
031  /**
032   * Node represents string s[i],s[i+1],...,s[j],
033   * suffix-link is a link to node, which represents string s[i+1],...,s[j].
034   */
035  private Node suffixNode;
036
037  /**
038   * Number of symbols from the root to this node.
039   * <p>
040   * Note that this is not equal to number of nodes from root to this node,
041   * because in a compact suffix-tree edge can span multiple symbols - see {@link Edge#getSpan()}.
042   * </p><p>
043   * Depth of {@link #suffixNode} is always equal to this depth minus one.
044   * </p> 
045   */
046  int depth;
047
048  int startSize, endSize;
049
050  public Node(Node node, Node suffixNode) {
051    this(node.suffixTree, suffixNode);
052  }
053
054  public Node(SuffixTree suffixTree, Node suffixNode) {
055    this.suffixTree = suffixTree;
056    this.suffixNode = suffixNode;
057    edges = new HashMap<Object, Edge>();
058  }
059
060  public Object symbolAt(int index) {
061    return suffixTree.symbolAt(index);
062  }
063
064  public void addEdge(int charIndex, Edge edge) {
065    edges.put(symbolAt(charIndex), edge);
066  }
067
068  public void removeEdge(int charIndex) {
069    edges.remove(symbolAt(charIndex));
070  }
071
072  public Edge findEdge(Object ch) {
073    return edges.get(ch);
074  }
075
076  public Node getSuffixNode() {
077    return suffixNode;
078  }
079
080  public void setSuffixNode(Node suffixNode) {
081    this.suffixNode = suffixNode;
082  }
083
084  public Collection<Edge> getEdges() {
085    return edges.values();
086  }
087
088}