/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Nov 16, 2005 */ package ml.core.helper; import java.util.Vector; import ml.colour.Colour; import ml.core.exceptions.Exception; import ml.math.Vector3D; public class Value implements Cloneable { public enum Type { None, Boolean, Integer, Float, String, Vector3D, Colour, BooleanArray, IntegerArray, FloatArray, StringArray, Vector3DArray } protected Type type; protected int maxSize; /** * This array stores the values of this property. We have to be careful because it isn't typed * and we don't want to have mixed types in this array. */ protected Vector values; public Value() { values = new Vector(); } public Type getType() { return type; } public void setType(Type type) { this.type = type; if(type==Type.BooleanArray || type==Type.IntegerArray || type==Type.FloatArray || type==Type.StringArray || type==Type.Vector3DArray) { values.setSize(0); maxSize=0; } else if (type != Type.None) { values.setSize(1); maxSize = 1; } } public int getSize() { return values.size(); } public void setMaxSize(int maxSize) throws Exception { if (type == Type.Integer || type == Type.Float || type == Type.String || type == Type.Vector3D || type == Type.Colour || type == Type.None) { throw new ml.core.exceptions.Exception("cannot set array size for non-array value types"); } this.maxSize = maxSize; values.setSize(maxSize); } public void copyFrom(Value value) { type=value.type; maxSize=value.maxSize; values=(Vector) value.values.clone(); } @Override public Value clone() { Value value=new Value(); value.copyFrom(this); return value; } public void remove(int index) throws Exception { if (type == Type.Integer || type == Type.Float || type == Type.String || type == Type.Vector3D || type == Type.Colour || type == Type.None) { throw new ml.core.exceptions.Exception("cannot remove array element on non-array value types"); } if(index >= values.size() || index < 0) { throw new ml.core.exceptions.Exception("invalid array index"); } values.remove(index); } public String getStringValue() throws Exception { if (type == Type.String) { return (String) values.get(0); } else { throw new ml.core.exceptions.Exception("cannot return string from non-string Value"); } } public Vector3D getVectorValue() throws Exception { if (type == Type.Vector3D) { return ((Vector3D) values.get(0)).clone(); } else { throw new ml.core.exceptions.Exception("cannot return vector from non-vector Value"); } } public Colour getColourValue() throws Exception { if (type == Type.Colour) { return (Colour) values.get(0); } else { throw new ml.core.exceptions.Exception("cannot return double from non-colour Value"); } } public boolean getBooleanValue() throws Exception { if (type == Type.Boolean) { return (Boolean) values.get(0); } else { throw new ml.core.exceptions.Exception("cannot return boolean from non-boolean Value"); } } public int getIntegerValue() throws Exception { if (type == Type.Integer) { return (Integer) values.get(0); } else { throw new ml.core.exceptions.Exception("cannot return integer from non-integer Value"); } } public double getFloatValue() throws Exception { if (type == Type.Float) { return (Double) values.get(0); } else { throw new ml.core.exceptions.Exception("cannot return double from non-double Value"); } } public void setValue(String stringValue) throws Exception { if (type == Type.String) { values.set(0, stringValue); } else { throw new ml.core.exceptions.Exception("cannot set string value on non-string Value"); } } public void setValue(Vector3D vectorValue) throws Exception { if (type == Type.Vector3D) { values.set(0, vectorValue.clone()); } else { throw new ml.core.exceptions.Exception("cannot set vector value on non-vector Value"); } } public void setValue(Colour colourValue) throws Exception { if (type == Type.Colour) { values.set(0, colourValue.clone()); } else { throw new ml.core.exceptions.Exception("cannot set colour value on non-colour Value"); } } public void setValue(boolean booleanValue) throws Exception { if (type == Type.Boolean) { values.set(0, booleanValue); } else { throw new ml.core.exceptions.Exception("cannot set boolean value on non-boolean Value"); } } public void setValue(int integerValue) throws Exception { if (type == Type.Integer) { values.set(0, integerValue); } else { throw new ml.core.exceptions.Exception("cannot set integer value on non-integer Value"); } } public void setValue(double doubleValue) throws Exception { if (type == Type.Float) { values.set(0, doubleValue); } else { throw new ml.core.exceptions.Exception("cannot set double value on non-double Value"); } } public Vector3D getArrayVectorValue(int index) throws Exception { if (type == Type.Vector3DArray && index < values.size()) { return (Vector3D) values.get(index); } else { throw new ml.core.exceptions.Exception("cannot get vector value from non-vector Value"); } } public String getArrayStringValue(int index) throws Exception { if (type == Type.StringArray && index < values.size()) { return (String) values.get(index); } else { throw new ml.core.exceptions.Exception("cannot get string value from non-string Value"); } } public boolean getArrayBooleanValue(int index) throws Exception { if (type == Type.BooleanArray && index < values.size()) { return (Boolean) values.get(index); } else { throw new ml.core.exceptions.Exception("cannot get boolean value from non-boolean Value"); } } public int getArrayIntegerValue(int index) throws Exception { if (type == Type.IntegerArray && index < values.size()) { return (Integer) values.get(index); } else { throw new ml.core.exceptions.Exception("cannot get integer value from non-integer Value"); } } public double getArrayFloatValue(int index) throws Exception { if (type == Type.FloatArray && index < values.size()) { return (Double) values.get(index); } else { throw new ml.core.exceptions.Exception("cannot get double value from non-double Value"); } } public void setArrayValue(int index, String stringValue) throws Exception { if (type == Type.StringArray && (index < maxSize || maxSize == 0)) { if (maxSize == 0 && index >= values.size()) { values.setSize(index + 1); } values.set(index, stringValue); } else { throw new ml.core.exceptions.Exception("cannot set string value on non-string Value"); } } public void setArrayValue(int index, Vector3D vectorValue) throws Exception { if (type == Type.Vector3DArray && (index < maxSize || maxSize == 0)) { if (maxSize == 0 && index >= values.size()) { values.setSize(index + 1); } values.set(index, vectorValue); } else { throw new ml.core.exceptions.Exception("cannot set vector value on non-vector Value"); } } public void setArrayValue(int index, boolean booleanValue) throws Exception { if (type == Type.BooleanArray && ((index < maxSize) || (maxSize == 0))) { if (maxSize == 0 && index >= values.size()) { values.setSize(index + 1); } values.set(index, booleanValue); } else { throw new ml.core.exceptions.Exception("cannot set boolean value on non-boolean Value"); } } public void setArrayValue(int index, int integerValue) throws Exception { if (type == Type.IntegerArray && ((index < maxSize) || (maxSize == 0))) { if (maxSize == 0 && index >= values.size()) { values.setSize(index + 1); } values.set(index, integerValue); } else { throw new ml.core.exceptions.Exception("cannot set integer value on non-integer Value"); } } public void setArrayValue(int index, double doubleValue) throws Exception { if (type == Type.FloatArray && (index < maxSize || maxSize == 0)) { if (maxSize == 0 && index >= values.size()) { values.setSize(index + 1); } values.set(index, doubleValue); } else { throw new ml.core.exceptions.Exception("cannot set double value on non-double Value"); } } public void setValue(Value value) { type = value.type; maxSize = value.maxSize; values = new Vector(); if (type == Type.Vector3D || type == Type.Vector3DArray || type == Type.Colour) { for (int i = 0; i < value.values.size(); i++) { if (type == Type.Vector3D || type == Type.Vector3DArray) { values.add(((Vector3D) value.values.get(i)).clone()); } else if (type == Type.Colour) { values.add(((Colour) value.values.get(i)).clone()); } } } else { // a shallow copy is enough here as the references inside the arrays // are replaced when values change for these types values = (Vector) value.values.clone(); } } }