001: /*
002: * Copyright 2006 Davide Deidda
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * SqlInteger.java
019: *
020: * Created on 4 dicembre 2003, 23.41
021: */
022:
023: package it.biobytes.ammentos.fieldtypes;
024:
025: import java.sql.Types;
026:
027: import it.biobytes.ammentos.*;
028:
029: import it.biobytes.ammentos.util.*;
030:
031: /**
032: *
033: * @author davide
034: */
035: public class IntegerType implements FieldType {
036:
037: public String formatValue(Object value) {
038: String res = null;
039: if (value != null)
040: res = value.toString();
041: return res;
042: }
043:
044: public Object loadValue(java.sql.ResultSet rs, Field field)
045: throws java.sql.SQLException {
046: Object res = null;
047: res = new Integer(rs.getInt(field.getName()));
048: if (rs.wasNull())
049: res = null;
050: return res;
051: }
052:
053: public Object parseValue(String str) throws PersistenceException {
054: return Integer.valueOf(str);
055: }
056:
057: public void setParamValue(Object fieldValue,
058: java.sql.PreparedStatement pstmt, int paramIndex)
059: throws java.sql.SQLException {
060: pstmt.setInt(paramIndex, ((Integer) fieldValue).intValue());
061: }
062:
063: public Object generateValue() throws PersistenceException {
064: return new Integer(Sequencer.nextInt());
065: }
066:
067: public boolean isNumeric() {
068: return true;
069: }
070:
071: public Object[] getPossibleValues() {
072: return null;
073: }
074:
075: /**
076: * Returns the sum of the provided values, or null if summing is not possible
077: *
078: * @param value1 First value to add
079: * @param value2 Second value to add
080: */
081: public Object addValues(Object value1, Object value2) {
082: int v1 = (Integer) value1;
083: int v2 = (Integer) value2;
084: return (v1 + v2);
085: }
086:
087: /**
088: * Gets the underlying mapped Java class represented from elements of this
089: * type
090: *
091: * @return the Java class mapped from this FieldType
092: */
093: public Class getMappedClass() {
094: return Integer.TYPE;
095: }
096:
097: public int getSqlType() {
098: return Types.INTEGER;
099: }
100: }
|