craterdog.collections.abstractions.Collection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-collection-framework Show documentation
Show all versions of java-collection-framework Show documentation
A set of interfaces and classes that define a Java collection framework.
/************************************************************************
* 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.Iterator;
import craterdog.core.Sequential;
import craterdog.smart.SmartObject;
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 extends SmartObject>
implements Comparable>, Accessible, Sequential {
static private final XLogger logger = XLoggerFactory.getXLogger(Collection.class);
@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.getSize() == that.getSize()) {
result = true; // so far anyway...
Iterator thisIterator = this.createIterator();
Iterator thatIterator = that.createIterator();
while (thisIterator.hasNext()) {
E thisElement = thisIterator.getNext();
E thatElement = thatIterator.getNext();
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);
if (that == null) return 1;
if (this == that) return 0; // same object
int result = 0;
Comparator