Skip to content

Commit

Permalink
#498 NoNulls
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 21, 2017
1 parent 70eda3c commit 8212a77
Show file tree
Hide file tree
Showing 13 changed files with 748 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ The MIT License (MIT)
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.67</minimum>
<minimum>0.65</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.69</minimum>
<minimum>0.66</minimum>
</limit>
<limit>
<counter>COMPLEXITY</counter>
Expand Down
156 changes: 156 additions & 0 deletions src/main/java/org/cactoos/collection/CollectionNoNulls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* 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.collection;

import java.util.Collection;
import java.util.Iterator;
import org.cactoos.iterator.IteratorNoNulls;

/**
* A decorator of {@link Collection} that tolerates no NULLs.
*
* <p>There is no thread-safety guarantee.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Element type
* @since 0.27
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class CollectionNoNulls<X> implements Collection<X> {

/**
* Original collection.
*/
private final Collection<X> col;

/**
* Ctor.
* @param items Original one
*/
public CollectionNoNulls(final Collection<X> items) {
this.col = items;
}

@Override
public int size() {
return this.col.size();
}

@Override
public boolean isEmpty() {
return this.col.isEmpty();
}

@Override
public Iterator<X> iterator() {
return new IteratorNoNulls<X>(this.col.iterator());
}

@Override
public boolean contains(final Object item) {
if (item == null) {
throw new IllegalArgumentException(
"Argument of #contains(T) is NULL"
);
}
return this.col.contains(item);
}

@Override
public Object[] toArray() {
final Object[] array = this.col.toArray();
for (int idx = 0; idx < array.length; ++idx) {
if (array[idx] == null) {
throw new IllegalStateException(
String.format(
"Item #%d of #toArray() is NULL", idx
)
);
}
}
return array;
}

@Override
@SuppressWarnings("PMD.UseVarargs")
public <T> T[] toArray(final T[] array) {
this.col.toArray((Object[]) array);
for (int idx = 0; idx < array.length; ++idx) {
if (array[idx] == null) {
throw new IllegalStateException(
String.format(
"Item #%d of #toArray(array) is NULL", idx
)
);
}
}
return array;
}

@Override
public boolean add(final X item) {
if (item == null) {
throw new IllegalStateException(
"Item of #add(T) is NULL"
);
}
return this.col.add(item);
}

@Override
public boolean remove(final Object item) {
if (item == null) {
throw new IllegalStateException(
"Item of #remove(T) is NULL"
);
}
return this.col.remove(item);
}

@Override
public boolean containsAll(final Collection<?> items) {
return this.col.containsAll(new CollectionNoNulls<>(items));
}

@Override
public boolean addAll(final Collection<? extends X> items) {
return this.col.removeAll(new CollectionNoNulls<>(items));
}

@Override
public boolean removeAll(final Collection<?> items) {
return this.col.removeAll(new CollectionNoNulls<>(items));
}

@Override
public boolean retainAll(final Collection<?> items) {
return this.col.retainAll(new CollectionNoNulls<>(items));
}

@Override
public void clear() {
this.col.clear();
}
}
59 changes: 59 additions & 0 deletions src/main/java/org/cactoos/iterable/IterableNoNulls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* 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;

import java.util.Iterator;
import org.cactoos.iterator.IteratorNoNulls;

/**
* A decorator for {@link Iterable} that doesn't allow any NULL.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of item
* @since 0.27
*/
public final class IterableNoNulls<X> implements Iterable<X> {

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

/**
* Ctor.
* @param items The items
*/
public IterableNoNulls(final Iterable<X> items) {
this.origin = items;
}

@Override
public Iterator<X> iterator() {
return new IteratorNoNulls<>(this.origin.iterator());
}

}
85 changes: 85 additions & 0 deletions src/main/java/org/cactoos/iterator/IteratorNoNulls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* 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.iterator;

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;

/**
* A decorator of an {@link Iterator} that returns no NULL.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of item
* @since 0.27
*/
public final class IteratorNoNulls<X> implements Iterator<X> {

/**
* Iterator.
*/
private final Iterator<X> iterator;

/**
* Position.
*/
private final AtomicLong pos;

/**
* Ctor.
* @param src Source iterable
*/
public IteratorNoNulls(final Iterator<X> src) {
this.iterator = src;
this.pos = new AtomicLong();
}

@Override
public boolean hasNext() {
return this.iterator.hasNext();
}

@Override
public X next() {
final X next = this.iterator.next();
if (next == null) {
throw new IllegalStateException(
String.format(
"Item #%d of %s is NULL",
this.pos.get(), this.iterator
)
);
}
this.pos.incrementAndGet();
return next;
}

@Override
public void remove() {
this.iterator.remove();
}

}
Loading

0 comments on commit 8212a77

Please sign in to comment.