Android Open Source - AnkiStats Term






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;
//from  w ww. j a va  2 s .co  m


public class Term implements TreeElement{
    private TopLevelParser parser;
    public static enum TermType { TERM_MUL_FACTOR, TERM_DIV_FACTOR, FACTOR, INVALID};
    private TermType termType = TermType.INVALID;
    private Factor factor = null;
    private Term term = null;
    
    
    public Term(String termString, TopLevelParser parser){
        this.parser = parser;

        if(!TopLevelParser.stringHasValidBrackets(termString)){
            this.termType = TermType.INVALID;
            return;
        }

        boolean isReady = false;

        isReady = initAsTermMulOrDivFactor(termString);
        if(!isReady)
            isReady = initAsFactor(termString);
        if(!isReady)
            this.termType = TermType.INVALID;
    }
    
    private boolean initAsTermMulOrDivFactor(String termString){
        int bracketChecker = 0;
        for(int i = 0; i< termString.length(); i++){
            if(termString.charAt(i) == '('){
                bracketChecker++;
            }
            if(termString.charAt(i) == ')'){
                bracketChecker--;
            }
            if((termString.charAt(i) == '*' || termString.charAt(i) == '/') && bracketChecker == 0){
                String leftSubString = termString.substring(0, i);
                if(!TopLevelParser.stringHasValidBrackets(leftSubString))
                    continue;
                Term leftTerm = new Term(leftSubString, parser);
                boolean isValidFirstPartTerm = leftTerm.getTermType() != TermType.INVALID;
                
                if(!isValidFirstPartTerm)
                    continue;
                
                boolean isValidSecondPartFactor = false;
                String rightSubString = termString.substring(i+1, termString.length());
                if(!TopLevelParser.stringHasValidBrackets(rightSubString))
                    continue;
                Factor rightFactor = new Factor(rightSubString, parser);
                isValidSecondPartFactor = rightFactor.getFactorType() != Factor.FactorType.INVALID;
                
                if(isValidSecondPartFactor){
                    if(termString.charAt(i) == '*')
                        this.termType = TermType.TERM_MUL_FACTOR;
                    else
                        this.termType = TermType.TERM_DIV_FACTOR;
                    this.term=leftTerm;
                    this.factor=rightFactor;
                    return true;
                }
                
            }
        }
        
        return false;
    }

    private boolean initAsFactor(String termString){
        Factor factor = new Factor(termString, parser);
        boolean isValidTerm = factor.getFactorType() != Factor.FactorType.INVALID;
        if(isValidTerm){
            this.termType = TermType.FACTOR;
            this.factor = factor;
            return true;
        }
        return false;
    }
    
    
    public TermType getTermType() {
        return termType;
    }
    public double getValue() throws ExpressionFormatException{
        switch (termType) {
            case TERM_MUL_FACTOR:
                return term.getValue() * factor.getValue();
            case TERM_DIV_FACTOR:
                return  term.getValue() / factor.getValue();
            case FACTOR:
                return factor.getValue();
            case INVALID:
            default:
                throw new ExpressionFormatException("could not parse Term");
        }
    }

    @Override
    public boolean isVariable() {
        switch (termType) {
            case TERM_MUL_FACTOR:
            case TERM_DIV_FACTOR:
                return  term.isVariable() || factor.isVariable();
            case FACTOR:
                return factor.isVariable();
            case INVALID:
            default:
                throw new ExpressionFormatException("could not parse Term");
        }
    }


}




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