Coverage Report - com.jcabi.github.mock.MkIterable
 
Classes in this File Line Coverage Branch Coverage Complexity
MkIterable
71%
10/14
0%
0/22
1.5
MkIterable$1
75%
3/4
N/A
1.5
MkIterable$AjcClosure1
100%
1/1
N/A
1.5
MkIterable$Mapping
N/A
N/A
1.5
 
 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.mock;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.xml.XML;
 35  
 import java.io.IOException;
 36  
 import java.util.Iterator;
 37  
 import javax.validation.constraints.NotNull;
 38  
 import lombok.EqualsAndHashCode;
 39  
 import lombok.ToString;
 40  
 
 41  
 /**
 42  
  * Mock iterable.
 43  
  *
 44  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 45  
  * @version $Id$
 46  
  * @since 0.5
 47  
  */
 48  
 @Loggable(Loggable.DEBUG)
 49  0
 @ToString
 50  
 @Immutable
 51  0
 @EqualsAndHashCode(of = { "storage", "xpath", "mapping" })
 52  178
 final class MkIterable<T> implements Iterable<T> {
 53  
 
 54  
     /**
 55  
      * Storage to get XML from.
 56  
      */
 57  
     private final transient MkStorage storage;
 58  
 
 59  
     /**
 60  
      * XPath.
 61  
      */
 62  
     private final transient String xpath;
 63  
 
 64  
     /**
 65  
      * Mapping.
 66  
      */
 67  
     private final transient MkIterable.Mapping<T> mapping;
 68  
 
 69  
     /**
 70  
      * Public ctor.
 71  
      * @param stg Storage
 72  
      * @param path Path to search
 73  
      * @param map Mapping
 74  
      */
 75  
     MkIterable(@NotNull(message = "stg can't be NULL")
 76  
         final MkStorage stg,
 77  
         @NotNull(message = "path can't be NULL") final String path,
 78  
         @NotNull(message = "map can't be NULL") final MkIterable.Mapping<T> map
 79  111
     ) {
 80  111
         this.storage = stg;
 81  111
         this.xpath = path;
 82  111
         this.mapping = map;
 83  111
     }
 84  
 
 85  
     @Override
 86  
     @NotNull(message = "Iterator is never NULL")
 87  
     public Iterator<T> iterator() {
 88  
         final Iterator<XML> nodes;
 89  
         try {
 90  232
             nodes = this.storage.xml().nodes(this.xpath).iterator();
 91  0
         } catch (final IOException ex) {
 92  0
             throw new IllegalStateException(ex);
 93  116
         }
 94  116
         return new Iterator<T>() {
 95  
             @Override
 96  
             public boolean hasNext() {
 97  266
                 return nodes.hasNext();
 98  
             }
 99  
             @Override
 100  
             public T next() {
 101  178
                 return MkIterable.this.mapping.map(nodes.next());
 102  
             }
 103  
             @Override
 104  
             public void remove() {
 105  0
                 throw new UnsupportedOperationException("#remove()");
 106  
             }
 107  
         };
 108  
     }
 109  
 
 110  
     /**
 111  
      * Mapping.
 112  
      */
 113  
     @Immutable
 114  
     public interface Mapping<X> {
 115  
         /**
 116  
          * Map from XML to X.
 117  
          * @param xml The XML to get it from
 118  
          * @return X
 119  
          */
 120  
         X map(XML xml);
 121  
     }
 122  
 
 123  
 }