package Galaxy.Visitor.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import Core.Pair;
public class EnvVariableContext implements Context<Pair<String,String>>{
Map<String, List<String>> myContext;
public EnvVariableContext(){
myContext = new TreeMap<String, List<String>>();
}
@Override
public void addContext(Pair<String, String> context) {
// TODO Auto-generated method stub
String key = context.getElem1();
String value = context.getElem2();
if(!myContext.containsKey(key)){
myContext.put(key, new ArrayList());
myContext.get(key).add(value);
}
else{
myContext.get(key).add(myContext.get(key).size(), value);
}
}
@Override
public EnvVariableContext copy() {
// TODO Auto-generated method stub
EnvVariableContext newContext = new EnvVariableContext();
for(String key : myContext.keySet()){
List<String> myVals = new ArrayList<String>();
for(String value : myContext.get(key))
myVals.add(value);
newContext.myContext.put(key, myVals);
}
return newContext;
}
public String getValue(String key){
return getAbsoluteContext(new Pair(key,null)).getElem2();
}
@Override
public Pair<String, String> getAbsoluteContext(
Pair<String, String> relativeContext) {
// TODO Auto-generated method stub
String key = relativeContext.getElem1();
if(myContext.containsKey(key)==false)
return relativeContext;
return new Pair<String,String>(key,
myContext.get(key).get(myContext.get(key).size()-1));
}
@Override
public Pair<String, String> popContext() {
// TODO Auto-generated method stub
return null;
}
public Pair<String,String> popContext(String key){
if(myContext.containsKey(key)){
List<String> s = myContext.get(key);
Pair<String,String> poppedPair = new Pair(key, s.get(s.size()-1));
myContext.get(key).remove(s.size()-1);
return poppedPair;
}
return null;
}
}