package org.apache.struts.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class ActionMessages implements Serializable {
  public static final String GLOBAL_MESSAGE = "org.apache.struts.action.GLOBAL_MESSAGE";
  
  protected HashMap messages = new HashMap();
  
  protected int iCount = 0;
  
  public ActionMessages() {}
  
  public ActionMessages(ActionMessages messages) {
    add(messages);
  }
  
  public void add(String property, ActionMessage message) {
    ActionMessageItem item = (ActionMessageItem)this.messages.get(property);
    List list = null;
    if (item == null) {
      list = new ArrayList();
      item = new ActionMessageItem(this, list, this.iCount++);
      this.messages.put(property, item);
    } else {
      list = item.getList();
    } 
    list.add(message);
  }
  
  public void add(ActionMessages messages) {
    if (messages == null)
      return; 
    Iterator props = messages.properties();
    while (props.hasNext()) {
      String property = props.next();
      Iterator msgs = messages.get(property);
      while (msgs.hasNext()) {
        ActionMessage msg = msgs.next();
        add(property, msg);
      } 
    } 
  }
  
  public void clear() {
    this.messages.clear();
  }
  
  public boolean empty() {
    return isEmpty();
  }
  
  public boolean isEmpty() {
    return this.messages.isEmpty();
  }
  
  public Iterator get() {
    if (this.messages.isEmpty())
      return Collections.EMPTY_LIST.iterator(); 
    ArrayList results = new ArrayList();
    ArrayList actionItems = new ArrayList();
    for (Iterator i = this.messages.values().iterator(); i.hasNext();)
      actionItems.add(i.next()); 
    Collections.sort(actionItems, new Comparator(this) {
          private final ActionMessages this$0;
          
          public int compare(Object o1, Object o2) {
            return ((ActionMessages.ActionMessageItem)o1).getOrder() - ((ActionMessages.ActionMessageItem)o2).getOrder();
          }
        });
    for (Iterator iterator = actionItems.iterator(); iterator.hasNext(); ) {
      ActionMessageItem ami = (ActionMessageItem)iterator.next();
      for (Iterator messages = ami.getList().iterator(); messages.hasNext();)
        results.add(messages.next()); 
    } 
    return results.iterator();
  }
  
  public Iterator get(String property) {
    ActionMessageItem item = (ActionMessageItem)this.messages.get(property);
    if (item == null)
      return Collections.EMPTY_LIST.iterator(); 
    return item.getList().iterator();
  }
  
  public Iterator properties() {
    return this.messages.keySet().iterator();
  }
  
  public int size() {
    int total = 0;
    for (Iterator i = this.messages.values().iterator(); i.hasNext(); ) {
      ActionMessageItem ami = i.next();
      total += ami.getList().size();
    } 
    return total;
  }
  
  public int size(String property) {
    ActionMessageItem ami = (ActionMessageItem)this.messages.get(property);
    if (ami == null)
      return 0; 
    return ami.getList().size();
  }
  
  protected class ActionMessageItem implements Serializable {
    protected List list;
    
    protected int iOrder;
    
    private final ActionMessages this$0;
    
    public ActionMessageItem(ActionMessages this$0, List list, int iOrder) {
      this.this$0 = this$0;
      this.list = null;
      this.iOrder = 0;
      this.list = list;
      this.iOrder = iOrder;
    }
    
    public List getList() {
      return this.list;
    }
    
    public void setList(List list) {
      this.list = list;
    }
    
    public int getOrder() {
      return this.iOrder;
    }
    
    public void setOrder(int iOrder) {
      this.iOrder = iOrder;
    }
  }
}
