Skip to content

Commit

Permalink
collections
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 24, 2017
1 parent 1ca3a93 commit c1b005c
Show file tree
Hide file tree
Showing 17 changed files with 1,036 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ There are a few design principles behind Cactoos:

* No `null` ([why?](http://www.yegor256.com/2014/05/13/why-null-is-bad.html))
* No code in constructors ([why?](http://www.yegor256.com/2015/05/07/ctors-must-be-code-free.html))
* No getters and setters ([why?](http://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html))
* No mutable objects ([why?](http://www.yegor256.com/2014/06/09/objects-should-be-immutable.html))
* No `static` methods, not even `private` ones ([why?](http://www.yegor256.com/2017/02/07/private-method-is-new-class.html))
* No `instanceof`, type casting, or reflection ([why?](http://www.yegor256.com/2015/04/02/class-casting-is-anti-pattern.html))
* No public methods without `@Override`
* No statements in test methods except `assertThat` ([why?](http://www.yegor256.com/2017/05/17/single-statement-unit-tests.html))

Expand Down Expand Up @@ -105,11 +108,12 @@ To filter a collection:
Collection<String> filtered = new IterableAsCollection(
new FilteredIterable<>(
new ArrayAsIterable("hello", "world", "dude"),
new Predicate() {
new Function<String, Boolean>() {
@Override

public boolean apply(String i) {
return i.length() > 4;
}
}
i -> i.length() > 4
)
);
```
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/org/cactoos/Func.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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;

import java.io.IOException;

/**
* Function.
*
* <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 interface Func<X, Y> {

/**
* Apply it.
* @param input The argument
* @return The result
* @throws IOException If fails
*/
Y apply(X input) throws IOException;

}
61 changes: 61 additions & 0 deletions src/main/java/org/cactoos/list/ArrayAsIterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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.Arrays;
import java.util.Iterator;

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

/**
* The array.
*/
private final X[] array;

/**
* Ctor.
* @param items The array
*/
@SafeVarargs
@SuppressWarnings("varargs")
public ArrayAsIterable(final X... items) {
this.array = items;
}

@Override
public Iterator<X> iterator() {
return Arrays.asList(this.array).iterator();
}

}
74 changes: 74 additions & 0 deletions src/main/java/org/cactoos/list/ConcatenatedIterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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.Arrays;
import java.util.Iterator;

/**
* A few Iterables joined together.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <T> Type of item
* @since 0.1
*/
public final class ConcatenatedIterable<T> implements Iterable<T> {

/**
* Iterables.
*/
private final Iterable<Iterable<T>> list;

/**
* Ctor.
* @param items Items to concatenate
*/
@SafeVarargs
@SuppressWarnings("varargs")
public ConcatenatedIterable(final Iterable<T>... items) {
this(Arrays.asList(items));
}

/**
* Ctor.
* @param items Items to concatenate
*/
public ConcatenatedIterable(final Iterable<Iterable<T>> items) {
this.list = items;
}

@Override
public Iterator<T> iterator() {
return new ConcatenatedIterator<>(
new TransformedIterable<>(
this.list,
new StickyFunc<>(Iterable::iterator)
)
);
}

}
77 changes: 77 additions & 0 deletions src/main/java/org/cactoos/list/ConcatenatedIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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.Arrays;
import java.util.Iterator;

/**
* A few Iterables joined together.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <T> Type of item
* @since 0.1
*/
public final class ConcatenatedIterator<T> implements Iterator<T> {

/**
* Iterables.
*/
private final Iterable<Iterator<T>> list;

/**
* Ctor.
* @param items Items to concatenate
*/
@SafeVarargs
@SuppressWarnings("varargs")
public ConcatenatedIterator(final Iterator<T>... items) {
this(Arrays.asList(items));
}

/**
* Ctor.
* @param items Items to concatenate
*/
public ConcatenatedIterator(final Iterable<Iterator<T>> items) {
this.list = items;
}

@Override
public boolean hasNext() {
return new FilteredIterable<>(
this.list, Iterator::hasNext
).iterator().hasNext();
}

@Override
public T next() {
return new FilteredIterable<>(
this.list, Iterator::hasNext
).iterator().next().next();
}
}
68 changes: 68 additions & 0 deletions src/main/java/org/cactoos/list/FilteredIterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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.Func;

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

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

/**
* Function.
*/
private final Func<X, Boolean> func;

/**
* Ctor.
* @param src Source iterable
* @param fnc Func
*/
public FilteredIterable(final Iterable<X> src, final Func<X, Boolean> fnc) {
this.iterable = src;
this.func = fnc;
}

@Override
public Iterator<X> iterator() {
return new FilteredIterator<>(
this.iterable.iterator(), this.func
);
}

}
Loading

0 comments on commit c1b005c

Please sign in to comment.