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.token;
021
022public class Token {
023
024  private final int line;
025  private final int column;
026  private final String value;
027
028  /**
029   * Cache for hash code.
030   */
031  private int hash;
032
033  public Token(String value, int line, int column) {
034    this.value = value;
035    this.column = column;
036    this.line = line;
037  }
038
039  public int getLine() {
040    return line;
041  }
042
043  public int getColumn() {
044    return column;
045  }
046
047  public String getValue() {
048    return value;
049  }
050
051  @Override
052  public boolean equals(Object object) {
053    if (object instanceof Token) {
054      Token anotherToken = (Token) object;
055      return anotherToken.line == line && anotherToken.column == column && anotherToken.value.equals(value);
056    }
057    return false;
058  }
059
060  @Override
061  public int hashCode() {
062    int h = hash;
063    if (h == 0) {
064      h = value.hashCode();
065      h = 31 * h + line;
066      h = 31 * h + column;
067      hash = h;
068    }
069    return h;
070  }
071
072  @Override
073  public String toString() {
074    return "'" + value + "'[" + line + "," + column + "]";
075  }
076
077}