diff --git a/src/main/java/org/cactoos/iterable/Skipped.java b/src/main/java/org/cactoos/iterable/Skipped.java index 6aa5efb9e6..bddbaf0066 100644 --- a/src/main/java/org/cactoos/iterable/Skipped.java +++ b/src/main/java/org/cactoos/iterable/Skipped.java @@ -35,6 +35,16 @@ */ public final class Skipped extends IterableEnvelope { + /** + * Ctor. + * @param skip How many to skip + * @param src The underlying iterable + */ + @SafeVarargs + public Skipped(final int skip, final T... src) { + this(skip, new IterableOf<>(src)); + } + /** * Ctor. * @param skip Count skip elements diff --git a/src/main/java/org/cactoos/iterable/SolidIterable.java b/src/main/java/org/cactoos/iterable/SolidIterable.java new file mode 100644 index 0000000000..105132b6c0 --- /dev/null +++ b/src/main/java/org/cactoos/iterable/SolidIterable.java @@ -0,0 +1,55 @@ +/** + * 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.iterable; + +/** + * An {@link Iterable} that is both synchronized and sticky. + * + *

Objects of this class are thread-safe.

+ * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.24 + */ +public final class SolidIterable extends IterableEnvelope { + + /** + * Ctor. + * @param src The underlying iterable + */ + @SafeVarargs + public SolidIterable(final X... src) { + this(new IterableOf<>(src)); + } + + /** + * Ctor. + * @param iterable The iterable + */ + public SolidIterable(final Iterable iterable) { + super(() -> new SyncIterable<>(new StickyIterable<>(iterable))); + } + +} diff --git a/src/main/java/org/cactoos/iterable/StickyIterable.java b/src/main/java/org/cactoos/iterable/StickyIterable.java index d1d28488ff..3a6ec8c9c4 100644 --- a/src/main/java/org/cactoos/iterable/StickyIterable.java +++ b/src/main/java/org/cactoos/iterable/StickyIterable.java @@ -39,6 +39,15 @@ */ public final class StickyIterable extends IterableEnvelope { + /** + * Ctor. + * @param src The underlying iterable + */ + @SafeVarargs + public StickyIterable(final X... src) { + this(new IterableOf<>(src)); + } + /** * Ctor. * @param iterable The iterable diff --git a/src/main/java/org/cactoos/iterable/SyncIterable.java b/src/main/java/org/cactoos/iterable/SyncIterable.java index 6f159e8ac2..85568df84e 100644 --- a/src/main/java/org/cactoos/iterable/SyncIterable.java +++ b/src/main/java/org/cactoos/iterable/SyncIterable.java @@ -44,6 +44,15 @@ */ public final class SyncIterable extends IterableEnvelope { + /** + * Ctor. + * @param src The underlying iterable + */ + @SafeVarargs + public SyncIterable(final X... src) { + this(new IterableOf<>(src)); + } + /** * Ctor. * @param iterable The iterable diff --git a/src/main/java/org/cactoos/list/SolidList.java b/src/main/java/org/cactoos/list/SolidList.java new file mode 100644 index 0000000000..9066606771 --- /dev/null +++ b/src/main/java/org/cactoos/list/SolidList.java @@ -0,0 +1,76 @@ +/** + * 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.Collection; +import java.util.Iterator; +import org.cactoos.iterable.IterableOf; + +/** + * A {@link java.util.List} that is both synchronized and sticky. + * + *

Objects of this class are thread-safe.

+ * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.24 + */ +public final class SolidList extends ListEnvelope { + + /** + * Ctor. + * @param items The array + */ + @SafeVarargs + public SolidList(final X... items) { + this(new IterableOf<>(items)); + } + + /** + * Ctor. + * @param items The array + */ + public SolidList(final Iterable items) { + this(new ListOf<>(items)); + } + + /** + * Ctor. + * @param items The array + * @since 0.21 + */ + public SolidList(final Iterator items) { + this(new ListOf<>(items)); + } + + /** + * Ctor. + * @param list The iterable + */ + public SolidList(final Collection list) { + super(() -> new SyncList<>(new StickyList<>(list))); + } + +} diff --git a/src/main/java/org/cactoos/map/SolidMap.java b/src/main/java/org/cactoos/map/SolidMap.java new file mode 100644 index 0000000000..23ee400fae --- /dev/null +++ b/src/main/java/org/cactoos/map/SolidMap.java @@ -0,0 +1,151 @@ +/** + * 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.map; + +import java.util.Iterator; +import java.util.Map; +import org.cactoos.Func; +import org.cactoos.iterable.IterableOf; +import org.cactoos.iterable.Mapped; + +/** + * A {@link Map} that is both synchronized and sticky. + * + *

Objects of this class are thread-safe.

+ * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of key + * @param Type of value + * @since 0.24 + */ +public final class SolidMap extends MapEnvelope { + + /** + * Ctor. + * @param list List of entries + */ + @SafeVarargs + public SolidMap(final Map.Entry... list) { + this(new IterableOf<>(list)); + } + + /** + * Ctor. + * @param map The map to extend + * @param list List of entries + */ + @SafeVarargs + public SolidMap(final Map map, final Map.Entry... list) { + this(map, new IterableOf<>(list)); + } + + /** + * Ctor. + * @param map The map to extend + * @param list List of items + * @param key Func to create key + * @param value Func to create value + * @param Type of items in the list + * @checkstyle ParameterNumberCheck (5 lines) + */ + public SolidMap(final Map map, + final Iterable list, final Func key, + final Func value) { + this( + map, list, + item -> new MapEntry<>(key.apply(item), value.apply(item)) + ); + } + + /** + * Ctor. + * @param list List of items + * @param key Func to create key + * @param value Func to create value + * @param Type of items in the list + */ + public SolidMap(final Iterable list, final Func key, + final Func value) { + this(list, item -> new MapEntry<>(key.apply(item), value.apply(item))); + } + + /** + * Ctor. + * @param list List of items + * @param entry Func to create entry + * @param Type of items in the list + */ + public SolidMap(final Iterable list, + final Func> entry) { + this(new Mapped<>(entry, list)); + } + + /** + * Ctor. + * @param map The map to extend + * @param list List of items + * @param entry Func to create entry + * @param Type of items in the list + */ + public SolidMap(final Map map, final Iterable list, + final Func> entry) { + this(map, new Mapped<>(entry, list)); + } + + /** + * Ctor. + * @param list Entries for the entries + */ + public SolidMap(final Iterable> list) { + this(new MapOf<>(list)); + } + + /** + * Ctor. + * @param list Entries for the entries + */ + public SolidMap(final Iterator> list) { + this(() -> list); + } + + /** + * Ctor. + * @param map Pre-existing map we want to extend + * @param list Entries for the entries + */ + public SolidMap(final Map map, + final Iterable> list) { + this(new MapOf<>(map, list)); + } + + /** + * Ctor. + * @param map The map + */ + public SolidMap(final Map map) { + super(() -> new SyncMap<>(new StickyMap<>(map))); + } + +} diff --git a/src/test/java/org/cactoos/list/SolidListTest.java b/src/test/java/org/cactoos/list/SolidListTest.java new file mode 100644 index 0000000000..af59f04527 --- /dev/null +++ b/src/test/java/org/cactoos/list/SolidListTest.java @@ -0,0 +1,48 @@ +/** + * 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 org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link SolidList}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @author Mehmet Yildirim (memoyil@gmail.com) + * @version $Id$ + * @since 0.24 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class SolidListTest { + + @Test + public void behavesAsCollection() throws Exception { + MatcherAssert.assertThat( + "Can't behave as a list", + new SolidList<>(1, 0, -1, -1, 2), + new BehavesAsList<>(0) + ); + } + +} diff --git a/src/test/java/org/cactoos/map/SolidMapTest.java b/src/test/java/org/cactoos/map/SolidMapTest.java new file mode 100644 index 0000000000..511237eea0 --- /dev/null +++ b/src/test/java/org/cactoos/map/SolidMapTest.java @@ -0,0 +1,52 @@ +/** + * 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.map; + +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link SolidMap}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.24 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class SolidMapTest { + + @Test + public void behavesAsMap() { + MatcherAssert.assertThat( + "Can't behave as a map", + new SolidMap( + new MapEntry<>(0, -1), + new MapEntry<>(1, 1) + ), + new BehavesAsMap<>(0, 1) + ); + } + +}