View Javadoc

1   package org.apache.torque.om.mapper;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.ResultSet;
23  import java.sql.SQLException;
24  
25  import org.apache.torque.TorqueException;
26  
27  /**
28   * Maps a database record to a Integer.
29   *
30   * @version $Id: IntegerMapper.java 1380467 2012-09-04 07:33:05Z tfischer $
31   */
32  public class IntegerMapper implements RecordMapper<Integer>
33  {
34      /** Serial Version UID. */
35      private static final long serialVersionUID = 1L;
36  
37      /** The internal offset for the mapper. */
38      private final int internalOffset;
39  
40      /**
41       * Constructs a IntegerMapper with an offset of 0.
42       */
43      public IntegerMapper()
44      {
45          internalOffset = 0;
46      }
47  
48      /**
49       * Constructs a IntegerMapper with the given offset.
50       *
51       * @param offset the additional offset (0 based).
52       */
53      public IntegerMapper(int offset)
54      {
55          this.internalOffset = offset;
56      }
57  
58      public Integer processRow(
59                  ResultSet resultSet,
60                  int rowOffset)
61              throws TorqueException
62      {
63          try
64          {
65              Integer result = Integer.valueOf(
66                      resultSet.getInt(rowOffset + internalOffset + 1));
67              if (result == 0 && resultSet.wasNull())
68              {
69                  return null;
70              }
71              return result;
72          }
73          catch (SQLException e)
74          {
75              throw new TorqueException("Result could not be mapped to a Integer",
76                      e);
77          }
78      }
79  }