org.bitbucket.dollar.RepeatWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dollar Show documentation
Show all versions of dollar Show documentation
Java API that unifies collections, arrays, iterators/iterable and char sequences (String, StringBuilder, etc).
The newest version!
package org.bitbucket.dollar;
/*
* Dollar, http://bitbucket.org/dfa/dollar
* (c) 2010, 2011 Davide Angelocola
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import org.bitbucket.dollar.Dollar.Wrapper;
public class RepeatWrapper extends AbstractWrapper {
protected final ConcatWrapper concatWrapper;
public RepeatWrapper(ConcatWrapper concatWrapper) {
this.concatWrapper = concatWrapper;
}
public RepeatWrapper(Wrapper wrapper, int n) {
Preconditions.requireNotNull(wrapper , "n must be positive");
Preconditions.require(n > 0, "n must be positive");
concatWrapper = new ConcatWrapper(new LinkedList());
for (int i = 0; i < n; i++) {
concatWrapper.concat(wrapper.copy());
}
}
@Override
public Iterator iterator() {
return concatWrapper.iterator();
}
@Override
public Wrapper shuffle(Random random) {
Preconditions.requireNotNull(random , "random must be non-null");
return concatWrapper.shuffle(random);
}
@Override
public int size() {
return concatWrapper.size();
}
@Override
public Wrapper copy() {
return new RepeatWrapper((ConcatWrapper) concatWrapper.copy());
}
@Override
public Wrapper sort() {
return concatWrapper.sort();
}
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object object) {
if (object instanceof RepeatWrapper) {
RepeatWrapper repeatWrapper = (RepeatWrapper) object;
return concatWrapper.equals(repeatWrapper.concatWrapper); // XXX: deep equals?
} else {
return false;
}
}
@Override
public int hashCode() {
int hash = 7;
hash *= 83 + concatWrapper.hashCode();
return hash;
}
}