package Galaxy.Visitor.util;
import java.util.ArrayList;
import java.util.List;
import Core.Pair;
public class PositionContext implements Context<Pair<Integer, Integer>>{
List<Pair<Integer, Integer>> positionContext;
public PositionContext(){
this.positionContext = new ArrayList<Pair<Integer,Integer>>();
}
@Override
public void addContext(Pair<Integer,Integer> pos) {
// TODO Auto-generated method stub
positionContext.add(pos);
}
@Override
public Pair<Integer, Integer> getAbsoluteContext(
Pair<Integer, Integer> relativeContext) {
// TODO Auto-generated method stub
if(positionContext.size() == 0) return relativeContext;
Pair<Integer,Integer> absPos = positionContext.get(positionContext.size()-1);
return new Pair(absPos.getElem1()+relativeContext.getElem1(),
absPos.getElem2() + relativeContext.getElem2());
}
@Override
public Pair<Integer, Integer> popContext() {
Pair<Integer, Integer> pos=null;
// TODO Auto-generated method stub
if(positionContext.size() > 0){
pos = positionContext.get(positionContext.size()-1);
positionContext.remove(positionContext.size()-1);
}
return pos;
}
@Override
public PositionContext copy() {
// TODO Auto-generated method stub
PositionContext positionCtxt;
positionCtxt = new PositionContext();
for(Pair<Integer,Integer> s : this.positionContext){
positionCtxt.addContext(new Pair<Integer, Integer>(s.getElem1(), s.getElem2()));
}
return positionCtxt;
}
}