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