Skip to content

Commit

Permalink
Merge branch 'master' into 460_LowestOf
Browse files Browse the repository at this point in the history
  • Loading branch information
bedward70 committed Feb 18, 2018
2 parents 6e9358f + 61c12f8 commit ba92baf
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 124 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

**ATTENTION**: We're still in a very early alpha version, the API
may and _will_ change frequently. Please, use it at your own risk,
until we release version 1.0 (<del>July</del> <del>August</del> December 2017).
until we release version 1.0 (<del>July 2017</del> May 2018).

Cactoos is a collection of object-oriented Java primitives.

Expand All @@ -32,7 +32,7 @@ to do almost exactly the same, but through objects.
**Principles**.
There are a few design principles behind Cactoos:

* No `null` ([why?](http://www.yegor256.com/2014/05/13/why-null-is-bad.html))
* No `null` ([why?](http://www.yegor256.com/2014/05/13/why-null-is-bad.html)) and no [NULL checking](https://github.com/yegor256/cactoos/issues/650)
* 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))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/io/Directory.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.stream.Stream;

/**
* Files in a directory.
* Files and folders in a directory.
*
* <p>There is no thread-safety guarantee.
*
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/cactoos/iterable/Reversed.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
*/
package org.cactoos.iterable;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
* Reverse iterator.
*
Expand All @@ -47,22 +43,24 @@ public final class Reversed<X> extends IterableEnvelope<X> {
*/
@SafeVarargs
public Reversed(final X... src) {
this(new IterableOf<>(src));
this(new org.cactoos.collection.Reversed<>(src));
}

/**
* Ctor.
* @param src Source iterable
* @since 0.23
*/
public Reversed(final Iterable<X> src) {
super(() -> {
final List<X> list = new LinkedList<>();
for (final X item : src) {
list.add(item);
}
Collections.reverse(list);
return list;
});
this(new org.cactoos.collection.Reversed<>(src));
}

/**
* Ctor.
* @param reversed Reversed collection
*/
public Reversed(final org.cactoos.collection.Reversed<X> reversed) {
super(() -> reversed);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,54 @@
*/
package org.cactoos.iterator;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.UncheckedScalar;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Iterator that returns the same set of elements always.
* Iterator that returns the set of elements.
*
* <p>There is no thread-safety guarantee.
* <p>There is no thread-safety guarantee.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of item
* @since 0.8
* @since 0.30
*/
public final class StickyIterator<X> implements Iterator<X> {
public final class IteratorOf<X> implements Iterator<X> {

/**
* The gate.
* The list of items to iterate.
*/
private final UncheckedScalar<Iterator<X>> gate;
private final X[] list;

/**
* Ctor.
* @param items Items to iterate
* Current position.
*/
@SafeVarargs
public StickyIterator(final X... items) {
this(new IterableOf<>(items).iterator());
}
private final AtomicInteger position;

/**
* Ctor.
* @param iterator The iterator
* @param items Items to iterate
*/
public StickyIterator(final Iterator<X> iterator) {
this.gate = new UncheckedScalar<>(
new StickyScalar<>(
() -> {
final Collection<X> temp = new LinkedList<>();
while (iterator.hasNext()) {
temp.add(iterator.next());
}
return temp.iterator();
}
)
);
@SafeVarargs
public IteratorOf(final X... items) {
this.list = items;
this.position = new AtomicInteger(0);
}

@Override
public boolean hasNext() {
return this.gate.value().hasNext();
return this.position.intValue() < this.list.length;
}

@Override
public X next() {
return this.gate.value().next();
if (!this.hasNext()) {
throw new NoSuchElementException(
"The iterator doesn't have any more items"
);
}
return this.list[this.position.getAndIncrement()];
}
}
74 changes: 74 additions & 0 deletions src/main/java/org/cactoos/map/Grouped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* Iterable as {@link Map}.
*
* <p>This class groups objects from iterable by applying
* functions for keys and values</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Nikita Salomatin (nsalomatin@hotmail.com)
* @version $Id$
* @param <K> Type of key
* @param <V> Type of value
* @param <T> Type of entry objects of functions
* @since 0.30
*/
public final class Grouped<K, V, T> extends MapEnvelope<K, List<V>> {

/**
* Ctor.
*
* @param list Iterable which is used to retrieve data from
* @param keys Function to get a key
* @param values Function to get a value
*/
public Grouped(
final Iterable<T> list,
final Function<T, K> keys,
final Function<T, V> values
) {
super(() -> {
final Stream<T> stream = StreamSupport.stream(
list.spliterator(), false
);
return stream.collect(
Collectors.groupingBy(
keys,
Collectors.mapping(values, Collectors.toList())
)
);
});
}
}
75 changes: 75 additions & 0 deletions src/main/java/org/cactoos/scalar/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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 org.cactoos.Scalar;

/**
* Constant value.
*
* <p>This {@link Scalar} represents a constant value which never changes.</p>
*
* <p>Contrary to {@link StickyScalar} this constant is always
* pre-computed.</p>
*
* <p>This class implements {@link Scalar}, which throws a checked
* {@link Exception}. Despite that this class does NOT throw a checked
* exception as it only returns a pre-computed value.</p>
*
* <p>Example:
* <pre>
* final Scalar&lt;String&gt; constant = new Constant&lg;&gt;("Value");
* System.out.print("Constant is always the same: ");
* System.out.println(constant.value() == constant.value());
* </pre>
* </p>
*
* <p>This class is thread-safe.</p>
*
* @author Aliceice (elena.ihde-simon@posteo.de)
* @version $Id$
* @param <T> Type of result
* @see StickyScalar
* @since 0.30
*/
public final class Constant<T> implements Scalar<T> {

/**
* Pre-computed value.
*/
private final T val;

/**
* Ctor.
* @param value The pre-computed constant.
*/
public Constant(final T value) {
this.val = value;
}

@Override
public T value() {
return this.val;
}
}
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/func/RepeatedFuncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.security.SecureRandom;
import java.util.Iterator;
import org.cactoos.Func;
import org.cactoos.iterator.StickyIterator;
import org.cactoos.iterator.IteratorOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand All @@ -44,7 +44,7 @@ public final class RepeatedFuncTest {

@Test
public void runsFuncMultipleTimes() throws Exception {
final Iterator<Integer> iter = new StickyIterator<>(1, 2, 5, 6);
final Iterator<Integer> iter = new IteratorOf<>(1, 2, 5, 6);
final Func<Boolean, Integer> func = new RepeatedFunc<>(
input -> {
return iter.next();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/io/DirectoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
public final class DirectoryTest {

@Test
public void listsFilesInDirectory() throws IOException {
public void listsFilesAndFoldersInDirectory() throws IOException {
final Path dir = Files.createTempDirectory("x1");
dir.resolve("x/y").toFile().mkdirs();
Files.write(dir.resolve("x/y/test"), "".getBytes());
MatcherAssert.assertThat(
"Can't list files in a directory",
"Can't list files and folders in a directory",
new Directory(dir),
// @checkstyle MagicNumber (1 line)
Matchers.iterableWithSize(4)
Expand Down
Loading

0 comments on commit ba92baf

Please sign in to comment.