Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MkIterable |
|
| 1.5;1.5 | ||||
MkIterable$1 |
|
| 1.5;1.5 | ||||
MkIterable$AjcClosure1 |
|
| 1.5;1.5 | ||||
MkIterable$Mapping |
|
| 1.5;1.5 |
1 | 275 | /** |
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.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 lombok.EqualsAndHashCode; | |
38 | import lombok.ToString; | |
39 | ||
40 | /** | |
41 | * Mock iterable. | |
42 | * | |
43 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
44 | * @version $Id: 595d0b244121c6f9586bb85e3de90a6e289e390f $ | |
45 | * @since 0.5 | |
46 | */ | |
47 | @Loggable(Loggable.DEBUG) | |
48 | 0 | @ToString |
49 | @Immutable | |
50 | 0 | @EqualsAndHashCode(of = { "storage", "xpath", "mapping" }) |
51 | 194 | final class MkIterable<T> implements Iterable<T> { |
52 | ||
53 | /** | |
54 | * Storage to get XML from. | |
55 | */ | |
56 | private final transient MkStorage storage; | |
57 | ||
58 | /** | |
59 | * XPath. | |
60 | */ | |
61 | private final transient String xpath; | |
62 | ||
63 | /** | |
64 | * Mapping. | |
65 | */ | |
66 | private final transient MkIterable.Mapping<T> mapping; | |
67 | ||
68 | /** | |
69 | * Public ctor. | |
70 | * @param stg Storage | |
71 | * @param path Path to search | |
72 | * @param map Mapping | |
73 | */ | |
74 | MkIterable(final MkStorage stg, | |
75 | final String path, | |
76 | final MkIterable.Mapping<T> map | |
77 | 133 | ) { |
78 | 133 | this.storage = stg; |
79 | 133 | this.xpath = path; |
80 | 133 | this.mapping = map; |
81 | 133 | } |
82 | ||
83 | @Override | |
84 | public Iterator<T> iterator() { | |
85 | final Iterator<XML> nodes; | |
86 | try { | |
87 | 275 | nodes = this.storage.xml().nodes(this.xpath).iterator(); |
88 | 0 | } catch (final IOException ex) { |
89 | 0 | throw new IllegalStateException(ex); |
90 | 138 | } |
91 | 138 | return new Iterator<T>() { |
92 | @Override | |
93 | public boolean hasNext() { | |
94 | 291 | return nodes.hasNext(); |
95 | } | |
96 | @Override | |
97 | public T next() { | |
98 | 194 | return MkIterable.this.mapping.map(nodes.next()); |
99 | } | |
100 | @Override | |
101 | public void remove() { | |
102 | 0 | throw new UnsupportedOperationException("#remove()"); |
103 | } | |
104 | }; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Mapping. | |
109 | */ | |
110 | @Immutable | |
111 | public interface Mapping<X> { | |
112 | /** | |
113 | * Map from XML to X. | |
114 | * @param xml The XML to get it from | |
115 | * @return X | |
116 | */ | |
117 | X map(XML xml); | |
118 | } | |
119 | ||
120 | } |