ValueFloat.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 » ValueFloat.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;

/**
 * A <b>float</b> value.
 *
 * @author Stan Bailes
 */
public class ValueFloat extends Value implements Externalizable {
    float        val;

    public ValueFloat() {}
    
    public ValueFloat(float val) { this.val = val; }

    public ValueFloat(String s) throws ValueException {
        try {
//-            //#ifdef JDK11
//-      this.val = Float.valueOf(s).floatValue();
            //#else
            this.val = Float.parseFloat(s);
            //#endif
        } catch (NumberFormatException ne) {
            throw new ValueException("FLOAT format error");
        }
    }

    public final float floatValue() { 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 ValueFloat(0.0f - 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 ValueFloat.binop(op, this, r);
    }

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

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

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

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

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

    public Value binop(int op, ValueScaledInteger r) throws ValueException {
  return ValueDouble.binop(op, new ValueDouble(val),
                                 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, ValueFloat e, ValueFloat f)
  throws ValueException
    {
  switch (op) {
        case Op.DIVIDE:
      try {
    return new ValueFloat(e.val / f.val);
      } catch (ArithmeticException ae) {
    throw new ValueException("divide by zero");
      }
        case Op.EQ:
      return new ValueBoolean(e.val == f.val);
        case Op.EXP:
      return new ValueFloat((float)(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 ValueFloat(e.val - f.val);
        case Op.NE:
      return new ValueBoolean(e.val != f.val);      
        case Op.PLUS:
      return new ValueFloat(e.val + f.val);
        case Op.TIMES:
      return new ValueFloat(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 Float(val);
    }

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

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

    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);
        int prec = type.getPrecision();
  if (prec > 0 && vs.requiredPrecision() > prec) {
      throw new ValueException("Too large: " + val + ", precision = " +
    vs.requiredPrecision() + ", type precision = " +
    prec);
  }
        int scale = type.getScale();
        if (scale > 0) {
            vs.setScale(scale);
        }
  return vs;
    }

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

    

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

    public void serializeKey(KeyStream out) throws IOException {
  out.writeFloat(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.