package org.apache.struts.action;

import java.io.Serializable;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections.FastHashMap;

public class ActionMappings implements Serializable {
  protected FastHashMap mappings = new FastHashMap();
  
  protected transient ActionServlet servlet = null;
  
  protected ActionMapping unknown = null;
  
  public boolean getFast() {
    return this.mappings.getFast();
  }
  
  public void setFast(boolean fast) {
    this.mappings.setFast(fast);
  }
  
  public ActionMapping getUnknown(ServletRequest request) {
    return getUnknown((HttpServletRequest)request);
  }
  
  public ActionMapping getUnknown(HttpServletRequest request) {
    if (this.unknown != null)
      return this.unknown; 
    String[] paths = findMappings();
    for (int i = 0; i < paths.length; i++) {
      ActionMapping mapping = findMapping(paths[i]);
      if (mapping.getUnknown()) {
        this.unknown = mapping;
        return mapping;
      } 
    } 
    return null;
  }
  
  public ActionServlet getServlet() {
    return this.servlet;
  }
  
  public void setServlet(ActionServlet servlet) {
    this.servlet = servlet;
  }
  
  public void addMapping(ActionMapping mapping) {
    this.mappings.put(mapping.getPath(), mapping);
  }
  
  public ActionMapping findMapping(String path) {
    return (ActionMapping)this.mappings.get(path);
  }
  
  public String[] findMappings() {
    return (String[])this.mappings.keySet().toArray((Object[])new String[this.mappings.size()]);
  }
  
  public void removeMapping(ActionMapping mapping) {
    this.mappings.remove(mapping.getPath());
  }
}
