Package ket.math

Source Code of ket.math.Token

/*
* Copyright (C) 2011  Alasdair C. Hamilton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

package ket.math;

import ket.display.*;
import ket.display.box.Box;
import ket.math.purpose.*;

/**
* Represent the occurrence of a symbol somewhere in an equation.  That is, any
* leaf node of the equation tree.
*/
public class Token extends Argument {

  State state;

  @SuppressWarnings("unused")
  private Token() {
    super();
    this.state = null;
  }

  public Token(int value) {
    super();
    setState(new IntegerValue(value));
  }

  public Token(double value) {
    super();
    setState(new DoubleValue(value));
  }

  public Token(State state) {
    super();
    setState(state);
  }

  @Override
  public State getState() {
    return state;
  }

  @Override
  public String getValue() {
    if (state instanceof Text) {
      return ((Text) state).getValue(); //BUG: Unexpected behaviour.
    } else {
      return null; // TODO: Used?
    }
  }

  @Override
  public Function getFunction() {
    return null;
  }

  @Override
  public int size() {
    return 0;
  }

  @Override
  public int recursiveSize() {
    return 1;
  }

  public void setState(State state) {
    assert state!=null : "Tokens must have a non-null state.";
    this.state = state;
  }

  ///////////////////////////
  // MATH OBJECT INTERFACE //
  ///////////////////////////

  @Override
  public String toPrefixNotation() {
    return state instanceof Text ? toString() : state.getName();
  }

  @Override
  public String toEditString() {
    return toString();
  }

  @Override
  public String toString() {
    String text = state.toString();
    return TextTools.removeWhitespace(text);
  }

  public Token cloneToken() {
    Token clone = new Token(state);
    if (isBold()) {
      clone.setBold(true);
    }
    return clone;
  }

  @Override
  public boolean matches(String pattern) {
    return state.matches(pattern);
  }

  /**
   * Check if the state of a given argument is equal to value.
   */
  @Override
  public boolean elementEquals(double value) {
    if (state instanceof NumberValue) {
      NumberValue numberValue = (NumberValue) state;
      return value==numberValue.getDouble();
     } else {
      return false;
    }
  }

  @Override
  public boolean elementEquals(Argument argument) {
    if (argument==null) {
      return false;
    } else if (state==null) {
      return argument.getState()==null;
    }
    return state.equals(argument.getState());
  }

  @Override
  public boolean subBranchEquals(Argument argument) {
    return this.elementEquals(argument);
  }

  @Override
  public void removeIntermediate() {
    remove();
  }

  @Override
  public String toHTML() {
    return state.toHTML();
  }

  @Override
  public String toLatex() {
    return " " + state.toLatex() + " ";
  }

  @Override
  public Box toBox(long settings, ColourScheme colourScheme) {
    Box box;
    if (isBold()) {
      settings |= Box.BOLD_FONT;
    }
    if (isVisible()) {
      box = state.toBox(this, settings, colourScheme);
    } else {
      box = super.toBox(settings, colourScheme);
    }
    box.setArgument(this);
    return box;
  }


  public String toVerboseString() {
    return state.toString();
  }

  /**
   * Return a unique identifier to associate with the given state.
   */
  public int hashCode() {
    return state.hashCode();
  }

  /**
   * If an argument is branch[-](argument) then return argument,
   * otherwise return null.
   */
  @Override
  public Argument getNegativeFunctionArg() {
    return null;
  }

  public boolean isLeaf() {
    return true;
  }
}
TOP

Related Classes of ket.math.Token

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.