Element type
* @since 0.23
* @checkstyle AbstractClassNameCheck (500 lines)
+ * @todo #947:30min ListEnvelope should only delegates all the methods
+ * of List to the wrapped List. See IterableEnvelope for an example.
+ * If needed ListOf should have some methods that were previously here
+ * and implement List instead of extending ListEnvelope.
+ * Again see IterableOf for an example.
*/
@SuppressWarnings(
{
diff --git a/src/main/java/org/cactoos/scalar/Mapped.java b/src/main/java/org/cactoos/scalar/Mapped.java
new file mode 100644
index 0000000000..f4515eaa79
--- /dev/null
+++ b/src/main/java/org/cactoos/scalar/Mapped.java
@@ -0,0 +1,72 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017-2019 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.scalar;
+
+import java.io.IOException;
+import org.cactoos.Func;
+import org.cactoos.Scalar;
+
+/**
+ * {@link Scalar} that apply a {@link Func} to the result
+ * of another {@link Scalar}.
+ *
+ * There is no thread-safety guarantee.
+ *
+ *
This class implements {@link Scalar}, which throws a checked
+ * {@link IOException}. This may not be convenient in many cases. To make
+ * it more convenient and get rid of the checked exception you can
+ * use the {@link Unchecked} decorator.
+ *
+ * @param Type of wrapped scalar result
+ * @param Type of result
+ * @since 1.0.0
+ */
+public final class Mapped implements Scalar {
+
+ /**
+ * Original scalar.
+ */
+ private final Scalar origin;
+
+ /**
+ * Func.
+ */
+ private final Func func;
+
+ /**
+ * Ctor.
+ * @param func Fun to apply
+ * @param scalar Encapsulated scalar
+ */
+ public Mapped(final Func func, final Scalar scalar) {
+ this.origin = scalar;
+ this.func = func;
+ }
+
+ @Override
+ public U value() throws Exception {
+ return this.func.apply(this.origin.value());
+ }
+
+}
diff --git a/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java b/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java
deleted file mode 100644
index 03c5ebb977..0000000000
--- a/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- * The MIT License (MIT)
- *
- * Copyright (c) 2017-2019 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.iterable;
-
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import org.hamcrest.core.IsEqual;
-import org.hamcrest.core.IsNot;
-import org.junit.Test;
-import org.llorllale.cactoos.matchers.Assertion;
-
-/**
- * Test case for {@link IterableEnvelope}.
- *
- * @since 0.35
- * @checkstyle JavadocMethodCheck (500 lines)
- */
-public final class IterableEnvelopeTest {
-
- @Test(expected = UnsupportedOperationException.class)
- public void returnsIteratorWithUnsupportedRemove() {
- final IterableEnvelope list = new IterableEnvelope(
- () -> {
- final List inner = new LinkedList<>();
- inner.add("eleven");
- return inner;
- }
- ) { };
- final Iterator iterator = list.iterator();
- iterator.next();
- iterator.remove();
- }
-
- @Test
- public void notEqualsToObjectOfAnotherType() {
- new Assertion<>(
- "Must not equal to object of another type",
- new IterableOf<>(),
- new IsNot<>(new IsEqual<>(new Object()))
- ).affirm();
- }
-
- @Test
- public void notEqualsToIterableWithDifferentElements() {
- final IterableOf first = new IterableOf<>(1, 2);
- final IterableOf second = new IterableOf<>(1, 3);
- new Assertion<>(
- "Must not equal to Iterable with different elements",
- first,
- new IsNot<>(new IsEqual<>(second))
- ).affirm();
- }
-
- @Test
- public void isEqualToItself() {
- final IterableOf iterable = new IterableOf<>(1, 2);
- new Assertion<>(
- "Must be equal to itself",
- iterable,
- new IsEqual<>(iterable)
- ).affirm();
- }
-
- @Test
- public void isEqualToIterableWithTheSameElements() {
- final IterableOf iterable = new IterableOf<>(1, 2);
- new Assertion<>(
- "Must be equal to Iterable with the same elements",
- iterable,
- new IsEqual<>(new IterableOf<>(1, 2))
- ).affirm();
- }
-
- @Test
- public void equalToEmptyIterable() {
- final IterableOf iterable = new IterableOf<>();
- new Assertion<>(
- "Empty Iterable must be equal to empty Iterable",
- iterable,
- new IsEqual<>(new IterableOf<>())
- ).affirm();
- }
-
- @Test
- public void differentHashCode() {
- final IterableOf first = new IterableOf<>(1, 2);
- final IterableOf second = new IterableOf<>(2, 1);
- new Assertion<>(
- "Must have different hashCode for Iterables with different content",
- first.hashCode(),
- new IsNot<>(new IsEqual<>(second.hashCode()))
- ).affirm();
- }
-
- @Test
- public void equalHashCode() {
- final IterableOf iterable = new IterableOf<>(1, 2);
- new Assertion<>(
- "Must have equal hashCode for Iterables with equal content",
- iterable.hashCode(),
- new IsEqual<>(new IterableOf<>(1, 2).hashCode())
- ).affirm();
- }
-}
diff --git a/src/test/java/org/cactoos/iterable/IterableOfTest.java b/src/test/java/org/cactoos/iterable/IterableOfTest.java
index a9bdf1154e..54864721d9 100644
--- a/src/test/java/org/cactoos/iterable/IterableOfTest.java
+++ b/src/test/java/org/cactoos/iterable/IterableOfTest.java
@@ -32,7 +32,9 @@
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsIterableWithSize;
import org.hamcrest.core.IsEqual;
+import org.hamcrest.core.IsNot;
import org.junit.Test;
+import org.llorllale.cactoos.matchers.Assertion;
/**
* Test case for {@link IterableOf}.
@@ -132,4 +134,74 @@ public void reportTotalPagedLength() throws Exception {
)
);
}
+ @Test
+ public void notEqualsToObjectOfAnotherType() {
+ new Assertion<>(
+ "Must not equal to object of another type",
+ new IterableOf<>(),
+ new IsNot<>(new IsEqual<>(new Object()))
+ ).affirm();
+ }
+
+ @Test
+ public void notEqualsToIterableWithDifferentElements() {
+ final IterableOf first = new IterableOf<>(1, 2);
+ final IterableOf second = new IterableOf<>(1, 3);
+ new Assertion<>(
+ "Must not equal to Iterable with different elements",
+ first,
+ new IsNot<>(new IsEqual<>(second))
+ ).affirm();
+ }
+
+ @Test
+ public void isEqualToItself() {
+ final IterableOf iterable = new IterableOf<>(1, 2);
+ new Assertion<>(
+ "Must be equal to itself",
+ iterable,
+ new IsEqual<>(iterable)
+ ).affirm();
+ }
+
+ @Test
+ public void isEqualToIterableWithTheSameElements() {
+ final IterableOf iterable = new IterableOf<>(1, 2);
+ new Assertion<>(
+ "Must be equal to Iterable with the same elements",
+ iterable,
+ new IsEqual<>(new IterableOf<>(1, 2))
+ ).affirm();
+ }
+
+ @Test
+ public void equalToEmptyIterable() {
+ final IterableOf iterable = new IterableOf<>();
+ new Assertion<>(
+ "Empty Iterable must be equal to empty Iterable",
+ iterable,
+ new IsEqual<>(new IterableOf<>())
+ ).affirm();
+ }
+
+ @Test
+ public void differentHashCode() {
+ final IterableOf first = new IterableOf<>(1, 2);
+ final IterableOf second = new IterableOf<>(2, 1);
+ new Assertion<>(
+ "Must have different hashCode for Iterables with different content",
+ first.hashCode(),
+ new IsNot<>(new IsEqual<>(second.hashCode()))
+ ).affirm();
+ }
+
+ @Test
+ public void equalHashCode() {
+ final IterableOf iterable = new IterableOf<>(1, 2);
+ new Assertion<>(
+ "Must have equal hashCode for Iterables with equal content",
+ iterable.hashCode(),
+ new IsEqual<>(new IterableOf<>(1, 2).hashCode())
+ ).affirm();
+ }
}