Skip to content

Commit

Permalink
#18: DeadProc, ConstFunc, IterableAsBooleans
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 25, 2017
1 parent efbacbf commit e35f61a
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/main/java/org/cactoos/func/ConstFunc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* 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.func;

import org.cactoos.Func;

/**
* Func that always returns the same result
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <X> Type of input
* @param <Y> Type of output
* @since 0.1
*/
public final class ConstFunc<X, Y> implements Func<X, Y> {

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

/**
* Ctor.
* @param rslt What to return
*/
public ConstFunc(final Y rslt) {
this.result = rslt;
}

@Override
public Y apply(final X input) {
return this.result;
}

}
45 changes: 45 additions & 0 deletions src/main/java/org/cactoos/func/DeadProc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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.func;

import org.cactoos.Proc;

/**
* Proc that does nothing.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <T> Type of items
* @since 0.1
*/
public final class DeadProc<T> implements Proc<T> {

@Override
public void apply(final T input) {
// nothing to do
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/func/ProcAsFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.cactoos.Proc;

/**
* Consumer that is a func that returns {@code boolean}.
* Proc that is a {@link Func} that returns {@code boolean}.
*
* <p>You may need this class when you iterate a collection, for example:</p>
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/cactoos/list/AllOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
* )
* ).asValue();</pre>
*
* <p>Or even more compact, through {@link IterableAsBooleans}:</p>
*
* <pre> new AllOf(
* new IterableAsBooleans&lt;String&gt;(
* Arrays.asList("hello", "world"),
* i -> System.out.println(i)
* )
* ).asValue();</pre>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/org/cactoos/list/IterableAsBooleans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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.util.Iterator;
import org.cactoos.Proc;
import org.cactoos.func.ProcAsFunc;

/**
* Iterable into booleans.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <X> Type of source item
* @since 0.1
*/
public final class IterableAsBooleans<X> implements Iterable<Boolean> {

/**
* Iterable.
*/
private final Iterable<X> iterable;

/**
* Proc.
*/
private final Proc<X> proc;

/**
* Ctor.
* @param src Source iterable
* @param prc Func
*/
public IterableAsBooleans(final Iterable<X> src, final Proc<X> prc) {
this.iterable = src;
this.proc = prc;
}

@Override
public Iterator<Boolean> iterator() {
return new TransformedIterator<>(
this.iterable.iterator(),
new ProcAsFunc<X>(this.proc)
);
}

}
21 changes: 21 additions & 0 deletions src/test/java/org/cactoos/list/AllOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.list;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.cactoos.func.ProcAsFunc;
Expand Down Expand Up @@ -58,4 +59,24 @@ public void iteratesList() {
);
}

/**
* AllOf can test all items in the list.
*/
@Test
public void iteratesEmptyList() {
final List<String> list = new LinkedList<>();
MatcherAssert.assertThat(
new AllOf(
new IterableAsBooleans<String>(
Collections.emptyList(),
list::add
)
).asValue(),
Matchers.allOf(
Matchers.equalTo(true),
Matchers.equalTo(list.isEmpty())
)
);
}

}

0 comments on commit e35f61a

Please sign in to comment.