Coverage Report - com.jcabi.jdbc.ListOutcome
 
Classes in this File Line Coverage Branch Coverage Complexity
ListOutcome
90%
10/11
14%
2/14
1.333
ListOutcome$AjcClosure1
100%
1/1
N/A
1.333
ListOutcome$Mapping
N/A
N/A
1.333
 
 1  2
 /**
 2  
  * Copyright (c) 2012-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.jdbc;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import java.sql.ResultSet;
 35  
 import java.sql.SQLException;
 36  
 import java.sql.Statement;
 37  
 import java.util.LinkedList;
 38  
 import java.util.List;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import lombok.ToString;
 41  
 
 42  
 /**
 43  
  * Outcome that returns a list.
 44  
  *
 45  
  * <p>Use it when you need a full collection:
 46  
  *
 47  
  * <pre> Collection&lgt;User&gt; users = new JdbcSession(source)
 48  
  *   .sql("SELECT * FROM user")
 49  
  *   .select(
 50  
  *     new ListOutcome&lt;User&gt;(
 51  
  *       new ListOutcome.Mapping&lt;User&gt;() {
 52  
  *         &#64;Override
 53  
  *         public User map(final ResultSet rset) throws SQLException {
 54  
  *           return new User.Simple(rset.getLong(1), rset.getString(2));
 55  
  *         }
 56  
  *       }
 57  
  *     )
 58  
  *   );</pre>
 59  
  *
 60  
  * @author Yegor Bugayenko (yegor@teamed.io)
 61  
  * @version $Id: 5e3338db07e1cb57587823d5971f52d7a5a60dc4 $
 62  
  * @since 0.13
 63  
  * @param <T> Type of items
 64  
  */
 65  1
 @Immutable
 66  1
 @ToString
 67  0
 @EqualsAndHashCode(of = "mapping")
 68  
 public final class ListOutcome<T> implements Outcome<List<T>> {
 69  
 
 70  
     /**
 71  
      * Mapping.
 72  
      */
 73  
     private final transient ListOutcome.Mapping<T> mapping;
 74  
 
 75  
     /**
 76  
      * Public ctor.
 77  
      * @param mpg Mapping
 78  
      */
 79  1
     public ListOutcome(final ListOutcome.Mapping<T> mpg) {
 80  1
         this.mapping = mpg;
 81  1
     }
 82  
 
 83  
     @Override
 84  
     @Loggable(Loggable.DEBUG)
 85  
     public List<T> handle(final ResultSet rset, final Statement stmt)
 86  
         throws SQLException {
 87  2
         final List<T> result = new LinkedList<T>();
 88  3
         while (rset.next()) {
 89  2
             result.add(this.mapping.map(rset));
 90  
         }
 91  1
         return result;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Mapping.
 96  
      */
 97  
     @Immutable
 98  
     public interface Mapping<T> {
 99  
         /**
 100  
          * Map.
 101  
          * @param rset Result set
 102  
          * @return Object
 103  
          * @throws SQLException If fails
 104  
          */
 105  
         T map(ResultSet rset) throws SQLException;
 106  
     }
 107  
 
 108  
 }