ValueDouble.java :  » Database-DBMS » Quadcap-Embeddable-Database » com » quadcap » sql » types » Java Open Source

Java Open Source » Database DBMS » Quadcap Embeddable Database 
Quadcap Embeddable Database » com » quadcap » sql » types » ValueDouble.java
package com.quadcap.sql.types;

/* Copyright 1999 - 2003 Quadcap Software.  All rights reserved.
 *
 * This software is distributed under the Quadcap Free Software License.
 * This software may be used or modified for any purpose, personal or
 * commercial.  Open Source redistributions are permitted.  Commercial
 * redistribution of larger works derived from, or works which bundle
 * this software requires a "Commercial Redistribution License"; see
 * http://www.quadcap.com/purchase.
 *
 * Redistributions qualify as "Open Source" under  one of the following terms:
 *   
 *    Redistributions are made at no charge beyond the reasonable cost of
 *    materials and delivery.
 *
 *    Redistributions are accompanied by a copy of the Source Code or by an
 *    irrevocable offer to provide a copy of the Source Code for up to three
 *    years at the cost of materials and delivery.  Such redistributions
 *    must allow further use, modification, and redistribution of the Source
 *    Code under substantially the same terms as this license.
 *
 * Redistributions of source code must retain the copyright notices as they
 * appear in each source code file, these license terms, and the
 * disclaimer/limitation of liability set forth as paragraph 6 below.
 *
 * Redistributions in binary form must reproduce this Copyright Notice,
 * these license terms, and the disclaimer/limitation of liability set
 * forth as paragraph 6 below, in the documentation and/or other materials
 * provided with the distribution.
 *
 * The Software is provided on an "AS IS" basis.  No warranty is
 * provided that the Software is free of defects, or fit for a
 * particular purpose.  
 *
 * Limitation of Liability. Quadcap Software shall not be liable
 * for any damages suffered by the Licensee or any third party resulting
 * from use of the Software.
 */

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import java.sql.Types;

import com.quadcap.util.Debug;
import com.quadcap.sql.io.ExternalizeProxy;

/**
 * A <b>double</b> value.
 *
 * @author Stan Bailes
 */
public class ValueDouble extends Value implements Externalizable, ExternalizeProxy {
    double        val;

    public ValueDouble() {}
    
    public ValueDouble(double val) {
        this.val = val;
    }

    public ValueDouble(String s) throws ValueException {
        try {
//-            //#ifdef JDK11
//-  this.val = Double.valueOf(s).doubleValue();
            //#else
            this.val = Double.parseDouble(s);
            //#endif
        } catch (NumberFormatException ne) {
            throw new ValueException("DOUBLE format error");
        }
    }

    public final double doubleValue() { return val; }

    public Value unop(int op) throws ValueException {
  switch (op) {
  case Op.NULL:
      return ValueBoolean.falseBoolean;
  case Op.PLUS:
      return this;
  case Op.MINUS:
      return new ValueDouble(0.0 - val);
  default:
      throw new ValueException("Unary op: " + Op.toString(op) +
             " not implemented for this type");
  }
    }
    
    public Value binop(int op, Value l) throws ValueException {
  return l.binop(op, this);
    }

    public Value binop(int op, ValueFloat r) throws ValueException {
  return ValueDouble.binop(op, this, new ValueDouble(r.val));
    }

    public Value binop(int op, ValueDouble r) throws ValueException {
  return ValueDouble.binop(op, this, r);
    }

    public Value binop(int op, ValueInteger r) throws ValueException {
  return Value.binop(op, this, new ValueDouble(r.val));
    }

    public Value binop(int op, ValueShort r) throws ValueException {
  return Value.binop(op, this, new ValueDouble(r.val));
    }

    public Value binop(int op, ValueByte r) throws ValueException {
  return Value.binop(op, this, new ValueDouble(r.val));
    }

    public Value binop(int op, ValueLong r) throws ValueException {
  return Value.binop(op, this, new ValueDouble(r.val));
    }

    public Value binop(int op, ValueScaledInteger r) throws ValueException {
  return ValueDouble.binop(op, this, new ValueDouble(r.doubleValue()));
    }

    public Value binop(int op, ValueNull r) throws ValueException {
  switch (op) {
  case Op.EQ:
  case Op.NE:
  case Op.LT:
  case Op.LE:
  case Op.GT:
  case Op.GE:
      return ValueUnknown.valueUnknown;
  case Op.PLUS:
  case Op.MINUS:
  case Op.TIMES:
  case Op.DIVIDE:
  case Op.EXP:
      return r;
  case Op.COMPARE:
      return r.valCmpNull();
  default:
      throw badBinop(op, r);
  }
    }

    
    public static final Value binop(int op, ValueDouble e, ValueDouble f)
  throws ValueException
    {
  switch (op) {
        case Op.DIVIDE:
      try {
    return new ValueDouble(e.val / f.val);
      } catch (ArithmeticException ae) {
    throw new ValueException(ae.toString());
      }
        case Op.EQ:
      return new ValueBoolean(e.val == f.val);
        case Op.EXP:
      return new ValueDouble((Math.pow(e.val, f.val)));
        case Op.GE:
      return new ValueBoolean(e.val >= f.val);
        case Op.GT:
      return new ValueBoolean(e.val > f.val);      
        case Op.LE:
      return new ValueBoolean(e.val <= f.val);      
        case Op.LT:
      return new ValueBoolean(e.val < f.val);      
        case Op.MINUS:
      return new ValueDouble(e.val - f.val);
        case Op.NE:
      return new ValueBoolean(e.val != f.val);      
        case Op.PLUS:
      return new ValueDouble(e.val + f.val);
        case Op.TIMES:
      return new ValueDouble(e.val * f.val);
  case Op.COMPARE:
      if (e.val < f.val) return ValueInteger.MINUS_ONE;
      if (e.val > f.val) return ValueInteger.PLUS_ONE;
      return ValueInteger.ZERO;
      
  default:
      throw badBinop(op, e, f);
  }
    }

    public Object asJavaObject() {
  return new Double(val);
    }

    public void fromJavaObject(Object obj) throws ValueException {
  if (obj instanceof Double) {
      val = ((Double)obj).doubleValue();
  } else {
      throw new ValueException("bad type: " + obj);
  }
    }

    public void readExternal(ObjectInput in) throws IOException {
  val = in.readDouble();
    }
    
    public void writeExternal(ObjectOutput out)
  throws IOException
    {
  out.writeDouble(val);
    }

    public Object readObject(ObjectInput in)
  throws IOException, ClassNotFoundException
    {
  ValueDouble v = new ValueDouble();
  v.readExternal(in);
  return v;
    }

    public void writeObject(ObjectOutput out, Object object)
  throws IOException
    {
  ((ValueDouble)object).writeExternal(out);
    }

    public Value convert(TypeTinyInt type) throws ValueException {
        return TypeTinyInt.convertNumber(val);
    }

    public Value convert(TypeSmallInt type) throws ValueException {
        return TypeSmallInt.convertNumber(val);
    }

    public Value convert(TypeInt type) throws ValueException {
        return TypeInt.convertNumber(val);
    }

    public Value convert(TypeBigInt type) throws ValueException {
        return TypeBigInt.convertNumber(val);
    }

    public Value convert(TypeDecimal type) throws ValueException {
  ValueScaledInteger vs = new ValueScaledInteger(val);
  if (type.getPrecision() >= 0) {
      if (vs.requiredPrecision() > type.getPrecision()) {
    throw new ValueException("Too large: " + val +
           ", precision = " +
           vs.requiredPrecision() + 
           ", type precision = " +
           type.getPrecision());
      }
      vs.setScale(type.getScale());
  }
  return vs;
    }

    public Value convert(TypeReal type) throws ValueException {
        if (type.getPrecision() > 31) {
            return this;
        } else {
            return new ValueFloat((float)val);
        }
    }

    public Type getType() {
  return TypeReal.typeDouble;
    }

    public void serializeKey(KeyStream out) throws IOException {
  out.writeDouble(val);
    }

    public String toString() {
  return String.valueOf(val);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.