📄 value.java
字号:
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source 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. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus.env;import com.caucho.quercus.QuercusException;import com.caucho.quercus.QuercusRuntimeException;import com.caucho.quercus.expr.Expr;import com.caucho.quercus.program.AbstractFunction;import com.caucho.util.L10N;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.math.BigDecimal;import java.math.BigInteger;import java.net.MalformedURLException;import java.net.URL;import java.util.*;/** * Represents a PHP expression value. */abstract public class Value implements java.io.Serializable{ protected static final L10N L = new L10N(Value.class); public static final StringValue SCALAR_V = new StringBuilderValue("scalar"); public static final Value []NULL_VALUE_ARRAY = new Value[0]; public static final Value []NULL_ARGS = new Value[0]; // // Properties // /** * Returns the value's class name. */ public String getClassName() { return getType(); } // // Predicates and Relations // /** * Returns true for an implementation of a class */ public boolean isA(String name) { return false; } /** * Checks if 'this' is a valid protected call for 'className' */ public void checkProtected(Env env, String className) { } /** * Checks if 'this' is a valid private call for 'className' */ public void checkPrivate(Env env, String className) { } /** * Returns the ValueType. */ public ValueType getValueType() { return ValueType.VALUE; } /** * Returns true for an array. */ public boolean isArray() { return false; } /** * Returns true for a double-value. */ public boolean isDoubleConvertible() { return false; } /** * Returns true for a long-value. */ public boolean isLongConvertible() { return false; } /** * Returns true for a null. */ public boolean isNull() { return false; } /** * Returns true for a number. */ public boolean isNumberConvertible() { return isLongConvertible() || isDoubleConvertible(); } /** * Matches is_numeric */ public boolean isNumeric() { return false; } /** * Returns true for an object. */ public boolean isObject() { return false; } /* * Returns true for a resource. */ public boolean isResource() { return false; } /** * Returns true for a StringValue. */ public boolean isString() { return false; } /** * Returns true for a BinaryValue. */ public boolean isBinary() { return false; } /** * Returns true for a UnicodeValue. */ public boolean isUnicode() { return false; } /** * Returns true for a BooleanValue */ public boolean isBoolean() { return false; } /** * Returns true for a DefaultValue */ public boolean isDefault() { return false; } /** * Returns true if the value is set. */ public boolean isset() { return true; } /** * Returns true if the value is empty */ public boolean isEmpty() { return false; } /** * Returns true if there are more elements. */ public boolean hasCurrent() { return false; } /** * Returns true for equality */ public Value eqValue(Value rValue) { return eq(rValue) ? BooleanValue.TRUE : BooleanValue.FALSE; } /** * Returns true for equality */ public boolean eq(Value rValue) { if (rValue instanceof BooleanValue) return toBoolean() == rValue.toBoolean(); else if (isLongConvertible() && rValue.isLongConvertible()) return toLong() == rValue.toLong(); else if (isNumberConvertible() || rValue.isNumberConvertible()) return toDouble() == rValue.toDouble(); else return toString().equals(rValue.toString()); } /** * Returns true for equality */ public boolean eql(Value rValue) { return this == rValue.toValue(); } /** * Returns a negative/positive integer if this Value is * lessthan/greaterthan rValue. */ public int cmp(Value rValue) { // This is tricky: implemented according to Table 15-5 of // http://us2.php.net/manual/en/language.operators.comparison.php Value lVal = toValue(); Value rVal = rValue.toValue(); if (lVal instanceof StringValue && rVal instanceof NullValue) return ((StringValue) lVal).cmpString(StringValue.EMPTY); if (lVal instanceof NullValue && rVal instanceof StringValue) return StringValue.EMPTY.cmpString((StringValue) rVal); if (lVal instanceof StringValue && rVal instanceof StringValue) return ((StringValue) lVal).cmpString((StringValue) rVal); if (lVal instanceof NullValue || lVal instanceof BooleanValue || rVal instanceof NullValue || rVal instanceof BooleanValue) { boolean lBool = toBoolean(); boolean rBool = rValue.toBoolean(); if (!lBool && rBool) return -1; if (lBool && !rBool) return 1; return 0; } if (lVal.isObject() && rVal.isObject()) return ((ObjectValue) lVal).cmpObject((ObjectValue) rVal); if ((lVal instanceof StringValue || lVal instanceof NumberValue || lVal instanceof ResourceValue) && (rVal instanceof StringValue || rVal instanceof NumberValue || rVal instanceof ResourceValue)) return NumberValue.compareNum(lVal, rVal); if (lVal instanceof ArrayValue) return 1; if (rVal instanceof ArrayValue) return -1; if (lVal instanceof ObjectValue) return 1; if (rVal instanceof ObjectValue) return -1; // XXX: proper default case? throw new RuntimeException("values are incomparable: " + lVal + " <=> " + rVal); } /** * Returns true for less than */ public boolean lt(Value rValue) { return cmp(rValue) < 0; } /** * Returns true for less than or equal to */ public boolean leq(Value rValue) { return cmp(rValue) <= 0; } /** * Returns true for greater than */ public boolean gt(Value rValue) { return cmp(rValue) > 0; } /** * Returns true for greater than or equal to */ public boolean geq(Value rValue) { return cmp(rValue) >= 0; } // // Conversions // /** * Converts to a boolean. */ public boolean toBoolean() { return true; } /** * Converts to a long. */ public long toLong() { return toBoolean() ? 1 : 0; } /** * Converts to an int */ public int toInt() { return (int) toLong(); } /** * Converts to a double. */ public double toDouble() { return 0; } /** * Converts to a char */ public char toChar() { String s = toString(); if (s == null || s.length() < 1) return 0; else return s.charAt(0); } /** * Converts to a string. * * @param env */ public StringValue toString(Env env) { return toStringValue(); } /** * Converts to an array. */ public Value toArray() { return new ArrayValueImpl().append(this); } /** * Converts to an array if null. */ public Value toAutoArray() { return this; } /** * Casts to an array. */ public ArrayValue toArrayValue(Env env) { env.warning(L.l("'{0}' ({1}) is not assignable to ArrayValue", this, getType())); return null; } /** * Converts to an object if null. */ public Value toAutoObject(Env env) { return this; } /** * Converts to an object. */ public Value toObject(Env env) { ObjectValue obj = env.createObject(); obj.putField(env, env.createString("scalar"), this); return obj; } /** * Converts to a java object. */ public Object toJavaObject() { return null; } /** * Converts to a java object. */ public Object toJavaObject(Env env, Class type) { env.warning(L.l("Can't convert {0} to Java {1}", getClass().getName(), type.getName())); return null; } /** * Converts to a java object. */ public Object toJavaObjectNotNull(Env env, Class type) { env.warning(L.l("Can't convert {0} to Java {1}", getClass().getName(), type.getName())); return null; } /** * Converts to a java boolean object. */ public Boolean toJavaBoolean() { return toBoolean() ? Boolean.TRUE : Boolean.FALSE; } /** * Converts to a java byte object. */ public Byte toJavaByte() { return new Byte((byte) toLong()); } /** * Converts to a java short object. */ public Short toJavaShort() { return new Short((short) toLong()); } /** * Converts to a java Integer object. */ public Integer toJavaInteger() { return new Integer((int) toLong()); } /** * Converts to a java Long object. */ public Long toJavaLong() { return new Long((int) toLong()); } /** * Converts to a java Float object. */ public Float toJavaFloat() { return new Float((float) toDouble()); } /** * Converts to a java Double object. */ public Double toJavaDouble() { return new Double(toDouble()); } /** * Converts to a java Character object. */ public Character toJavaCharacter() { return new Character(toChar()); } /** * Converts to a java String object. */ public String toJavaString() { return toString(); } /** * Converts to a java Collection object. */ public Collection toJavaCollection(Env env, Class type) { env.warning(L.l("Can't convert {0} to Java {1}", getClass().getName(), type.getName())); return null; } /** * Converts to a java List object. */ public List toJavaList(Env env, Class type) { env.warning(L.l("Can't convert {0} to Java {1}", getClass().getName(), type.getName())); return null; } /** * Converts to a java Map object. */ public Map toJavaMap(Env env, Class type) { env.warning(L.l("Can't convert {0} to Java {1}", getClass().getName(), type.getName())); return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -