-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into new_ctor_for_Iter…
…ableAsList
- Loading branch information
Showing
10 changed files
with
630 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,22 +24,22 @@ | |
package org.cactoos.list; | ||
|
||
import java.io.IOException; | ||
import java.util.Iterator; | ||
import org.cactoos.Func; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.text.FormattedText; | ||
|
||
/** | ||
* First element in {@link Iterable} or fallback value if iterable is empty. | ||
* Element from position in {@link Iterable} | ||
* or fallback value if iterable hasn't this position. | ||
* | ||
* <p>There is no thread-safety guarantee. | ||
* | ||
* @author Kirill ([email protected]) | ||
* @version $Id$ | ||
* @param <T> Scalar type | ||
* @since 0.1 | ||
* @since 0.7 | ||
*/ | ||
public final class FirstOf<T> implements Scalar<T> { | ||
public final class ItemOfIterable<T> implements Scalar<T> { | ||
|
||
/** | ||
* Source iterable. | ||
|
@@ -51,12 +51,17 @@ public final class FirstOf<T> implements Scalar<T> { | |
*/ | ||
private final Func<Iterable<T>, T> fbk; | ||
|
||
/** | ||
* Position. | ||
*/ | ||
private final int pos; | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterable | ||
*/ | ||
public FirstOf(final Iterable<T> src) { | ||
public ItemOfIterable(final Iterable<T> src) { | ||
this( | ||
src, | ||
itr -> { | ||
|
@@ -74,7 +79,7 @@ public FirstOf(final Iterable<T> src) { | |
* @param src Iterable | ||
* @param fbk Fallback value | ||
*/ | ||
public FirstOf(final Iterable<T> src, final T fbk) { | ||
public ItemOfIterable(final Iterable<T> src, final T fbk) { | ||
this(src, itr -> fbk); | ||
} | ||
|
||
|
@@ -84,20 +89,58 @@ public FirstOf(final Iterable<T> src, final T fbk) { | |
* @param src Iterable | ||
* @param fbk Fallback value | ||
*/ | ||
public FirstOf(final Iterable<T> src, final Func<Iterable<T>, T> fbk) { | ||
public ItemOfIterable( | ||
final Iterable<T> src, | ||
final Func<Iterable<T>, T> fbk | ||
) { | ||
this(src, 0, fbk); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterable | ||
* @param pos Position | ||
*/ | ||
public ItemOfIterable(final Iterable<T> src, final int pos) { | ||
this( | ||
src, | ||
pos, | ||
itr -> { | ||
throw new IOException( | ||
new FormattedText( | ||
"Iterable %s hasn't element from position %d", | ||
itr, | ||
pos | ||
).asString() | ||
); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterable | ||
* @param pos Position | ||
* @param fbk Fallback value | ||
*/ | ||
public ItemOfIterable( | ||
final Iterable<T> src, | ||
final int pos, | ||
final Func<Iterable<T>, T> fbk | ||
) { | ||
this.pos = pos; | ||
this.src = src; | ||
this.fbk = fbk; | ||
} | ||
|
||
@Override | ||
public T asValue() throws Exception { | ||
final Iterator<T> itr = this.src.iterator(); | ||
final T ret; | ||
if (itr.hasNext()) { | ||
ret = itr.next(); | ||
} else { | ||
ret = this.fbk.apply(this.src); | ||
} | ||
return ret; | ||
return new ItemOfIterator<>( | ||
this.src.iterator(), | ||
this.pos, | ||
this.fbk | ||
).asValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.cactoos.list; | ||
|
||
import java.io.IOException; | ||
import java.util.Iterator; | ||
import org.cactoos.Func; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.text.FormattedText; | ||
|
||
/** | ||
* Element from position in {@link Iterator} | ||
* or fallback value if iterator hasn't this position. | ||
* | ||
* <p>There is no thread-safety guarantee. | ||
* | ||
* @author Ilia Rogozhin ([email protected]) | ||
* @version $Id$ | ||
* @param <T> Scalar type | ||
* @since 0.7 | ||
*/ | ||
public final class ItemOfIterator<T> implements Scalar<T> { | ||
|
||
/** | ||
* Source iterator. | ||
*/ | ||
private final Iterator<T> src; | ||
|
||
/** | ||
* Fallback value. | ||
*/ | ||
private final Func<Iterable<T>, T> fbk; | ||
|
||
/** | ||
* Position. | ||
*/ | ||
private final int pos; | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterator | ||
*/ | ||
public ItemOfIterator(final Iterator<T> src) { | ||
this( | ||
src, | ||
itr -> { | ||
throw new IOException( | ||
new FormattedText("Iterator %s is empty", itr.iterator()) | ||
.asString() | ||
); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterator | ||
* @param fbk Fallback value | ||
*/ | ||
public ItemOfIterator(final Iterator<T> src, final T fbk) { | ||
this(src, itr -> fbk); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterator | ||
* @param fbk Fallback value | ||
*/ | ||
public ItemOfIterator( | ||
final Iterator<T> src, | ||
final Func<Iterable<T>, T> fbk | ||
) { | ||
this(src, 0, fbk); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterator | ||
* @param pos Position | ||
*/ | ||
public ItemOfIterator(final Iterator<T> src, final int pos) { | ||
this( | ||
src, | ||
pos, | ||
itr -> { | ||
throw new IOException( | ||
new FormattedText( | ||
"Iterator %s hasn't element from position %d", | ||
itr.iterator(), | ||
pos | ||
).asString() | ||
); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param src Iterator | ||
* @param pos Position | ||
* @param fbk Fallback value | ||
*/ | ||
public ItemOfIterator( | ||
final Iterator<T> src, | ||
final int pos, | ||
final Func<Iterable<T>, T> fbk | ||
) { | ||
this.pos = pos; | ||
this.src = src; | ||
this.fbk = fbk; | ||
} | ||
|
||
@Override | ||
public T asValue() throws Exception { | ||
if (this.pos < 0) { | ||
throw new IOException( | ||
new FormattedText( | ||
"Position must be nonnegative! But position = %d", | ||
this.pos | ||
).asString() | ||
); | ||
} | ||
final T ret; | ||
int cur; | ||
for (cur = 0; cur < this.pos && this.src.hasNext(); ++cur) { | ||
this.src.next(); | ||
} | ||
if (cur == this.pos && this.src.hasNext()) { | ||
ret = this.src.next(); | ||
} else { | ||
ret = this.fbk.apply(() -> this.src); | ||
} | ||
return ret; | ||
} | ||
} |
Oops, something went wrong.