该楼层疑似违规已被系统折叠隐藏此楼查看此楼
packagelab2.lang;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Stack;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
/**
*TheExpressionFactory
classrepresentsthefactorythat
*generatesexpressions.
*
*@authorgroup7
*
*/
publicclassExpressionFactory{
privateHashMapops=newHashMap();
privatePatternopPattern=null;
/**
*Setanoperatorintherule.
*
*@paramopTheoperator.Itcannotbenullorempty.
*@paramlevelThepriorityoftheoperator.Biggernumberrepresents
*higherpriority.
*/
publicvoidsetOp(Stringop,intlevel){
if(op==null){
thrownewNullPointerException();
}
op=op.trim();
if(op.length()==0){
thrownewIllegalArgumentException();
}
ops.put(op,level);
}
/**
*Initializetheinstance.Mustbecalledbeforeusingtheinstance.
*/
publicvoidinit(){
StringBuffersb=newStringBuffer();
for(Strings:ops.keySet()){
sb.append(Pattern.quote(s));
sb.append("|");
}
sb.append("\\(|\\)");
opPattern=pile(sb.toString());
}
/**
*Buildanexpressionfromthestring.
*
*@paramexpressionTheexpressionstring.
*@returnTheexpression.
*@throwsIllegalArgumentExceptionIfthisexpressionstringisinvalid.
*/
publicExpressionbuild(Stringexpression){
if(expression==null){
thrownewNullPointerException();
}
expression=expression.trim();
if(expression.length()==0){
thrownewIllegalArgumentException("emptyexpression");
}
StackopStack=newStack();
ListoutputStr=newArrayList();
ListoutputOp=newArrayList();
intstart=0;
Matcherm=opPattern.matcher(expression);