content: /**
   * Copyright 2010 The Apache Software Foundation
   *
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.alibaba.wasp.jdbc.value;
  
  import com.alibaba.wasp.SQLErrorCode;
  import com.alibaba.wasp.jdbc.JdbcException;
  import com.alibaba.wasp.util.MathUtils;
  
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  
  /**
   * Implementation of the BYTE data type.
   */
  public class ValueByte extends Value {
  
      /**
       * The precision in digits.
       */
      static final int PRECISION = 3;
  
      /**
       * The display size for a byte.
       * Example: -127
       */
      static final int DISPLAY_SIZE = 4;
  
      private final byte value;
  
      private ValueByte(byte value) {
          this.value = value;
      }
  
      private static ValueByte checkRange(int x) {
          if (x < Byte.MIN_VALUE || x > Byte.MAX_VALUE) {
              throw JdbcException.get(SQLErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, Integer.toString(x));
          }
          return ValueByte.get((byte) x);
      }
  
      public int getSignum() {
          return Integer.signum(value);
      }
  
      public Value negate() {
          return checkRange(-(int) value);
      }
  
      public Value subtract(Value v) {
          ValueByte other = (ValueByte) v;
          return checkRange(value - other.value);
      }
  
      public Value multiply(Value v) {
          ValueByte other = (ValueByte) v;
          return checkRange(value * other.value);
      }
  
      public Value divide(Value v) {
          ValueByte other = (ValueByte) v;
          if (other.value == 0) {
              throw JdbcException.get(SQLErrorCode.DIVISION_BY_ZERO_1, getSQL());
          }
          return ValueByte.get((byte) (value / other.value));
      }
  
      public Value modulus(Value v) {
          ValueByte other = (ValueByte) v;
          if (other.value == 0) {
              throw JdbcException.get(SQLErrorCode.DIVISION_BY_ZERO_1, getSQL());
          }
          return ValueByte.get((byte) (value % other.value));
      }
  
      public String getSQL() {
          return getString();
      }
  
      public int getType() {
          return BYTE;
      }
  
      public byte getByte() {
          return value;
      }
  
      protected int compareSecure(Value o, CompareMode mode) {
          ValueByte v = (ValueByte) o;
          return MathUtils.compareInt(value, v.value);
      }
  
      public String getString() {
          return String.valueOf(value);
      }
  
      public long getPrecision() {
          return PRECISION;
      }
  
      public int hashCode() {
          return value;
      }
  
      public Object getObject() {
          return Byte.valueOf(value);
      }
  
      public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
          prep.setByte(parameterIndex, value);
      }
  
      /**
       * Get or create byte value for the given byte.
       *
       * @param i the byte
       * @return the value
       */
      public static ValueByte get(byte i) {
          return new ValueByte(i);
      }
  
      public int getDisplaySize() {
          return DISPLAY_SIZE;
      }
  
      public boolean equals(Object other) {
          return other instanceof ValueByte && value == ((ValueByte) other).value;
      }
  
  }
  