Skip to content

Commit

Permalink
AbstractPCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
fernomac committed Aug 6, 2015
1 parent a138359 commit 63040b9
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/io/coronet/pico/AbstractPCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.coronet.pico;

import java.util.Iterator;

/**
* Abstract implementation of the {@code PCollection} interface. Adds
* a standard implementation of {@link Object#toString()}, since all the
* other methods with default implementations are implemented on the interface.
*/
public abstract class AbstractPCollection<E> implements PCollection<E> {

@Override
public String toString() {
Iterator<E> iter = iterator();
if (!iter.hasNext()) {
return "[]";
}

StringBuilder builder = new StringBuilder();
builder.append('[');

for (;;) {
E e = iter.next();
if (e == this) {
builder.append("(this collection)");
} else {
builder.append(e);
}

if (!iter.hasNext()) {
return builder.append(']').toString();
}

builder.append(',').append(' ');
}
}
}

0 comments on commit 63040b9

Please sign in to comment.