org.apache.isis.commons.collections.Can_Empty Maven / Gradle / Ivy
Go to download
Apache Isis Commons is a library with utilities, that are shared with the entire Apache Isis ecosystem.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.isis.commons.collections;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.springframework.lang.Nullable;
import org.apache.isis.commons.internal.base._Casts;
import org.apache.isis.commons.internal.exceptions._Exceptions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Value;
import lombok.val;
@Value @NoArgsConstructor(access = AccessLevel.PRIVATE)
final class Can_Empty implements Can {
private static final long serialVersionUID = 1L;
static final Can_Empty> INSTANCE = new Can_Empty<>();
@Override
public Cardinality getCardinality() {
return Cardinality.ZERO;
}
@Override
public Stream stream() {
return Stream.empty();
}
@Override
public Stream parallelStream() {
return Stream.empty();
}
@Override
public Optional getSingleton() {
return Optional.empty();
}
@Override
public Optional getFirst() {
return Optional.empty();
}
@Override
public Optional getLast() {
return Optional.empty();
}
@Override
public Optional get(final int elementIndex) {
return Optional.empty();
}
@Override
public boolean contains(final @Nullable T element) {
return false;
}
@Override
public int size() {
return 0;
}
@Override
public Iterator iterator() {
return Collections.emptyList().iterator();
}
@Override
public Can unique() {
return this;
}
@Override
public Can reverse() {
return this;
}
@Override
public Iterator reverseIterator() {
return iterator();
}
@Override
public void forEach(final Consumer super T> action) {
}
@Override
public Can filter(final @Nullable Predicate super T> predicate) {
return this; // identity
}
@Override
public void zip(final Iterable zippedIn, final BiConsumer super T, ? super R> action) {
// no-op
}
@Override
public Can zipMap(final Iterable zippedIn, final BiFunction super T, ? super Z, R> mapper) {
return Can.empty();
}
@Override
public Can add(final @Nullable T element) {
return element != null
? Can.ofSingleton(element)
: this;
}
@Override
public Can addUnique(final @Nullable T element) {
return add(element);
}
@Override
public Can addAll(final @Nullable Can other) {
return other != null
? other
: this;
}
@Override
public Can add(final int index, final @Nullable T element) {
if(index!=0) {
throw new IndexOutOfBoundsException(
"cannot add to empty can with index other than 0; got " + index);
}
return add(element);
}
@Override
public Can replace(final int index, final @Nullable T element) {
throw _Exceptions.unsupportedOperation("cannot replace an element in an empty Can");
}
@Override
public Can remove(final int index) {
throw new IndexOutOfBoundsException("cannot remove anything from an empty Can");
}
@Override
public Can remove(final @Nullable T element) {
return this; // on an empty can this is a no-op
}
@Override
public Can pickByIndex(final @Nullable int... indices) {
return Can.empty();
}
@Override
public int indexOf(final @Nullable T element) {
return -1;
}
@Override
public String toString() {
return "Can[]";
}
@Override
public boolean equals(final @Nullable Object obj) {
if(INSTANCE == obj) {
return true; // optimization not strictly necessary
}
return (obj instanceof Can)
? ((Can>)obj).isEmpty()
: false;
}
@Override
public int hashCode() {
return 0;
}
@Override
public int compareTo(final @Nullable Can other) {
if(other==null) {
return 0;
}
// when returning
// -1 ... this is before other
// +1 ... this is after other
return Integer.compare(0, other.size()); // all empty Cans are same and come first
}
@Override
public List toList() {
return Collections.emptyList(); // serializable and immutable
}
@Override
public Set toSet() {
return Collections.emptySet(); // serializable and immutable
}
@Override
public Set toSet(final @NonNull Consumer onDuplicated) {
return Collections.emptySet(); // serializable and immutable
}
@Override
public > C toCollection(final @NonNull Supplier collectionFactory) {
return collectionFactory.get();
}
@Override
public T[] toArray(final @NonNull Class elementType) {
val array = _Casts.uncheckedCast(Array.newInstance(elementType, 0));
return array;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy