Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into new_ctor_for_Iter…
Browse files Browse the repository at this point in the history
…ableAsList
  • Loading branch information
akryvtsun committed Jun 16, 2017
2 parents b6c347d + 24582cf commit 16ab3d7
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 29 deletions.
18 changes: 17 additions & 1 deletion src/main/java/org/cactoos/func/ProcAsFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,33 @@ public final class ProcAsFunc<X, Y> implements Func<X, Y> {
*/
private final Proc<X> proc;

/**
* The result.
*/
private final Y result;

/**
* Ctor.
* @param prc The proc
*/
public ProcAsFunc(final Proc<X> prc) {
this(prc, null);
}

/**
* Ctor.
* @param prc The proc
* @param rslt Result to return
* @since 0.7
*/
public ProcAsFunc(final Proc<X> prc, final Y rslt) {
this.proc = prc;
this.result = rslt;
}

@Override
public Y apply(final X input) throws Exception {
this.proc.exec(input);
return null;
return this.result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 -> {
Expand All @@ -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);
}

Expand All @@ -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();
}
}
161 changes: 161 additions & 0 deletions src/main/java/org/cactoos/list/ItemOfIterator.java
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;
}
}
Loading

0 comments on commit 16ab3d7

Please sign in to comment.