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
022public final class Edge {
023
024  private int beginIndex; // can't be changed
025  private int endIndex;
026  private Node startNode;
027  private Node endNode; // can't be changed, could be used as edge id
028
029  // each time edge is created, a new end node is created
030  public Edge(int beginIndex, int endIndex, Node startNode) {
031    this.beginIndex = beginIndex;
032    this.endIndex = endIndex;
033    this.startNode = startNode;
034    this.endNode = new Node(startNode, null);
035  }
036
037  public Node splitEdge(Suffix suffix) {
038    remove();
039    Edge newEdge = new Edge(beginIndex, beginIndex + suffix.getSpan(), suffix.getOriginNode());
040    newEdge.insert();
041    newEdge.endNode.setSuffixNode(suffix.getOriginNode());
042    beginIndex += suffix.getSpan() + 1;
043    startNode = newEdge.getEndNode();
044    insert();
045    return newEdge.getEndNode();
046  }
047
048  public void insert() {
049    startNode.addEdge(beginIndex, this);
050  }
051
052  public void remove() {
053    startNode.removeEdge(beginIndex);
054  }
055
056  /**
057   * @return length of this edge in symbols
058   */
059  public int getSpan() {
060    return endIndex - beginIndex;
061  }
062
063  public int getBeginIndex() {
064    return beginIndex;
065  }
066
067  public int getEndIndex() {
068    return endIndex;
069  }
070
071  public Node getStartNode() {
072    return startNode;
073  }
074
075  public Node getEndNode() {
076    return endNode;
077  }
078
079}