/**
 * Copyright 2010 Wealthfront Inc. Licensed under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.kaching.platform.common;

import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
import static java.util.Collections.EMPTY_LIST;

import java.util.List;

/**
 * Object helping with capturing and propagating errors.
 */
public class Errors {

  // null or [E :: L] where L is any list
  private List<String> messages;

  public Errors addMessage(String message, Object... values) {
    if (messages == null) {
      messages = newArrayList();
    }
    String formattedMessage = format(message, values);
    if (!messages.contains(formattedMessage)) {
      messages.add(formattedMessage);
    }
    return this;
  }

  public Errors addErrors(Errors errors) {
    if (errors.messages == null) {
      return this;
    }
    for (String message : errors.messages) {
      addMessage(message);
    }
    return this;
  }

  public void throwIfHasErrors() {
    if (messages != null) {
      throw new RuntimeException(toString());
    }
  }

  public int size() {
    return messages == null ? 0 : messages.size();
  }

  public boolean hasErrors() {
    return 0 < size();
  }

  @Override
  public int hashCode() {
    return messages == null ? EMPTY_LIST.hashCode() : messages.hashCode();
  }

  @Override
  public boolean equals(Object that) {
    if (this == that) {
      return true;
    }
    if (!(that instanceof Errors)) {
      return false;
    }
    return this.messages == null ?
        ((Errors) that).messages == null :
          this.messages.equals(((Errors) that).messages);
  }

  @Override
  public String toString() {
    if (messages == null) {
      return "no errors";
    }
    StringBuilder buf = new StringBuilder();
    int num = 1;
    String separator = "";
    for (String message : messages) {
      buf.append(separator);
      buf.append(num);
      buf.append(") ");
      buf.append(message);
      num++;
      separator = "\n\n";
    }
    return buf.toString();
  }

}

@@ -764,26 +764,122 @@
 ST;%0A
-%0Aimport java.util.
+import static java.util.Collections.emptyList;%0A%0Aimport java.util.List;%0A%0Aimport com.google.common.collect.Immutable
 List
@@ -1539,32 +1539,178 @@
 turn this;%0A  %7D%0A%0A
+  public List%3CString%3E getMessages() %7B%0A    if (messages == null) %7B%0A      return emptyList();%0A    %7D%0A    return ImmutableList.copyOf(messages);%0A  %7D%0A%0A
   public void th

Add a way to get to the error messages.