Skip to content

Commit

Permalink
#464 SolidList and SolidMap
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 18, 2017
1 parent b6d0789 commit 4c7b390
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/cactoos/iterable/Skipped.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
*/
public final class Skipped<T> extends IterableEnvelope<T> {

/**
* 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
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/org/cactoos/iterable/SolidIterable.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Objects of this class are thread-safe.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of item
* @since 0.24
*/
public final class SolidIterable<X> extends IterableEnvelope<X> {

/**
* 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<X> iterable) {
super(() -> new SyncIterable<>(new StickyIterable<>(iterable)));
}

}
9 changes: 9 additions & 0 deletions src/main/java/org/cactoos/iterable/StickyIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
*/
public final class StickyIterable<X> extends IterableEnvelope<X> {

/**
* Ctor.
* @param src The underlying iterable
*/
@SafeVarargs
public StickyIterable(final X... src) {
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param iterable The iterable
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/cactoos/iterable/SyncIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
*/
public final class SyncIterable<X> extends IterableEnvelope<X> {

/**
* Ctor.
* @param src The underlying iterable
*/
@SafeVarargs
public SyncIterable(final X... src) {
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param iterable The iterable
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/org/cactoos/list/SolidList.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Objects of this class are thread-safe.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of item
* @since 0.24
*/
public final class SolidList<X> extends ListEnvelope<X> {

/**
* Ctor.
* @param items The array
*/
@SafeVarargs
public SolidList(final X... items) {
this(new IterableOf<>(items));
}

/**
* Ctor.
* @param items The array
*/
public SolidList(final Iterable<X> items) {
this(new ListOf<>(items));
}

/**
* Ctor.
* @param items The array
* @since 0.21
*/
public SolidList(final Iterator<X> items) {
this(new ListOf<>(items));
}

/**
* Ctor.
* @param list The iterable
*/
public SolidList(final Collection<X> list) {
super(() -> new SyncList<>(new StickyList<>(list)));
}

}
151 changes: 151 additions & 0 deletions src/main/java/org/cactoos/map/SolidMap.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Objects of this class are thread-safe.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of key
* @param <Y> Type of value
* @since 0.24
*/
public final class SolidMap<X, Y> extends MapEnvelope<X, Y> {

/**
* Ctor.
* @param list List of entries
*/
@SafeVarargs
public SolidMap(final Map.Entry<X, Y>... list) {
this(new IterableOf<>(list));
}

/**
* Ctor.
* @param map The map to extend
* @param list List of entries
*/
@SafeVarargs
public SolidMap(final Map<X, Y> map, final Map.Entry<X, Y>... 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 <Z> Type of items in the list
* @checkstyle ParameterNumberCheck (5 lines)
*/
public <Z> SolidMap(final Map<X, Y> map,
final Iterable<Z> list, final Func<Z, X> key,
final Func<Z, Y> 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 <Z> Type of items in the list
*/
public <Z> SolidMap(final Iterable<Z> list, final Func<Z, X> key,
final Func<Z, Y> 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 <Z> Type of items in the list
*/
public <Z> SolidMap(final Iterable<Z> list,
final Func<Z, Map.Entry<X, Y>> 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 <Z> Type of items in the list
*/
public <Z> SolidMap(final Map<X, Y> map, final Iterable<Z> list,
final Func<Z, Map.Entry<X, Y>> entry) {
this(map, new Mapped<>(entry, list));
}

/**
* Ctor.
* @param list Entries for the entries
*/
public SolidMap(final Iterable<Map.Entry<X, Y>> list) {
this(new MapOf<>(list));
}

/**
* Ctor.
* @param list Entries for the entries
*/
public SolidMap(final Iterator<Map.Entry<X, Y>> list) {
this(() -> list);
}

/**
* Ctor.
* @param map Pre-existing map we want to extend
* @param list Entries for the entries
*/
public SolidMap(final Map<X, Y> map,
final Iterable<Map.Entry<X, Y>> list) {
this(new MapOf<>(map, list));
}

/**
* Ctor.
* @param map The map
*/
public SolidMap(final Map<X, Y> map) {
super(() -> new SyncMap<>(new StickyMap<>(map)));
}

}
48 changes: 48 additions & 0 deletions src/test/java/org/cactoos/list/SolidListTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}

}
Loading

0 comments on commit 4c7b390

Please sign in to comment.