001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.value;
007:
008: import java.sql.PreparedStatement;
009: import java.sql.SQLException;
010:
011: import org.h2.constant.ErrorCode;
012: import org.h2.message.Message;
013: import org.h2.util.ObjectUtils;
014:
015: /**
016: * Implementation of the REAL data type.
017: */
018: public class ValueFloat extends Value {
019: public static final int PRECISION = 7;
020: public static final int DISPLAY_SIZE = 15; // -1.12345676E-20
021:
022: private final float value;
023: private static final float FLOAT_ZERO = 0.0F;
024: private static final float FLOAT_ONE = 1.0F;
025: private static final ValueFloat ZERO = new ValueFloat(FLOAT_ZERO);
026: private static final ValueFloat ONE = new ValueFloat(FLOAT_ONE);
027:
028: private ValueFloat(float value) {
029: this .value = value;
030: }
031:
032: public Value add(Value v) {
033: ValueFloat v2 = (ValueFloat) v;
034: return ValueFloat.get(value + v2.value);
035: }
036:
037: public Value subtract(Value v) {
038: ValueFloat v2 = (ValueFloat) v;
039: return ValueFloat.get(value - v2.value);
040: }
041:
042: public Value negate() {
043: return ValueFloat.get(-value);
044: }
045:
046: public Value multiply(Value v) {
047: ValueFloat v2 = (ValueFloat) v;
048: return ValueFloat.get(value * v2.value);
049: }
050:
051: public Value divide(Value v) throws SQLException {
052: ValueFloat v2 = (ValueFloat) v;
053: if (v2.value == 0.0) {
054: throw Message.getSQLException(ErrorCode.DIVISION_BY_ZERO_1,
055: getSQL());
056: }
057: return ValueFloat.get(value / v2.value);
058: }
059:
060: public String getSQL() {
061: return getString();
062: }
063:
064: public int getType() {
065: return Value.FLOAT;
066: }
067:
068: protected int compareSecure(Value o, CompareMode mode) {
069: ValueFloat v = (ValueFloat) o;
070: if (value == v.value) {
071: return 0;
072: }
073: return value > v.value ? 1 : -1;
074: }
075:
076: public int getSignum() {
077: return value == 0 ? 0 : (value < 0 ? -1 : 1);
078: }
079:
080: public float getFloat() {
081: return value;
082: }
083:
084: public String getString() {
085: return String.valueOf(value);
086: }
087:
088: public long getPrecision() {
089: return PRECISION;
090: }
091:
092: public int getScale() {
093: // TODO value: what is the scale of a float?
094: return 0;
095: }
096:
097: public int hashCode() {
098: long hash = Float.floatToIntBits(value);
099: return (int) (hash ^ (hash >> 32));
100: }
101:
102: public Object getObject() {
103: return ObjectUtils.getFloat(value);
104: }
105:
106: public void set(PreparedStatement prep, int parameterIndex)
107: throws SQLException {
108: prep.setFloat(parameterIndex, value);
109: }
110:
111: public static ValueFloat get(float d) {
112: if (FLOAT_ZERO == d) {
113: return ZERO;
114: } else if (FLOAT_ONE == d) {
115: return ONE;
116: }
117: return (ValueFloat) Value.cache(new ValueFloat(d));
118: }
119:
120: public int getDisplaySize() {
121: return DISPLAY_SIZE;
122: }
123:
124: public boolean equals(Object other) {
125: return other instanceof ValueFloat
126: && value == ((ValueFloat) other).value;
127: }
128:
129: }
|