Package civquest.parser.ruleset

Source Code of civquest.parser.ruleset.Field

/*  This file is part of CivQuest.
*
*  CivQuest 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 2 of the License, or
*  (at your option) any later version.
*
*  CivQuest 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 CivQuest; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*  $Id: Field.java 567 2005-05-24 05:34:52Z wpausch $
*/
package civquest.parser.ruleset;

import civquest.parser.ruleset.exception.InvalidFieldValueException;
import civquest.parser.ruleset.exception.RulesetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;


/**
* A Field, contained within a Section, is a (name,value) pair.
* The value can be any Object, dependent on the name (in other words,
* you know by assumption what kind of value to expect from a given name).
*/
public class Field {
   
    private String name;
    private ArrayList<Object> values;
   
    public Field(String name) {
        this.name = name;
        values = new ArrayList<Object>(1);
    }

    public Field(String name, Object value) {
        this(name);
        addValue(value);
    }

  public swifu.parser.ruleset.Field toSwifuField() {
    swifu.parser.ruleset.Field field = new swifu.parser.ruleset.Field(name);

    Iterator iterator = values.listIterator();
    while (iterator.hasNext()) {
      field.addValue(iterator.next());
    }
    return field;
  }

    public Object clone() {
    Field retValue = new Field(name);
    Iterator iterator = values.listIterator();
    while (iterator.hasNext()) {
      retValue.addValue(iterator.next());
    }
    return retValue;
    }

    public int size() {
    return values.size();
  }

  public boolean isBoolValue() {
    return isBoolValue(0);
  }

  public boolean isBoolValue(int index) {
    if (!isStringValue(index)) {
      return false;
    } else {
      String value = (String)(getValue(index));
      return value.equals("True") || value.equals("False")
        || value.equals("true") || value.equals("false");
    }
  }

    public boolean getBoolValue() throws RulesetException {
    return getBoolValue(0, "");
  }

    public boolean getBoolValue(int index)
    throws RulesetException {
    return getBoolValue(index, "");
  }

  public boolean getBoolValue(String commentIfFail)
    throws RulesetException {
    return getBoolValue(0, commentIfFail);
  }

  public boolean getBoolValue(int index, String commentIfFail)
    throws RulesetException {

    if (!isStringValue(index)) {
      throw new InvalidFieldValueException(this, index, "Boolean", commentIfFail);
    } else {
      if (isBoolValue(index)) {
        String stringValue = getStringValue(index);
        return Boolean.valueOf(stringValue).booleanValue();
      } else {
        throw new InvalidFieldValueException(this, index, "Boolean", commentIfFail);
      }
    }
  }

  public boolean isIntValue() {
    return isIntValue(0);
  }

  public boolean isIntValue(int index) {
    Object value = getValue(index);

    if (!(value instanceof String)) {
      return false;
    } else {
      try {
        Integer.parseInt((String)(value));
        return true;
      } catch (NumberFormatException e) {
        return false;
      }
    }
  }
   
    public int getIntValue() throws RulesetException {
    return getIntValue(0, "");
  }

  public int getIntValue(int index) throws RulesetException {
    return getIntValue(index, "");
  }

  public int getIntValue(String commentIfFail)
    throws RulesetException {
    return getIntValue(0, commentIfFail);
  }

    public int getIntValue(int index, String commentIfFail)
    throws RulesetException {

    Object value = getValue(index);

    if (!(value instanceof String)) {
      throw new InvalidFieldValueException(this, index, "Integer", commentIfFail);
    } else {
      int retvalue = 0;
      try {
        retvalue = Integer.parseInt((String)(value));
      } catch(NumberFormatException e) {
        throw new InvalidFieldValueException(this, index, "Integer", commentIfFail);
      }

      return retvalue;
    }
    }

  public boolean isDoubleValue() {
    return isDoubleValue(0);
  }

  public boolean isDoubleValue(int index) {
    Object value = getValue(index);

    if (!(value instanceof String)) {
      return false;
    } else {
      try {
        Double.valueOf((String)(value));
        return true;
      } catch (NumberFormatException e) {
        return false;
      }
    }
  }

    public double getDoubleValue() throws RulesetException {
    return getDoubleValue(0, "");
  }

  public double getDoubleValue(int index) throws RulesetException {
    return getDoubleValue(index, "");
  }

  public double getDoubleValue(String commentIfFail)
    throws RulesetException {
    return getDoubleValue(0, commentIfFail);
  }

    public double getDoubleValue(int index, String commentIfFail)
    throws RulesetException {

    Object value = getValue(index);

    if (!(value instanceof String)) {
      throw new InvalidFieldValueException(this, index, "Double", commentIfFail);
    } else {
      double retvalue = 0;
      try {
        retvalue = (Double.valueOf((String)(value))).doubleValue();
      } catch(NumberFormatException e) {
        throw new InvalidFieldValueException(this, index, "Double", commentIfFail);
      }

      return retvalue;     
    }
    }

//     public float getFloatValue() {return getFloatValue(0);}
//     public float getFloatValue(int index) {return (float)(getDoubleValue(index));}

  public boolean isStringValue() {
    return isStringValue(0);
  }

  public boolean isStringValue(int index) {
    return (getValue(index) instanceof String);
  }

    public String getStringValue() throws RulesetException {
    return getStringValue(0, "");
  }

    public String getStringValue(int index) throws RulesetException {
    return getStringValue(index, "");
  }

  public String getStringValue(String commentIfFail) throws RulesetException {
    return getStringValue(0, commentIfFail);
  }

  public String getStringValue(int index, String commentIfFail) throws RulesetException {
    Object value = getValue(index);
    if (value instanceof String) {
      return (String)(value);
    } else {
      throw new InvalidFieldValueException(this, index, "String", commentIfFail);
    }
  }


  public boolean isTableValue() {
    return isTableValue(0);
  }

  public boolean isTableValue(int index) {
    return (getValue(index) instanceof Table);
  }

    public Table getTableValue() throws RulesetException {
    return getTableValue(0);
  }

  public Table getTableValue(int index) throws RulesetException {
    if (!isTableValue(index)) {
      throw new InvalidFieldValueException(this, index, "Table", "");
    } else {
      return (Table)(getValue(index));
    }
  }

  public boolean isListValue(int index) {
    return (getValue(index) instanceof java.util.List);
  }

    public java.util.List getListValue() throws RulesetException {
    return getListValue(0, "");
  }

  public java.util.List getListValue(int index)
    throws RulesetException {
    return getListValue(index, "");
  }

  public java.util.List getListValue(String commentIfFail)
    throws RulesetException {
    return getListValue(0, commentIfFail);
  }

  public java.util.List getListValue(int index, String commentIfFail)
    throws RulesetException {
    if (!isListValue(index)) {
      throw new InvalidFieldValueException(this, index, "List", commentIfFail);
    } else {
      return (java.util.List)(getValue(index));
    }
  }

 
    public Object getValue(int index) {
    return (index <= values.size()? values.get(index) : null);
  }

  public Object getValue() {
    return getValue(0);
  }
 
    public int getNumberOfValues() {
    return values.size();
  }

  public Iterator<Object> getValueIterator() {
    return values.iterator();
  }

    public void addValue(Object value) {
    values.add(value);
  }

    public String getName() {
    return name;
  }

  public String toString() {
    return getName();
  }
}
TOP

Related Classes of civquest.parser.ruleset.Field

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.