ma.vi.base.tuple.AbstractTuple Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.vikmad.base Show documentation
Show all versions of com.vikmad.base Show documentation
Base algos, data structures and utilities
The newest version!
/*
* Copyright (c) 2016 Vikash Madhow
*/
package ma.vi.base.tuple;
import java.util.Iterator;
import java.util.Objects;
/**
* Provides a default implementation of the java.lang.Object methods for a tuple.
*
* @author Vikash Madhow ([email protected])
*/
public abstract class AbstractTuple implements Tuple {
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Tuple)) {
return false;
}
Tuple other = (Tuple) obj;
int size = size();
if (size != other.size()) {
return false;
}
for (int i = 0; i < size; i++) {
Object a = get(i);
Object b = other.get(i);
if (!Objects.equals(a, b)) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int hash = 5;
for (int i = 0; i < size(); i++) {
Object element = get(i);
hash = 23 * hash + (element != null ? element.hashCode() : 0);
}
return hash;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder("(");
for (int i = 0; i < size(); i++) {
s.append(i == 0 ? "" : ", ").append(get(i));
}
return s.append(')').toString();
}
@Override
public Tuple clone() throws CloneNotSupportedException {
return (Tuple) super.clone();
}
/**
* Implementation of iterator using the {@link #asArray} method of this class (instead
* of the default interface one) which is slightly better as it caches the created
* array.
*/
@Override
public Iterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy