
craterdog.collections.abstractions.Collection Maven / Gradle / Ivy
/************************************************************************
* Copyright (c) Crater Dog Technologies(TM). All Rights Reserved. *
************************************************************************
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. *
* *
* This code is free software; you can redistribute it and/or modify it *
* under the terms of The MIT License (MIT), as published by the Open *
* Source Initiative. (See http://opensource.org/licenses/MIT) *
************************************************************************/
package craterdog.collections.abstractions;
import craterdog.collections.interfaces.Accessible;
import craterdog.core.Composite;
import craterdog.core.Sequential;
import craterdog.utils.NaturalComparator;
import java.lang.reflect.Array;
import java.util.Comparator;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
/**
* This abstract class defines the invariant methods that all collections must inherit.
*
* @author Derk Norton
* @param The type of element managed by the collection.
*/
public abstract class Collection implements Comparable>, Accessible, Sequential, Composite {
static private final XLogger logger = XLoggerFactory.getXLogger(Collection.class);
@Override
public String toString() {
logger.entry();
String string = toString("");
logger.exit(string);
return string;
}
@Override
public String toString(String indentation) {
logger.entry(indentation);
String string = "[]"; // empty collection
// check for an empty collection
if (!isEmpty()) {
// see if the collection should be formatted across multiple lines
boolean isMultiline = checkForMultiline();
// start formatting the collection
String nextIndentation = indentation + " ";
StringBuilder builder = new StringBuilder();
builder.append("[");
if (isMultiline) {
addNewLine(builder, nextIndentation);
}
// format each element in the collection
Iterator iterator = this.createDefaultIterator();
do {
E element = iterator.getNextElement();
Composite composite = element instanceof Composite ? (Composite) element : null;
if (composite != null) {
builder.append(composite.toString(nextIndentation));
} else {
builder.append(element.toString());
}
if (iterator.hasNext()) {
builder.append(",");
if (isMultiline) {
addNewLine(builder, nextIndentation);
} else {
builder.append(" ");
}
}
} while (iterator.hasNext());
// finish formatting the collection
if (isMultiline) {
addNewLine(builder, indentation);
}
builder.append("]");
string = builder.toString();
}
logger.exit(string);
return string;
}
static private void addNewLine(StringBuilder builder, String indentation) {
builder.append("\n");
builder.append(indentation);
}
@Override
public int hashCode() {
logger.entry();
int hash = 5;
for (E element : this) {
hash = 11 * hash + element.hashCode();
}
logger.exit(hash);
return hash;
}
@Override
public boolean equals(Object object) {
logger.entry(object);
boolean result = false;
if (object != null && getClass() == object.getClass()) {
@SuppressWarnings("unchecked")
final Collection that = (Collection) object;
if (this.getNumberOfElements() == that.getNumberOfElements()) {
result = true; // so far anyway...
Iterator thisIterator = this.createDefaultIterator();
Iterator thatIterator = that.createDefaultIterator();
while (thisIterator.hasNextElement()) {
E thisElement = thisIterator.getNextElement();
E thatElement = thatIterator.getNextElement();
if (!thisElement.equals(thatElement)) {
result = false; // oops, found a difference
break;
}
}
}
}
logger.exit(result);
return result;
}
@Override
public int compareTo(Collection that) {
logger.entry(that);
int result;
Comparator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy