
br.com.objectos.collections.base.AbstractBaseCollectionJavaAny Maven / Gradle / Ivy
/*
* Copyright (C) 2021-2022 Objectos Software LTDA.
*
* Licensed 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 br.com.objectos.collections.base;
import br.com.objectos.core.object.Checks;
import br.com.objectos.core.object.ToString;
import br.com.objectos.core.object.ToStringObject;
import br.com.objectos.latest.Concrete;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.RandomAccess;
/**
* An array-based {@code abstract} implementation of {@link BaseCollection}.
*
* @param type of the elements in this collection
*
* @since 2
*/
@Concrete(modifiers = "public abstract", simpleName = "AbstractBaseCollection")
abstract class AbstractBaseCollectionJavaAny implements BaseCollection {
AbstractBaseCollectionJavaAny() {}
/**
* Returns {@code true} if this collection contains all of the specified
* elements.
*
* @param first
* the first element to check for presence in this set
* @param more
* the additional elements to check for presence in this set
*
* @return {@code true} if this collection contains all of the specified
* elements
*
* @see Collection#contains(Object)
*/
@Override
public final boolean contains(Object first, Object... more) {
Checks.checkNotNull(more, "more == null");
if (isEmpty()) {
return false;
}
if (!contains(first)) {
return false;
}
for (Object e : more) {
if (!contains(e)) {
return false;
}
}
return true;
}
/**
* Returns {@code true} if this collection contains all of the elements in the
* specified collection.
*
*
* This method does not throw a {@code ClassCastException}.
*
* @param c
* a collection whose elements are to be checked
*
* @return {@code true} if this collection contains all of the elements in the
* specified collection
*
* @see Collection#contains(Object)
*/
@Override
public final boolean containsAll(Collection> c) {
Checks.checkNotNull(c, "c == null");
if (c instanceof RandomAccess && c instanceof List) {
List> list;
list = (List>) c;
for (int i = 0; i < list.size(); i++) {
Object test;
test = list.get(i);
if (!contains(test)) {
return false;
}
}
return true;
}
else {
for (Object e : c) {
if (!contains(e)) {
return false;
}
}
return true;
}
}
/**
* Formats and appends to the {@code toString} builder at the specified
* indentation {@code level} a string representation of this collection.
*
*
* The string representation may contain:
*
*
* - the simple name of the collections's class; and
* - the string representation of each element paired with its iteration
* index
*
*
* @param toString
* the builder of a {@code toString} method
* @param level
* the indentation level.
*/
@Override
public void formatToString(StringBuilder toString, int level) {
ToString.formatStart(toString, this);
Iterator iterator;
iterator = iterator();
int index;
index = 0;
if (iterator.hasNext()) {
int length;
length = sizeDigits();
String name;
name = indexName(index, length);
E value;
value = iterator.next();
ToString.formatFirstPair(toString, level, name, value);
while (iterator.hasNext()) {
index++;
name = indexName(index, length);
value = iterator.next();
ToString.formatNextPair(toString, level, name, value);
}
}
ToString.formatEnd(toString, level);
}
/**
* Returns the only element in this collection or throws an exception if the
* collection is empty or if the collection contains more than one element.
*
* @return the only element of this collection
*
* @throws IllegalStateException
* if the collection is empty or if the collection contains more than
* one element
*/
@Override
public final E getOnly() {
switch (size()) {
case 0:
throw new IllegalStateException("Could not getOnly: empty.");
case 1:
return getOnlyImpl();
default:
throw new IllegalStateException("Could not getOnly: more than one element.");
}
}
/**
* Returns {@code true} if this collection contains no elements.
*
* @return {@code true} if this collection contains no elements
*/
@Override
public final boolean isEmpty() {
return size() == 0;
}
/**
* Returns a new string by joining together, in iteration order, the
* string representation of each element in this collection.
*
*
* The string representation of each element is obtained by invoking its
* {@link Object#toString()} method.
*
* @return a new string representing this collection
*/
@Override
public String join() {
if (isEmpty()) {
return "";
}
StringBuilder sb;
sb = new StringBuilder();
UnmodifiableIterator it;
it = iterator();
while (it.hasNext()) {
Object element;
element = it.next();
sb.append(element);
}
return sb.toString();
}
/**
* Returns a new string by joining together, in iteration order, the
* string representation of each element in this collection separated by the
* specified {@code delimiter}.
*
*
* The string representation of each element is obtained by invoking its
* {@link Object#toString()} method.
*
* @param delimiter
* the separator to use
*
* @return a new string representing this collection
*/
@Override
public String join(String delimiter) {
Checks.checkNotNull(delimiter, "delimiter == null");
if (isEmpty()) {
return "";
}
StringBuilder sb;
sb = new StringBuilder();
UnmodifiableIterator it;
it = iterator();
Object element;
element = it.next();
sb.append(element);
while (it.hasNext()) {
sb.append(delimiter);
element = it.next();
sb.append(element);
}
return sb.toString();
}
/**
* Returns a new string by joining together, in iteration order, the
* string representation of each element in this collection separated by the
* specified {@code delimiter} and with the specified {@code prefix} and
* {@code suffix}.
*
* @param delimiter
* the separator to use
* @param prefix
* the first part of the output
* @param suffix
* the last part of the output
*
* @return a new string representing this collection
*/
@Override
public String join(String delimiter, String prefix, String suffix) {
Checks.checkNotNull(delimiter, "delimiter == null");
Checks.checkNotNull(prefix, "prefix == null");
Checks.checkNotNull(suffix, "suffix == null");
if (isEmpty()) {
return prefix + suffix;
}
StringBuilder sb;
sb = new StringBuilder();
sb.append(prefix);
UnmodifiableIterator it;
it = iterator();
Object element;
element = it.next();
sb.append(element);
while (it.hasNext()) {
sb.append(delimiter);
element = it.next();
sb.append(element);
}
sb.append(suffix);
return sb.toString();
}
/**
* This operation is not supported.
*
*
* This method performs no operation other than throw an
* {@link UnsupportedOperationException}.
*
* @param o
* ignored (this operation is not supported)
*
* @return this method does not return as it always throw an exception
*
* @throws UnsupportedOperationException
* always
*/
@Override
public final boolean remove(Object o) {
throw new UnsupportedOperationException();
}
/**
* This operation is not supported.
*
*
* This method performs no operation other than throw an
* {@link UnsupportedOperationException}.
*
* @param c
* ignored (this operation is not supported)
*
* @return this method does not return as it always throw an exception
*
* @throws UnsupportedOperationException
* always
*/
@Override
public final boolean removeAll(Collection> c) {
throw new UnsupportedOperationException();
}
/**
* This operation is not supported.
*
*
* This method performs no operation other than throw an
* {@link UnsupportedOperationException}.
*
* @param c
* ignored (this operation is not supported)
*
* @return this method does not return as it always throw an exception
*
* @throws UnsupportedOperationException
* always
*/
@Override
public final boolean retainAll(Collection> c) {
throw new UnsupportedOperationException();
}
/**
* Not implemented in this release. It might be implemented in a future
* release.
*
* @return this method does not return as it always throws an exception
*
* @throws UnsupportedOperationException
* this method may be implemented in a future release
*/
@Override
public Object[] toArray() {
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Not implemented in this release. It might be implemented in a future
* release.
*
* @param a
* ignored (this operation in not yet implemented)
*
* @return this method does not return as it always throws an exception
*
* @throws UnsupportedOperationException
* this method may be implemented in a future release
*/
@Override
public T[] toArray(T[] a) {
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Returns the string representation of this collection as defined by the
* {@link ToStringObject#formatToString(StringBuilder, int)} method.
*
* @return a string representation of this collection
*/
@Override
public final String toString() {
return ToString.toString(this);
}
/**
* This method is called by {@link #getOnly()} when the size is
* {@code size == 1}.
*
* @return the only element in this collection
*/
protected E getOnlyImpl() {
UnmodifiableIterator it;
it = iterator();
return it.next();
}
/**
* Returns a name for the specified index having the specified length, result
* is padded with zero digits if necessary.
*
*
* Auxiliary method for {@link #formatToString(StringBuilder, int)}
* implementation.
*
*
* Examples:
*
*
{@code
* indexName(1, 3); // returns "001"
* indexName(37, 3); // returns "037"
* indexName(912, 3); // returns "912"
* }
*
* @param index
* the index value to format
* @param length
* the length of the result string
*
* @return a name for the specified index having the specified length
*/
protected final String indexName(int index, int length) {
StringBuilder sb;
sb = new StringBuilder();
String s;
s = Integer.toString(index);
int indexLength;
indexLength = s.length();
int diff;
diff = length - indexLength;
for (int i = 0; i < diff; i++) {
sb.append(' ');
}
sb.append(s);
return sb.toString();
}
/**
* Returns the number of digits this collection's size has.
*
*
* Auxiliary method for {@link #formatToString(StringBuilder, int)}
* implementation.
*
* @return the number of digits this collection's size has
*/
protected final int sizeDigits() {
int size;
size = size();
if (size < 10) {
return 1;
}
else if (size < 100) {
return 2;
}
else {
double l;
l = Math.log10(size);
double f;
f = Math.floor(l);
return (int) f;
}
}
}