Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

ParamCollection.java

Go to the documentation of this file.
00001 package cedar.jetweb.model.parameters;
00002 
00003 import cedar.jetweb.JetWebException;
00004 
00005 import java.util.HashMap;
00006 import java.util.Vector;
00007 
00009 
00010 public class ParamCollection <T>{
00011 
00012     private Integer id_ = -1;
00013     private HashMap<Integer, Param<T> > paramsById_ = 
00014     new HashMap<Integer, Param<T> >();
00015 
00016     private HashMap<String, Param<T> > paramsByName_ = 
00017     new HashMap<String, Param<T> >();
00018 
00020 
00021     public ParamCollection(Integer id){
00022     id_ = id;
00023     }
00024 
00025     public Integer getCollectionId(){
00026     return id_;
00027     }
00028 
00029     public ParamCollection<T> setCollectionId(Integer id){
00030     id_ = id;
00031     return this;
00032     }
00033 
00034     public Vector<Param<T> > getParams(){
00035     return (Vector<Param<T>>)paramsById_.values();
00036     }
00037 
00038     public Param<T> get(String name){
00039     return paramsByName_.get(name);
00040     }
00041 
00042     public Param<T> get(Integer id){
00043     return paramsById_.get(id);
00044     }
00045 
00046     public Integer size(){
00047     return paramsById_.size();
00048     }
00049 
00051 
00056     public ParamCollection<T> add(Param<T> param)
00057     throws JetWebException{
00058     
00059     String name = param.getName();
00060     Integer id = param.getParamId();
00061 
00062     if(id<0 && name.equals("")){
00063         throw new JetWebException
00064         ("Attempting to add unspecified parameter to collection",
00065          "param value = " + param.getValue());
00066     }
00067     Param<T> byName = paramsByName_.get(name);
00068     Param<T> byId = paramsById_.get(id);
00069     
00070     boolean gotName = byName != null;
00071     boolean gotId = byId != null;
00072     
00073     if(gotName && gotId ){
00074         if(!byName.equals(byId)){
00075         throw new JetWebException
00076             ("Attempting to add parameter to collection " + 
00077              "with conflicting name and id!",
00078              "name, id = " + name + ", " + id);
00079         }else{
00080         return this;
00081         }
00082     }
00083 
00084     if(gotName){
00085         paramsById_.put(byName.getParamId(), byName);
00086         return this;
00087     }
00088 
00089     if(gotId){
00090         paramsByName_.put(byId.getName(), byId);
00091         return this;
00092     }
00093     
00094     if(id>0){
00095         paramsById_.put(id, param);
00096     }
00097 
00098     if(!name.equals("")){
00099         paramsByName_.put(name, param);
00100     }
00101 
00102     return this;
00103     }
00104 
00106 
00107     public boolean retrieve(){
00108     return false;
00109     }
00110 
00111     public boolean store(){
00112     return false;
00113     }
00114 
00115 }

Generated Wed Jan 17 09:14:27 GMT 2007