1 5 package org.h2.value; 6 7 import java.sql.PreparedStatement ; 8 import java.sql.SQLException ; 9 10 import org.h2.engine.Constants; 11 import org.h2.message.Message; 12 13 public class ValueByte extends Value { 14 public static final int PRECISION = 3; 15 16 private byte value; 17 18 private ValueByte(byte value) { 19 this.value = value; 20 } 21 22 public Value add(Value v) throws SQLException { 23 ValueByte other = (ValueByte) v; 24 if(Constants.OVERFLOW_EXCEPTIONS) { 25 return checkRange(value + other.value); 26 } 27 return ValueByte.get((byte) (value + other.value)); 28 } 29 30 private ValueByte checkRange(int value) throws SQLException { 31 if(value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { 32 throw Message.getSQLException(Message.OVERFLOW_FOR_TYPE_1, DataType.getDataType(Value.BYTE).name); 33 } else { 34 return ValueByte.get((byte)value); 35 } 36 } 37 38 public int getSignum() { 39 return value == 0 ? 0 : (value < 0 ? -1 : 1); 40 } 41 42 public Value negate() throws SQLException { 43 if(Constants.OVERFLOW_EXCEPTIONS) { 44 return checkRange(-(int)value); 45 } 46 return ValueByte.get((byte) (-value)); 47 } 48 49 public Value subtract(Value v) throws SQLException { 50 ValueByte other = (ValueByte) v; 51 if(Constants.OVERFLOW_EXCEPTIONS) { 52 return checkRange(value - other.value); 53 } 54 return ValueByte.get((byte) (value - other.value)); 55 } 56 57 public Value multiply(Value v) throws SQLException { 58 ValueByte other = (ValueByte) v; 59 if(Constants.OVERFLOW_EXCEPTIONS) { 60 return checkRange(value * other.value); 61 } 62 return ValueByte.get((byte) (value * other.value)); 63 } 64 65 public Value divide(Value v) throws SQLException { 66 ValueByte other = (ValueByte) v; 67 if (other.value == 0) { 68 throw Message.getSQLException(Message.DIVISION_BY_ZERO_1, getSQL()); 69 } 70 return ValueByte.get((byte) (value / other.value)); 71 } 72 73 public String getSQL() { 74 return getString(); 75 } 76 77 public int getType() { 78 return Value.BYTE; 79 } 80 81 public byte getByte() { 82 return value; 83 } 84 85 protected int compareSecure(Value o, CompareMode mode) { 86 ValueByte v = (ValueByte) o; 87 if (value == v.value) { 88 return 0; 89 } 90 return value > v.value ? 1 : -1; 91 } 92 93 public String getString() { 94 return String.valueOf(value); 95 } 96 97 public long getPrecision() { 98 return PRECISION; 99 } 100 101 public int hashCode() { 102 return value; 103 } 104 105 public Object getObject() { 106 return new Byte (value); 107 } 108 109 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 110 prep.setByte(parameterIndex, value); 111 } 112 113 public static ValueByte get(byte i) { 114 return (ValueByte) Value.cache(new ValueByte(i)); 115 } 116 117 121 public int getDisplaySize() { 122 return PRECISION; 123 } 124 125 protected boolean isEqual(Value v) { 126 return v instanceof ValueByte && value == ((ValueByte)v).value; 127 } 128 129 } 130 | Popular Tags |