Android Open Source - AnkiStats Factor






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 . ja va 2s  .c  om


public class Factor implements TreeElement{
    private TopLevelParser parser;
    public static enum FactorType { PLUS_FACTOR, MINUS_FACTOR, POW, INVALID}
    private FactorType factorType = FactorType.INVALID;
    private Factor factor;
    private Pow pow;
    
    public Factor(String factorString, TopLevelParser parser){
        this.parser = parser;
        if(!TopLevelParser.stringHasValidBrackets(factorString)){
            this.factorType = FactorType.INVALID;
            return;
        }
        boolean isReady;

        isReady = initAsPlusFactor(factorString);
        if(!isReady)
            isReady = initAsMinusFactor(factorString);
        if(!isReady)
            isReady = initAsPow(factorString);
        if(!isReady)
            this.factorType = FactorType.INVALID;
    }

    private boolean initAsPlusFactor(String factorString){
        if(factorString.length() > 0 && factorString.charAt(0) == '+'){
            boolean isValidFactor;
            String leftSubString = factorString.substring(1, factorString.length());
            Factor leftFactor = new Factor(leftSubString, parser);
            isValidFactor = leftFactor.getFactorType() != FactorType.INVALID;
            if(isValidFactor){
                this.factorType = FactorType.PLUS_FACTOR;
                this.factor = leftFactor;
                return true;
            }
        }

        return false;
    }

    private boolean initAsMinusFactor(String factorString){
        if(factorString.length() > 0 && factorString.charAt(0) == '-'){
            boolean isValidFactor;
            String leftSubString = factorString.substring(1, factorString.length());
            Factor leftFactor = new Factor(leftSubString, parser);
            isValidFactor = leftFactor.getFactorType() != FactorType.INVALID;
            if(isValidFactor){
                this.factorType = FactorType.MINUS_FACTOR;
                this.factor = leftFactor;
                return true;
            }
        }

        return false;
    }

    private boolean initAsPow(String factorString){
        Pow pow = new Pow(factorString, parser);
        boolean isValidPow = pow.getPowType() != Pow.PowType.INVALID;
        if(isValidPow){
            this.factorType = FactorType.POW;
            this.pow = pow;
            return true;
        }
        return false;
    }

    public FactorType getFactorType() {
        return factorType;
    }

    public double getValue() throws ExpressionFormatException{
        switch (factorType) {
            case PLUS_FACTOR:
                return factor.getValue();
            case MINUS_FACTOR:
                return -factor.getValue();
            case POW:
                return pow.getValue();
            case INVALID:
            default:
                throw new ExpressionFormatException("cannot parse expression at factor level");
        }

    }

    @Override
    public boolean isVariable() throws ExpressionFormatException{
        switch (factorType) {
            case PLUS_FACTOR:
            case MINUS_FACTOR:
                return factor.isVariable();
            case POW:
                return pow.isVariable();
            case INVALID:
            default:
                throw new ExpressionFormatException("cannot parse expression at factor level");
        }
    }


}




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