Coverage Report - com.jcabi.github.Smarts
 
Classes in this File Line Coverage Branch Coverage Complexity
Smarts
41%
10/24
8%
1/12
2.625
Smarts$1
60%
3/5
N/A
2.625
 
 1  2
 /**
 2  
  * Copyright (c) 2013-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.github;
 31  
 
 32  
 import com.jcabi.log.Logger;
 33  
 import java.lang.reflect.Constructor;
 34  
 import java.lang.reflect.InvocationTargetException;
 35  
 import java.util.Iterator;
 36  
 import javax.validation.constraints.NotNull;
 37  
 import lombok.EqualsAndHashCode;
 38  
 
 39  
 /**
 40  
  * Smart items.
 41  
  *
 42  
  * <p>This class should be used as a decorator for an iterable of objects, for
 43  
  * example:
 44  
  *
 45  
  * <pre> Iterable&lt;Issue.Smart&gt; issues = new Smarts&lt;Issue.Smart&gt;(
 46  
  *   repo.issues().iterate(
 47  
  *     new HashMap&lt;String, String&gt;()
 48  
  *   )
 49  
  * );
 50  
  * for (Issue.Smart issue : issues) {
 51  
  *     System.out.println("state is: " + issue.state());
 52  
  * }</pre>
 53  
  *
 54  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 55  
  * @version $Id$
 56  
  * @since 0.5
 57  
  * @param <T> Type of iterable objects
 58  
  */
 59  0
 @EqualsAndHashCode(of = "origin")
 60  3
 public final class Smarts<T> implements Iterable<T> {
 61  
 
 62  
     /**
 63  
      * Original iterable.
 64  
      */
 65  
     private final transient Iterable<?> origin;
 66  
 
 67  
     /**
 68  
      * Public ctor.
 69  
      * @param items Items original
 70  
      */
 71  
     public Smarts(
 72  
         @NotNull(message = "items can't be NULL") final Iterable<?> items
 73  3
     ) {
 74  3
         this.origin = items;
 75  3
     }
 76  
 
 77  
     @Override
 78  
     @NotNull(message = "toString is never NULL")
 79  
     public String toString() {
 80  0
         return this.origin.toString();
 81  
     }
 82  
 
 83  
     @Override
 84  
     @NotNull(message = "Iterator is never NULL")
 85  
     public Iterator<T> iterator() {
 86  3
         final Iterator<?> iterator = this.origin.iterator();
 87  3
         return new Iterator<T>() {
 88  
             @Override
 89  
             public boolean hasNext() {
 90  2
                 return iterator.hasNext();
 91  
             }
 92  
             @Override
 93  
             public T next() {
 94  3
                 return Smarts.<T>wrap(iterator.next());
 95  
             }
 96  
             @Override
 97  
             public void remove() {
 98  0
                 iterator.remove();
 99  0
             }
 100  
         };
 101  
     }
 102  
 
 103  
     /**
 104  
      * Wrap an object, and make a "smart" decorator.
 105  
      * @param object Object to wrap
 106  
      * @return Decorator
 107  
      * @param <X> Type of result
 108  
      */
 109  
     @SuppressWarnings("unchecked")
 110  
     private static <X> X wrap(final Object object) {
 111  
         try {
 112  3
             return (X) Smarts.type(object).newInstance(object);
 113  0
         } catch (final InvocationTargetException ex) {
 114  0
             throw new IllegalStateException(ex);
 115  0
         } catch (final InstantiationException ex) {
 116  0
             throw new IllegalStateException(ex);
 117  0
         } catch (final IllegalAccessException ex) {
 118  0
             throw new IllegalStateException(ex);
 119  
         }
 120  
     }
 121  
 
 122  
     /**
 123  
      * Find "smart" class for the object.
 124  
      * @param object Object to wrap
 125  
      * @return Class to use
 126  
      */
 127  
     private static Constructor<?> type(final Object object) {
 128  3
         for (final Class<?> iface : object.getClass().getInterfaces()) {
 129  
             try {
 130  3
                 return Class.forName(
 131  
                     String.format("%s$Smart", iface.getName())
 132  
                 ).getDeclaredConstructor(iface);
 133  0
             } catch (final ClassNotFoundException ex) {
 134  0
                 Logger.debug(Smarts.class, "%s: %s", iface.getName(), ex);
 135  0
             } catch (final NoSuchMethodException ex) {
 136  0
                 throw new IllegalStateException(ex);
 137  0
             }
 138  
         }
 139  0
         throw new IllegalStateException(
 140  
             String.format(
 141  
                 "can't find Smart decorator for %s",
 142  
                 object.getClass().getName()
 143  
             )
 144  
         );
 145  
     }
 146  
 
 147  
 }