
net.markenwerk.commons.collections.sources.AbstractSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-collections Show documentation
Show all versions of commons-collections Show documentation
Some collection interfaces and implementations
The newest version!
/*
* Copyright (c) 2017 Torsten Krause, Markenwerk GmbH
*
* 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 NONINFRINGEMENT. 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 net.markenwerk.commons.collections.sources;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import net.markenwerk.commons.datastructures.Optional;
import net.markenwerk.commons.interfaces.Predicate;
import net.markenwerk.commons.iterables.ArrayIterable;
import net.markenwerk.commons.iterables.ObjectIterable;
/**
* An {@link AbstractSource} is an abstract base implementation of a
* {@link Source}.
*
* @param
* The payload type.
* @author Torsten Krause (tk at markenwerk dot net)
* @since 1.0.0
*/
public abstract class AbstractSource implements Source {
@Override
public final boolean isEmpty() {
return 0 == size();
}
@Override
public final Payload getFirst() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("This source is empty");
}
return doGetFirst();
}
/**
* Returns the first (i.e. first payload value in an
* {@link Source#iterator() iterator}) payload value, which is guaranteed to
* exist.
*
* @return The payload value.
*/
protected Payload doGetFirst() {
return iterator().next();
}
@Override
public final Optional getFirst(Payload reference) {
return getFirstMatch(createPredicate(reference));
}
@Override
public final Optional getFirstMatch(Predicate super Payload> predicate) throws IllegalArgumentException {
if (null == predicate) {
throw new IllegalArgumentException("The given predicate is null");
}
Iterator iterator = iterator();
while (iterator.hasNext()) {
Payload payload = iterator.next();
if (predicate.test(payload)) {
return new Optional(payload);
}
}
return new Optional();
}
@Override
public final Source getAll(Payload payload) {
return getAllMatches(createPredicate(payload));
}
@Override
public final Source getAllMatches(Predicate super Payload> predicate) throws IllegalArgumentException {
if (null == predicate) {
throw new IllegalArgumentException("The given predicate is null");
}
List list = new LinkedList();
for (Payload payload : this) {
if (predicate.test(payload)) {
list.add(payload);
}
}
return new CollectionSource(list);
}
@Override
public final boolean contains(Object reference) {
return containsAll(new ObjectIterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy