001/** 
002 * Copyright (c) 2007-2008, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.token.tokenizer;
025
026/**
027 * <br>
028 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
029 * All rights reserved.
030 * 
031 * <p>
032 */
033
034public class Token {
035  private int begin;
036
037  private int end;
038
039  private String tokenText;
040
041  public Token(int begin, int end, String tokenText) {
042    super();
043    if (end < begin)
044      throw new IllegalArgumentException(
045          String
046              .format(
047                  "the end of a token must follow the beginning of a token: begin=%1$d, end=%2$d, token text='%3$s'",
048                  begin,
049                  end,
050                  tokenText));
051    this.begin = begin;
052    this.end = end;
053    this.tokenText = tokenText == null ? "" : tokenText;
054    if (this.tokenText.length() != (end - begin))
055      throw new IllegalArgumentException(
056          String
057              .format(
058                  "the length of the token text must equal the extent specified by begin and end: token text length=%1$d, (end - begin)=%2$d, token text='%3$s'",
059                  this.tokenText.length(),
060                  (end - begin),
061                  tokenText));
062  }
063
064  public int getBegin() {
065    return begin;
066  }
067
068  public int getEnd() {
069    return end;
070  }
071
072  public String getTokenText() {
073    return tokenText;
074  }
075
076  @Override
077  public int hashCode() {
078    return tokenText.hashCode();
079  }
080
081  @Override
082  public boolean equals(Object obj) {
083    if (obj instanceof Token) {
084      Token token = (Token) obj;
085      if (tokenText.equals(token.getTokenText()) && begin == token.getBegin()
086          && end == token.getEnd())
087        return true;
088    }
089    return false;
090  }
091
092  @Override
093  public String toString() {
094    return tokenText + "[" + begin + "," + end + "]";
095  }
096
097}