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 DOUBLE data type.
017: */
018: public class ValueDouble extends Value {
019: public static final int PRECISION = 17;
020: public static final int DISPLAY_SIZE = 24; // -3.3333333333333334E-100
021:
022: private final double value;
023: private static final double DOUBLE_ZERO = 0.0;
024: private static final double DOUBLE_ONE = 1.0;
025: private static final ValueDouble ZERO = new ValueDouble(DOUBLE_ZERO);
026: private static final ValueDouble ONE = new ValueDouble(DOUBLE_ONE);
027:
028: private ValueDouble(double value) {
029: this .value = value;
030: }
031:
032: public Value add(Value v) {
033: ValueDouble v2 = (ValueDouble) v;
034: return ValueDouble.get(value + v2.value);
035: }
036:
037: public Value subtract(Value v) {
038: ValueDouble v2 = (ValueDouble) v;
039: return ValueDouble.get(value - v2.value);
040: }
041:
042: public Value negate() {
043: return ValueDouble.get(-value);
044: }
045:
046: public Value multiply(Value v) {
047: ValueDouble v2 = (ValueDouble) v;
048: return ValueDouble.get(value * v2.value);
049: }
050:
051: public Value divide(Value v) throws SQLException {
052: ValueDouble v2 = (ValueDouble) v;
053: if (v2.value == 0.0) {
054: throw Message.getSQLException(ErrorCode.DIVISION_BY_ZERO_1,
055: getSQL());
056: }
057: return ValueDouble.get(value / v2.value);
058: }
059:
060: public String getSQL() {
061: return getString();
062: }
063:
064: public int getType() {
065: return Value.DOUBLE;
066: }
067:
068: protected int compareSecure(Value o, CompareMode mode) {
069: ValueDouble v = (ValueDouble) 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 double getDouble() {
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 double?
094: return 0;
095: }
096:
097: public int hashCode() {
098: long hash = Double.doubleToLongBits(value);
099: return (int) (hash ^ (hash >> 32));
100: }
101:
102: public Object getObject() {
103: return ObjectUtils.getDouble(value);
104: }
105:
106: public void set(PreparedStatement prep, int parameterIndex)
107: throws SQLException {
108: prep.setDouble(parameterIndex, value);
109: }
110:
111: public static ValueDouble get(double d) {
112: if (DOUBLE_ZERO == d) {
113: return ZERO;
114: } else if (DOUBLE_ONE == d) {
115: return ONE;
116: }
117: return (ValueDouble) Value.cache(new ValueDouble(d));
118: }
119:
120: public int getDisplaySize() {
121: return DISPLAY_SIZE;
122: }
123:
124: public boolean equals(Object other) {
125: return other instanceof ValueDouble
126: && value == ((ValueDouble) other).value;
127: }
128:
129: }
|