Android Open Source - AnkiStats Expression






From Project

Back to project page AnkiStats.

License

The source code is released under:

GNU General Public License

If you think the Android project AnkiStats listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wildplot.android.parsing;
/*  ww w.j  a  v a 2s  . c  o m*/

public class Expression implements TreeElement{
    private TopLevelParser parser;
    public static enum ExpressionType { EXP_PLUS_TERM, EXP_MINUS_TERM, TERM, INVALID}
    private ExpressionType expressionType = ExpressionType.INVALID;
    private Expression expression = null;
    private Term term = null;

    
    public Expression(String expressionString, TopLevelParser parser){
        this.parser = parser;
        if(!TopLevelParser.stringHasValidBrackets(expressionString)){
            this.expressionType = ExpressionType.INVALID;
            return;
        }

        boolean isReady = initAsExpPlusOrMinusTerm(expressionString);
        if(!isReady)
            isReady = initAsTerm(expressionString);
        if(!isReady)
            this.expressionType = ExpressionType.INVALID;
        
    }
    
    private boolean initAsExpPlusOrMinusTerm(String expressionString){
        int bracketChecker = 0;
        for(int i = 0; i< expressionString.length(); i++){
            if(expressionString.charAt(i) == '('){
                bracketChecker++;
            }
            if(expressionString.charAt(i) == ')'){
                bracketChecker--;
            }

            if((expressionString.charAt(i) == '+' || expressionString.charAt(i) == '-') && bracketChecker == 0){
                String leftSubString = expressionString.substring(0, i);
                if(!TopLevelParser.stringHasValidBrackets(leftSubString))
                    continue;
                Expression leftExpression = new Expression(leftSubString, parser);
                boolean isValidFirstPartExpression = leftExpression.getExpressionType() != ExpressionType.INVALID;
                
                if(!isValidFirstPartExpression)
                    continue;

                String rightSubString = expressionString.substring(i+1, expressionString.length());
                if(!TopLevelParser.stringHasValidBrackets(rightSubString))
                    continue;

                Term rightTerm = new Term(rightSubString, parser);
                boolean isValidSecondPartTerm = rightTerm.getTermType() != Term.TermType.INVALID;
                
                if(isValidSecondPartTerm){
                    if(expressionString.charAt(i) == '+'){
                        this.expressionType = ExpressionType.EXP_PLUS_TERM;
                    } else {
                        this.expressionType = ExpressionType.EXP_MINUS_TERM;
                    }

                    this.expression=leftExpression;
                    this.term=rightTerm;
                    return true;
                }
                
            }
        }
        return false;
    }

    private boolean initAsTerm(String expressionString){
        if(!TopLevelParser.stringHasValidBrackets(expressionString))
            return false;
        Term term = new Term(expressionString, parser);
        boolean isValidTerm = term.getTermType() != Term.TermType.INVALID;
        if(isValidTerm){
            this.expressionType = ExpressionType.TERM;
            this.term = term;
            return true;
        }
        return false;
    }
    
    
    public ExpressionType getExpressionType(){
        return expressionType;
    }

    public double getValue() throws ExpressionFormatException{
        switch (expressionType) {
            case EXP_PLUS_TERM:
                return expression.getValue() + term.getValue();
            case EXP_MINUS_TERM:
                return expression.getValue() - term.getValue();
            case TERM:
                return term.getValue();
            default:
            case INVALID:
                throw new ExpressionFormatException("could not parse Expression");
        }
    }

    @Override
    public boolean isVariable() {
        switch (expressionType) {
            case EXP_PLUS_TERM:
            case EXP_MINUS_TERM:
                return expression.isVariable() || term.isVariable();
            case TERM:
                return term.isVariable();
            default:
            case INVALID:
                throw new ExpressionFormatException("could not parse Expression");
        }
    }

}




Java Source Code List

com.wildplot.android.ankistats.AnkiDb.java
com.wildplot.android.ankistats.AnkiStatsActivity.java
com.wildplot.android.ankistats.AnkiStatsApplication.java
com.wildplot.android.ankistats.AnswerButton.java
com.wildplot.android.ankistats.ApplicationTest.java
com.wildplot.android.ankistats.CardsTypes.java
com.wildplot.android.ankistats.CollectionData.java
com.wildplot.android.ankistats.Forecast.java
com.wildplot.android.ankistats.HourlyBreakdown.java
com.wildplot.android.ankistats.Intervals.java
com.wildplot.android.ankistats.ReviewCount.java
com.wildplot.android.ankistats.Utils.java
com.wildplot.android.ankistats.WeeklyBreakdown.java
com.wildplot.android.parsing.Atom.java
com.wildplot.android.parsing.ExpressionFormatException.java
com.wildplot.android.parsing.Expression.java
com.wildplot.android.parsing.Factor.java
com.wildplot.android.parsing.Pow.java
com.wildplot.android.parsing.Term.java
com.wildplot.android.parsing.TopLevelParser.java
com.wildplot.android.parsing.TreeElement.java
com.wildplot.android.parsing.AtomTypes.FunctionXAtom.java
com.wildplot.android.parsing.AtomTypes.FunctionXYAtom.java
com.wildplot.android.parsing.AtomTypes.MathFunctionAtom.java
com.wildplot.android.parsing.AtomTypes.NumberAtom.java
com.wildplot.android.parsing.AtomTypes.VariableAtom.java
com.wildplot.android.parsing.AtomTypes.XVariableAtom.java
com.wildplot.android.parsing.AtomTypes.YVariableAtom.java
com.wildplot.android.rendering.AdvancedPlotSheet.java
com.wildplot.android.rendering.BarGraph.java
com.wildplot.android.rendering.DrawableContainer.java
com.wildplot.android.rendering.FunctionDrawer.java
com.wildplot.android.rendering.FunctionDrawer_y.java
com.wildplot.android.rendering.Integral.java
com.wildplot.android.rendering.LegendDrawable.java
com.wildplot.android.rendering.LinesPoints.java
com.wildplot.android.rendering.Lines.java
com.wildplot.android.rendering.MultiScreenPart.java
com.wildplot.android.rendering.PieChart.java
com.wildplot.android.rendering.PlotSheet.java
com.wildplot.android.rendering.PointDrawer2D.java
com.wildplot.android.rendering.RelativeColorGradient.java
com.wildplot.android.rendering.ReliefDrawer.java
com.wildplot.android.rendering.XAxisBarGraph.java
com.wildplot.android.rendering.XAxisHistoGram.java
com.wildplot.android.rendering.XAxis.java
com.wildplot.android.rendering.XGrid.java
com.wildplot.android.rendering.YAxisBarGraph.java
com.wildplot.android.rendering.YAxisHistoGram.java
com.wildplot.android.rendering.YAxis.java
com.wildplot.android.rendering.YGrid.java
com.wildplot.android.rendering.graphics.wrapper.BasicStroke.java
com.wildplot.android.rendering.graphics.wrapper.BufferedImage.java
com.wildplot.android.rendering.graphics.wrapper.Color.java
com.wildplot.android.rendering.graphics.wrapper.FontMetrics.java
com.wildplot.android.rendering.graphics.wrapper.Graphics2D.java
com.wildplot.android.rendering.graphics.wrapper.Graphics.java
com.wildplot.android.rendering.graphics.wrapper.Rectangle.java
com.wildplot.android.rendering.graphics.wrapper.Stroke.java
com.wildplot.android.rendering.interfaces.Drawable.java
com.wildplot.android.rendering.interfaces.Function2D.java
com.wildplot.android.rendering.interfaces.Function3D.java
com.wildplot.android.rendering.interfaces.Legendable.java
com.wildplot.android.rendering.interfaces.StepFunction2D.java