From b99858db61a2d49b741cd8d1dd6ab7d6349b8d5a Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Sat, 10 Jun 2017 18:37:49 +0200 Subject: [PATCH 01/13] Refactored IterableAsList to remove unnecessary call to size() and remove not working cache no more calls to size() if not needed. Modifications in the underlying Iterable would lead to inconsistent cache. Removed cache completely. Caches do not work for modifyable Iterables. --- README.md | 1 + .../java/org/cactoos/list/IterableAsList.java | 49 ++++++------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 9111c559a7..ec628cdba4 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,7 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static - [Andriy Kryvtsun](https://github.com/englishman) - [Vseslav Sekorin](https://github.com/VsSekorin) - [Andrey Valyaev](https://github.com/DronMDF) + - [Tim Hinkes](https://github.com/timmeey) [Blog](https://blog.timmeey.de) ## License (MIT) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index a76c176eb9..1c5d6426af 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -24,7 +24,6 @@ package org.cactoos.list; import java.util.AbstractList; -import java.util.ArrayList; import java.util.List; import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; @@ -48,11 +47,6 @@ public final class IterableAsList extends AbstractList { */ private final Iterable source; - /** - * Cache for source. - */ - private final List cache; - /** * Iterable length. */ @@ -66,7 +60,6 @@ public final class IterableAsList extends AbstractList { public IterableAsList(final Iterable iterable) { super(); this.source = iterable; - this.cache = new ArrayList<>(0); this.length = new UncheckedScalar<>( new StickyScalar<>( new LengthOfIterable(iterable) @@ -76,19 +69,23 @@ public IterableAsList(final Iterable iterable) { @Override public T get(final int index) { - if (index < 0 || index >= this.size()) { - throw new IndexOutOfBoundsException( - new UncheckedText( - new FormattedText( - "index=%d, bounds=[%d; %d]", - index, - 0, - this.size() - ) - ).asString() - ); + int position = 0; + for (final T elem : this.source) { + if (position == index) { + return elem; + } + position += 1; } - return this.cachedItem(index); + throw new IndexOutOfBoundsException( + new UncheckedText( + new FormattedText( + "index=%d, bounds=[%d; %d]", + index, + 0, + this.size() + ) + ).asString() + ); } @Override @@ -96,18 +93,4 @@ public int size() { return this.length.asValue(); } - /** - * Find item in cache by index. - * - * @param index Item index - * @return Cached item - */ - private T cachedItem(final int index) { - if (this.cache.size() != this.size()) { - for (final T item : this.source) { - this.cache.add(item); - } - } - return this.cache.get(index); - } } From da64c705b53f3e92c378d5ad2a204692cc52a243 Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Sat, 10 Jun 2017 18:54:20 +0200 Subject: [PATCH 02/13] Switched to non Cached size Since size() is not called all the time, and the premise is a mutable iterable, the size() can not be cached. --- src/main/java/org/cactoos/list/IterableAsList.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 1c5d6426af..8dd9c642d1 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -25,7 +25,6 @@ import java.util.AbstractList; import java.util.List; -import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; @@ -61,9 +60,7 @@ public IterableAsList(final Iterable iterable) { super(); this.source = iterable; this.length = new UncheckedScalar<>( - new StickyScalar<>( - new LengthOfIterable(iterable) - ) + new LengthOfIterable(iterable) ); } From f9593621ba750800a00b62e219cdd1e07cd65817 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 09:34:01 +0300 Subject: [PATCH 03/13] #116: IterableAsMap --- .../java/org/cactoos/map/IterableAsMap.java | 148 ++++++++++++++++++ .../java/org/cactoos/map/package-info.java | 32 ++++ .../org/cactoos/map/IterableAsMapTest.java | 56 +++++++ .../java/org/cactoos/map/package-info.java | 32 ++++ 4 files changed, 268 insertions(+) create mode 100644 src/main/java/org/cactoos/map/IterableAsMap.java create mode 100644 src/main/java/org/cactoos/map/package-info.java create mode 100644 src/test/java/org/cactoos/map/IterableAsMapTest.java create mode 100644 src/test/java/org/cactoos/map/package-info.java diff --git a/src/main/java/org/cactoos/map/IterableAsMap.java b/src/main/java/org/cactoos/map/IterableAsMap.java new file mode 100644 index 0000000000..17e48e418d --- /dev/null +++ b/src/main/java/org/cactoos/map/IterableAsMap.java @@ -0,0 +1,148 @@ +/** + * 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.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import org.cactoos.func.StickyScalar; +import org.cactoos.func.UncheckedScalar; +import org.cactoos.list.ArrayAsIterable; + +/** + * Iterable as {@link Map}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of key + * @param Type of value + * @since 0.4 + */ +public final class IterableAsMap implements Map { + + /** + * The map. + */ + private final UncheckedScalar> map; + + /** + * Ctor. + * @param entries Entries for the map + */ + @SafeVarargs + @SuppressWarnings("varargs") + public IterableAsMap(final Map.Entry... entries) { + this(new ArrayAsIterable<>(entries)); + } + + /** + * Ctor. + * @param entries Entries for the map + */ + public IterableAsMap(final Iterable> entries) { + this.map = new UncheckedScalar<>( + new StickyScalar<>( + () -> { + final Map temp = new HashMap<>(0); + for (final Map.Entry entry : entries) { + temp.put(entry.getKey(), entry.getValue()); + } + return temp; + } + ) + ); + } + + @Override + public int size() { + return this.map.asValue().size(); + } + + @Override + public boolean isEmpty() { + return this.map.asValue().isEmpty(); + } + + @Override + public boolean containsKey(final Object key) { + return this.map.asValue().containsKey(key); + } + + @Override + public boolean containsValue(final Object value) { + return this.map.asValue().containsValue(value); + } + + @Override + public Y get(final Object key) { + return this.map.asValue().get(key); + } + + @Override + public Y put(final X key, final Y value) { + throw new UnsupportedOperationException( + "#put() is not supported" + ); + } + + @Override + public Y remove(final Object key) { + throw new UnsupportedOperationException( + "#remove() is not supported" + ); + } + + @Override + public void putAll(final Map entries) { + throw new UnsupportedOperationException( + "#putAll() is not supported" + ); + } + + @Override + public void clear() { + throw new UnsupportedOperationException( + "#clear() is not supported" + ); + } + + @Override + public Set keySet() { + return this.map.asValue().keySet(); + } + + @Override + public Collection values() { + return this.map.asValue().values(); + } + + @Override + public Set> entrySet() { + return this.map.asValue().entrySet(); + } + +} diff --git a/src/main/java/org/cactoos/map/package-info.java b/src/main/java/org/cactoos/map/package-info.java new file mode 100644 index 0000000000..cd9f22d554 --- /dev/null +++ b/src/main/java/org/cactoos/map/package-info.java @@ -0,0 +1,32 @@ +/** + * 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. + */ + +/** + * Maps. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + */ +package org.cactoos.map; diff --git a/src/test/java/org/cactoos/map/IterableAsMapTest.java b/src/test/java/org/cactoos/map/IterableAsMapTest.java new file mode 100644 index 0000000000..cdcad047ea --- /dev/null +++ b/src/test/java/org/cactoos/map/IterableAsMapTest.java @@ -0,0 +1,56 @@ +/** + * 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.AbstractMap; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link IterableAsMap}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class IterableAsMapTest { + + @Test + @SuppressWarnings("unchecked") + public void convertsIterableToMap() { + MatcherAssert.assertThat( + "Can't convert iterable to map", + new IterableAsMap<>( + new AbstractMap.SimpleEntry<>(0, "hello, "), + new AbstractMap.SimpleEntry<>(1, "world!") + ), + Matchers.hasEntry( + Matchers.equalTo(0), + Matchers.startsWith("hello") + ) + ); + } +} diff --git a/src/test/java/org/cactoos/map/package-info.java b/src/test/java/org/cactoos/map/package-info.java new file mode 100644 index 0000000000..6a01588604 --- /dev/null +++ b/src/test/java/org/cactoos/map/package-info.java @@ -0,0 +1,32 @@ +/** + * 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. + */ + +/** + * Maps, tests. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + */ +package org.cactoos.map; From 769e354b44eefbe2dbb6be5af2c23d728be97c5d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 10:32:53 +0300 Subject: [PATCH 04/13] #119: StickyScalar fixed --- .../java/org/cactoos/func/StickyScalar.java | 23 +++----- .../java/org/cactoos/func/StickyFuncTest.java | 53 +++++++++++++++++++ .../org/cactoos/func/StickyScalarTest.java | 53 +++++++++++++++++++ 3 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 src/test/java/org/cactoos/func/StickyFuncTest.java create mode 100644 src/test/java/org/cactoos/func/StickyScalarTest.java diff --git a/src/main/java/org/cactoos/func/StickyScalar.java b/src/main/java/org/cactoos/func/StickyScalar.java index 512d5aaf08..981a569883 100644 --- a/src/main/java/org/cactoos/func/StickyScalar.java +++ b/src/main/java/org/cactoos/func/StickyScalar.java @@ -23,8 +23,7 @@ */ package org.cactoos.func; -import java.util.ArrayList; -import java.util.List; +import org.cactoos.Func; import org.cactoos.Scalar; /** @@ -33,6 +32,7 @@ *

There is no thread-safety guarantee. * * @author Tim Hinkes (timmeey@timmeey.de) + * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of result * @since 0.3 @@ -40,29 +40,22 @@ public final class StickyScalar implements Scalar { /** - * The scalar to cache. + * Func. */ - private final Scalar source; - - /** - * The list used as a optional value. - */ - private final List cache; + private final Func func; /** * Ctor. * @param src The Scalar to cache */ public StickyScalar(final Scalar src) { - this.source = src; - this.cache = new ArrayList<>(1); + this.func = new StickyFunc<>( + input -> src.asValue() + ); } @Override public T asValue() throws Exception { - if (this.cache.isEmpty()) { - this.cache.add(this.source.asValue()); - } - return this.cache.get(0); + return this.func.apply(true); } } diff --git a/src/test/java/org/cactoos/func/StickyFuncTest.java b/src/test/java/org/cactoos/func/StickyFuncTest.java new file mode 100644 index 0000000000..94da814cbd --- /dev/null +++ b/src/test/java/org/cactoos/func/StickyFuncTest.java @@ -0,0 +1,53 @@ +/** + * 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 java.security.SecureRandom; +import org.cactoos.Func; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StickyFunc}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyFuncTest { + + @Test + public void cachesFuncResults() throws Exception { + final Func func = new StickyFunc<>( + input -> new SecureRandom().nextInt() + ); + MatcherAssert.assertThat( + func.apply(true) + func.apply(true), + Matchers.equalTo(func.apply(true) + func.apply(true)) + ); + } + +} diff --git a/src/test/java/org/cactoos/func/StickyScalarTest.java b/src/test/java/org/cactoos/func/StickyScalarTest.java new file mode 100644 index 0000000000..0e16476e24 --- /dev/null +++ b/src/test/java/org/cactoos/func/StickyScalarTest.java @@ -0,0 +1,53 @@ +/** + * 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 java.security.SecureRandom; +import org.cactoos.Scalar; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StickyScalar}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyScalarTest { + + @Test + public void cachesScalarResults() throws Exception { + final Scalar scalar = new StickyScalar<>( + () -> new SecureRandom().nextInt() + ); + MatcherAssert.assertThat( + scalar.asValue() + scalar.asValue(), + Matchers.equalTo(scalar.asValue() + scalar.asValue()) + ); + } + +} From 0a1894824c8e8e9f0d66833e9718f9ad8ad1bae2 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 15:44:20 +0300 Subject: [PATCH 05/13] #116 package removed --- .../cactoos/{map => list}/IterableAsMap.java | 3 +- .../java/org/cactoos/map/package-info.java | 32 ------------------- .../{map => list}/IterableAsMapTest.java | 2 +- .../java/org/cactoos/map/package-info.java | 32 ------------------- 4 files changed, 2 insertions(+), 67 deletions(-) rename src/main/java/org/cactoos/{map => list}/IterableAsMap.java (98%) delete mode 100644 src/main/java/org/cactoos/map/package-info.java rename src/test/java/org/cactoos/{map => list}/IterableAsMapTest.java (98%) delete mode 100644 src/test/java/org/cactoos/map/package-info.java diff --git a/src/main/java/org/cactoos/map/IterableAsMap.java b/src/main/java/org/cactoos/list/IterableAsMap.java similarity index 98% rename from src/main/java/org/cactoos/map/IterableAsMap.java rename to src/main/java/org/cactoos/list/IterableAsMap.java index 17e48e418d..b1f316434c 100644 --- a/src/main/java/org/cactoos/map/IterableAsMap.java +++ b/src/main/java/org/cactoos/list/IterableAsMap.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.map; +package org.cactoos.list; import java.util.Collection; import java.util.HashMap; @@ -29,7 +29,6 @@ import java.util.Set; import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; -import org.cactoos.list.ArrayAsIterable; /** * Iterable as {@link Map}. diff --git a/src/main/java/org/cactoos/map/package-info.java b/src/main/java/org/cactoos/map/package-info.java deleted file mode 100644 index cd9f22d554..0000000000 --- a/src/main/java/org/cactoos/map/package-info.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * 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. - */ - -/** - * Maps. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.4 - */ -package org.cactoos.map; diff --git a/src/test/java/org/cactoos/map/IterableAsMapTest.java b/src/test/java/org/cactoos/list/IterableAsMapTest.java similarity index 98% rename from src/test/java/org/cactoos/map/IterableAsMapTest.java rename to src/test/java/org/cactoos/list/IterableAsMapTest.java index cdcad047ea..57bb9a4c49 100644 --- a/src/test/java/org/cactoos/map/IterableAsMapTest.java +++ b/src/test/java/org/cactoos/list/IterableAsMapTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.map; +package org.cactoos.list; import java.util.AbstractMap; import org.hamcrest.MatcherAssert; diff --git a/src/test/java/org/cactoos/map/package-info.java b/src/test/java/org/cactoos/map/package-info.java deleted file mode 100644 index 6a01588604..0000000000 --- a/src/test/java/org/cactoos/map/package-info.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * 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. - */ - -/** - * Maps, tests. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.4 - */ -package org.cactoos.map; From 9d39e23a8fb57ab5b694dbd727fca9cab9802509 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 15:57:03 +0300 Subject: [PATCH 06/13] names --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3347205fe1..fc992280df 100644 --- a/README.md +++ b/README.md @@ -246,14 +246,14 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static ## Contributors - - [Yegor Bugayenko](https://github.com/yegor256) - - [Kirill Che.](https://github.com/g4s8) g4s8.public@gmail.com - - [Fabrício Cabral](https://github.com/fabriciofx) - - [Andriy Kryvtsun](https://github.com/englishman) - - [Vseslav Sekorin](https://github.com/VsSekorin) - - [Andrey Valyaev](https://github.com/DronMDF) - - [Dušan Rychnovský](https://github.com/dusan-rychnovsky) [Blog](http://blog.dusanrychnovsky.cz/) - - [Tim Hinkes](https://github.com/timmeey) [Blog](https://blog.timmeey.de) + - [@yegor256](https://github.com/yegor256) as Yegor Bugayenko (yegor256@gmail.com) + - [@g4s8](https://github.com/g4s8) as Kirill Che. (g4s8.public@gmail.com) + - [@fabriciofx](https://github.com/fabriciofx) as Fabrício Cabral + - [@englishman](https://github.com/englishman) as Andriy Kryvtsun + - [@VsSekorin](https://github.com/VsSekorin) as Vseslav Sekorin + - [@DronMDF](https://github.com/DronMDF) as Andrey Valyaev + - [@dusan-rychnovsky](https://github.com/dusan-rychnovsky) as Dušan Rychnovský ([Blog](http://blog.dusanrychnovsky.cz/)) + - [@timmeey](https://github.com/timmeey) as Tim Hinkes ([Blog](https://blog.timmeey.de)) ## License (MIT) From 2a49072a89e8507279e731fe46297af874847bc8 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 15:57:38 +0300 Subject: [PATCH 07/13] blog --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc992280df..1d9987c2b7 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static ## Contributors - - [@yegor256](https://github.com/yegor256) as Yegor Bugayenko (yegor256@gmail.com) + - [@yegor256](https://github.com/yegor256) as Yegor Bugayenko ([Blog](http://www.yegor256.com)) - [@g4s8](https://github.com/g4s8) as Kirill Che. (g4s8.public@gmail.com) - [@fabriciofx](https://github.com/fabriciofx) as Fabrício Cabral - [@englishman](https://github.com/englishman) as Andriy Kryvtsun From 4ad21e22e11df69c862619a2263f7fa03807d8ed Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 18:32:26 +0300 Subject: [PATCH 08/13] doc --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1d9987c2b7..8d9e1f3025 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Java version required: 1.8+. ## Input/Output -To read a file: +To read a text file in UTF-8: ```java String text = new BytesAsText( @@ -86,7 +86,7 @@ To read a binary file from classpath: ```java byte[] data = new InputAsBytes( - new ResourceAsInput("/foo/img.jpg") + new ResourceAsInput("foo/img.jpg") ).asBytes(); ``` From 424ced1e0f1c8e2e79818f154ec8f9310dba6879 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 18:34:26 +0300 Subject: [PATCH 09/13] doc --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8d9e1f3025..9b8a21a6ad 100644 --- a/README.md +++ b/README.md @@ -145,12 +145,11 @@ To iterate a collection: new AllOf( new TransformedIterable<>( new ArrayAsIterable<>("how", "are", "you"), - new Func.Quiet() { - @Override - public void exec(final String input) throws Exception { + new ProcAsFunc<>( + input -> { System.out.printf("Item: %s\n", input); } - } + ) ) ).asValue(); ``` @@ -160,7 +159,9 @@ Or even more compact: ```java new IterableAsBoolean( new ArrayAsIterable<>("how", "are", "you"), - (Func.Quiet) i -> System.out.printf("Item: %s\n", i) + new ProcAsFunc<>( + input -> System.out.printf("Item: %s\n", i) + ) ).asValue(); ``` From 307843fe8c082eb30bee61c9e708b858e401b4eb Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:12:49 +0300 Subject: [PATCH 10/13] #121: bug --- .../org/cactoos/io/TeeInputStreamTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/org/cactoos/io/TeeInputStreamTest.java diff --git a/src/test/java/org/cactoos/io/TeeInputStreamTest.java b/src/test/java/org/cactoos/io/TeeInputStreamTest.java new file mode 100644 index 0000000000..11f12c4376 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputStreamTest.java @@ -0,0 +1,81 @@ +/** + * 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.io; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link TeeInputStream}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.1 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class TeeInputStreamTest { + + @Test + public void copiesContentByteByByte() throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final String content = "Hello, товарищ!"; + MatcherAssert.assertThat( + "Can't copy InputStream to OutputStream byte by byte", + TeeInputStreamTest.asString( + new TeeInputStream( + new ByteArrayInputStream( + content.getBytes(StandardCharsets.UTF_8) + ), + baos + ) + ), + Matchers.allOf( + Matchers.equalTo(content), + Matchers.equalTo( + new String(baos.toByteArray(), StandardCharsets.UTF_8) + ) + ) + ); + } + + private static String asString(final InputStream input) throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while (true) { + final int data = input.read(); + if (data < 0) { + break; + } + baos.write(data); + } + input.close(); + return new String(baos.toByteArray(), StandardCharsets.UTF_8); + } + +} From e40eeb80203baa5b48da63629a54d0de19b70760 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:13:34 +0300 Subject: [PATCH 11/13] #121: fixed --- src/main/java/org/cactoos/io/TeeInputStream.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/io/TeeInputStream.java b/src/main/java/org/cactoos/io/TeeInputStream.java index bd31b29de9..c882476354 100644 --- a/src/main/java/org/cactoos/io/TeeInputStream.java +++ b/src/main/java/org/cactoos/io/TeeInputStream.java @@ -59,7 +59,9 @@ public TeeInputStream(final InputStream src, final OutputStream tgt) { @Override public int read() throws IOException { final int data = this.input.read(); - this.output.write(data); + if (data >= 0) { + this.output.write(data); + } return data; } From b1f65f228ced616ef9f9b04000c5a48e9c58b3de Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:35:55 +0300 Subject: [PATCH 12/13] #123: bug --- .../java/org/cactoos/io/TeeInputStream.java | 3 ++ .../java/org/cactoos/io/InputAsBytesTest.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main/java/org/cactoos/io/TeeInputStream.java b/src/main/java/org/cactoos/io/TeeInputStream.java index c882476354..9ea6a68425 100644 --- a/src/main/java/org/cactoos/io/TeeInputStream.java +++ b/src/main/java/org/cactoos/io/TeeInputStream.java @@ -37,14 +37,17 @@ * @since 0.1 */ public final class TeeInputStream extends InputStream { + /** * Input. */ private final InputStream input; + /** * Output. */ private final OutputStream output; + /** * Ctor. * @param src Source of data diff --git a/src/test/java/org/cactoos/io/InputAsBytesTest.java b/src/test/java/org/cactoos/io/InputAsBytesTest.java index 9268b1740f..3b009de1f1 100644 --- a/src/test/java/org/cactoos/io/InputAsBytesTest.java +++ b/src/test/java/org/cactoos/io/InputAsBytesTest.java @@ -23,8 +23,13 @@ */ package org.cactoos.io; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.concurrent.atomic.AtomicBoolean; +import org.cactoos.func.FuncAsMatcher; +import org.cactoos.text.BytesAsText; import org.cactoos.text.StringAsText; import org.cactoos.text.TextAsBytes; import org.hamcrest.MatcherAssert; @@ -84,4 +89,36 @@ public void readsInputIntoBytesWithSmallBuffer() throws IOException { ); } + @Test + public void closesInputStream() throws IOException { + final AtomicBoolean closed = new AtomicBoolean(); + final InputStream input = new ByteArrayInputStream( + "how are you?".getBytes() + ); + MatcherAssert.assertThat( + "Can't close InputStream correctly", + new BytesAsText( + new InputAsBytes( + new InputStreamAsInput( + new InputStream() { + @Override + public int read() throws IOException { + return input.read(); + } + @Override + public void close() throws IOException { + input.close(); + closed.set(true); + } + } + ) + ).asBytes(), + StandardCharsets.UTF_8 + ).asString(), + new FuncAsMatcher<>( + text -> closed.get() + ) + ); + } + } From 2e988e9d546cde3a50c683a3bcc895d2fb3e1a48 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:42:43 +0300 Subject: [PATCH 13/13] #123: fixed --- src/main/java/org/cactoos/io/InputAsBytes.java | 4 ++-- src/test/java/org/cactoos/io/InputAsBytesTest.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/io/InputAsBytes.java b/src/main/java/org/cactoos/io/InputAsBytes.java index 667edddc5f..0ea6e9b340 100644 --- a/src/main/java/org/cactoos/io/InputAsBytes.java +++ b/src/main/java/org/cactoos/io/InputAsBytes.java @@ -71,11 +71,11 @@ public InputAsBytes(final Input input, final int max) { @Override public byte[] asBytes() throws IOException { - try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + try (final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final InputStream stream = new TeeInput( this.source, new OutputStreamAsOutput(baos) - ).stream(); + ).stream()) { final byte[] buf = new byte[this.size]; while (true) { if (stream.read(buf) != buf.length) { diff --git a/src/test/java/org/cactoos/io/InputAsBytesTest.java b/src/test/java/org/cactoos/io/InputAsBytesTest.java index 3b009de1f1..202807517d 100644 --- a/src/test/java/org/cactoos/io/InputAsBytesTest.java +++ b/src/test/java/org/cactoos/io/InputAsBytesTest.java @@ -43,6 +43,7 @@ * @version $Id$ * @since 0.1 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class InputAsBytesTest {