From 09aecbeaa746e45b482713135f523012b6c7cc6f Mon Sep 17 00:00:00 2001 From: Michael Lee <michael.lee.0x2a@gmail.com> Date: Mon, 25 Jan 2016 18:36:44 -0800 Subject: [PATCH] Make WorkList's toString method non-consuming --- src/cse332/interfaces/worklists/WorkList.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cse332/interfaces/worklists/WorkList.java b/src/cse332/interfaces/worklists/WorkList.java index 0add88b..48c3837 100644 --- a/src/cse332/interfaces/worklists/WorkList.java +++ b/src/cse332/interfaces/worklists/WorkList.java @@ -89,22 +89,23 @@ public abstract class WorkList<E> implements Iterable<E> { } /** - * Note that the toString() method of a WorkList _consumes_ the WorkList. - * This can lead to odd and unpredictable behavior. - * - * @postcondition hasWork() is false + * Returns some basic information about this particular worklist. + * + * Calling this method does not consume the worklist. + * + * @return a string representation of this worklist */ @Override public String toString() { - StringBuilder result = new StringBuilder(); - result.append("["); - while (this.hasWork()) { - result.append(this.next().toString() + ", "); - } - if (result.length() > 1) { - result.replace(result.length() - 2, result.length(), ""); + if (this.hasWork()) { + return String.format("%s(size = %d, peek = %s)", + this.getClass().getSimpleName(), + this.size(), + this.peek()); + } else { + return String.format("%s(size = %d)", + this.getClass().getSimpleName(), + this.size()); } - result.append("]"); - return result.toString(); } } -- GitLab