package com.sirtrack.construct.lib;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Containers{
  
  public static class ContainerError extends RuntimeException {
    public ContainerError(String string) {
      super(string);
    }
  }
	
  static public class Container
  {
  	Map<Object, Object> dict = new LinkedHashMap<Object, Object>();
  	
  	public <T>Container( T... pairs ){
  		if(( pairs.length & 1 ) != 0 ){
  		  if( pairs[0] instanceof Map ){
  	      this.dict.putAll( (Map)(pairs[0]) );
  	      return;
  		  }
  		  else 
  		    throw new ContainerError( "length of parameters is not an even number: " + pairs.length );
  		}
  		for( int i=0; i<pairs.length;  ){
  			Object name = pairs[i++];
  			Object value = pairs[i++];
  			
  			this.dict.put( name, value);
  		}
  	}
  
  	public <K,V>V get( K name ){
  		return (V)dict.get(name);
  	}

  	public <K,V>V get( K name, V defaultval ){
  		if( dict.containsKey(name))
  			return (V)dict.get(name);
  		else
  			return defaultval;
  	}
  	
  	public void del( Object name ){
  		dict.remove( name );
  	}

    public void clear(){
      dict.clear();
    }

  	public void set( Object name, Object value ){
  		dict.put( name, value);
  	}
  	
  	public <T> Set<T> keys(){
  		return (Set<T>) dict.keySet();
  	}

  	public <T> T[] values( Class<T> clazz ){
  		return (T[]) dict.values().toArray((T[])Array.newInstance( clazz, 0));
  	}
  	
  	public void update( Container other ){
  		for( Object s : other.keys() ){
  				set( s, other.get(s));
  		}
  	}
  	
  	public boolean contains( Object name ){
  		return dict.containsKey(name);
  	}
  	
  	@Override
  	public boolean equals( Object o ){
  		if( o instanceof Container)
  			return dict.equals(((Container)o).dict);
  		else
  			return false;
  	}
    
  	public Container clone(){
  		return new Container( dict );
  	}
  
    public final String toString() {
      return dict.toString();
  }
  
    public Container reverse(){
    	Container reverse = new Container();
    	for( Object key : this.keys() ){
    		reverse.set( this.get(key ), key );
    	}
    	return reverse;
    }
  }
  
  static public List<Object> ListContainer( Object... args ){
  	ArrayList<Object> l = new ArrayList<Object>();
  	for( Object o: args )
  		l.add(o);
  	return l;
  }

}

@@ -2450,16 +2450,21 @@
 rgs )%7B%0D%0A
+%09  %0D%0A
   %09Array
@@ -2535,17 +2535,18 @@
 s )%0D%0A  %09
-%09
+  
 l.add(o)
@@ -2569,11 +2569,190 @@
 %0A  %7D%0D%0A%0D%0A
+  static public List%3CObject%3E ListContainer( String s )%7B%0D%0A    ArrayList%3CObject%3E l = new ArrayList%3CObject%3E();%0D%0A    for( byte b: s.getBytes() )%0D%0A    %09%09l.add(b);%0D%0A    return l;%0D%0A  %7D%0D%0A
 %7D%0D%0A

ListContainer can convert a String