StringTrimmedResultSet.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbutils.wrappers;

  18. import java.lang.reflect.InvocationHandler;
  19. import java.lang.reflect.Method;
  20. import java.sql.ResultSet;

  21. import org.apache.commons.dbutils.ProxyFactory;

  22. /**
  23.  * Wraps a {@code ResultSet} to trim strings returned by the
  24.  * {@code getString()} and {@code getObject()} methods.
  25.  *
  26.  * <p>
  27.  * Usage Example:
  28.  * This example shows how to decorate ResultSets so processing continues as
  29.  * normal but all Strings are trimmed before being returned from the
  30.  * {@code ResultSet}.
  31.  * </p>
  32.  *
  33.  * <pre>
  34.  * ResultSet resultSet = // somehow get a ResultSet;
  35.  *
  36.  * // Substitute wrapped ResultSet with additional behavior for real ResultSet
  37.  * resultSet = StringTrimmedResultSet.wrap(resultSet);
  38.  *
  39.  * // Pass wrapped ResultSet to processor
  40.  * List list = new BasicRowProcessor().toBeanList(resultSet);
  41.  * </pre>
  42.  */
  43. public class StringTrimmedResultSet implements InvocationHandler {

  44.     /**
  45.      * Wraps the {@code ResultSet} in an instance of this class.  This is
  46.      * equivalent to:
  47.      * <pre>
  48.      * ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(resultSet));
  49.      * </pre>
  50.      *
  51.      * @param resultSet The {@code ResultSet} to wrap.
  52.      * @return wrapped ResultSet
  53.      */
  54.     public static ResultSet wrap(final ResultSet resultSet) {
  55.         return ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(resultSet));
  56.     }

  57.     /**
  58.      * The wrapped result.
  59.      */
  60.     private final ResultSet resultSet;

  61.     /**
  62.      * Constructs a new instance of {@code StringTrimmedResultSet}
  63.      * to wrap the specified {@code ResultSet}.
  64.      * @param resultSet ResultSet to wrap
  65.      */
  66.     public StringTrimmedResultSet(final ResultSet resultSet) {
  67.         this.resultSet = resultSet;
  68.     }

  69.     /**
  70.      * Intercept calls to the {@code getString()} and
  71.      * {@code getObject()} methods and trim any Strings before they're
  72.      * returned.
  73.      *
  74.      * @see java.lang.reflect.InvocationHandler#invoke(Object, java.lang.reflect.Method, Object[])
  75.      * @param proxy Not used; all method calls go to the internal result set
  76.      * @param method The method to invoke on the result set
  77.      * @param args The arguments to pass to the result set
  78.      * @return string trimmed result
  79.      * @throws Throwable error
  80.      */
  81.     @Override
  82.     public Object invoke(final Object proxy, final Method method, final Object[] args)
  83.         throws Throwable {

  84.         Object result = method.invoke(this.resultSet, args);

  85.         if (result instanceof String
  86.                 && (method.getName().equals("getObject")
  87.                 || method.getName().equals("getString"))) {
  88.             result = ((String) result).trim();
  89.         }

  90.         return result;
  91.     }

  92. }