Android Open Source - AnkiStats Pow






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;
//  w w w . j a  v a2  s.c  om
/**
 * Created by mig on 25.05.13.
 */
public class Pow implements TreeElement{
    private TopLevelParser parser;
    public static enum PowType {ATOM, ATOM_POW_FACTOR, ATOM_SQRT_FACTOR, INVALID};
    private PowType powType = PowType.INVALID;
    private Atom atom;
    private Factor factor;

    public Pow(String powString, TopLevelParser parser){
        this.parser = parser;
        if(!TopLevelParser.stringHasValidBrackets(powString)){
            this.powType = PowType.INVALID;
            return;
        }
        boolean isReady;

        isReady = initAsAtom(powString);
        if(!isReady)
            isReady = initAsAtomPowFactor(powString);
        if(!isReady)
            isReady = initAsAtomSqrtFactor(powString);
        if(!isReady)
            this.powType = PowType.INVALID;
    }

    private boolean initAsAtom(String powString){
        Atom atom = new Atom(powString, parser);
        boolean isValidAtom = atom.getAtomType() != Atom.AtomType.INVALID;
        if(isValidAtom){
            this.powType = PowType.ATOM;
            this.atom = atom;
            return true;
        }
        return false;
    }
    private boolean initAsAtomPowFactor(String powString){
        int opPos = powString.indexOf("^");
        if(opPos > 0){
            String leftAtomString = powString.substring(0,opPos);
            String rightFactorString = powString.substring(opPos+1, powString.length());
            if(!TopLevelParser.stringHasValidBrackets(leftAtomString) || !TopLevelParser.stringHasValidBrackets(rightFactorString))
                return false;
            Atom leftAtom = new Atom(leftAtomString, parser);
            boolean isValidAtom = leftAtom.getAtomType() != Atom.AtomType.INVALID;
            if(isValidAtom){
                Factor rightFactor = new Factor(rightFactorString, parser);
                boolean isValidFactor = rightFactor.getFactorType() != Factor.FactorType.INVALID;
                if(isValidFactor){
                    this.powType= PowType.ATOM_POW_FACTOR;
                    this.atom = leftAtom;
                    this.factor = rightFactor;
                    return true;
                }
            }
        }

        return false;
    }

    private boolean initAsAtomSqrtFactor(String powString){
        int opPos = powString.indexOf("**");
        if(opPos > 0){
            String leftAtomString = powString.substring(0,opPos);
            String rightFactorString = powString.substring(opPos+2, powString.length());
            if(!TopLevelParser.stringHasValidBrackets(leftAtomString) || !TopLevelParser.stringHasValidBrackets(rightFactorString))
                return false;
            Atom leftAtom = new Atom(leftAtomString, parser);
            boolean isValidAtom = leftAtom.getAtomType() != Atom.AtomType.INVALID;
            if(isValidAtom){
                Factor rightFactor = new Factor(rightFactorString, parser);
                boolean isValidFactor = rightFactor.getFactorType() != Factor.FactorType.INVALID;
                if(isValidFactor){
                    this.powType= PowType.ATOM_SQRT_FACTOR;
                    this.atom = leftAtom;
                    this.factor = rightFactor;
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    public double getValue() throws ExpressionFormatException{
        switch (powType) {
            case ATOM:
                return atom.getValue();
            case ATOM_POW_FACTOR:
                return Math.pow(atom.getValue(), factor.getValue());
            case ATOM_SQRT_FACTOR:
                return Math.pow(atom.getValue(), 1.0/factor.getValue());
            case INVALID:
            default:
                throw new ExpressionFormatException("cannot parse Atom expression");
        }
    }

    @Override
    public boolean isVariable() throws ExpressionFormatException{
        switch (powType) {
            case ATOM:
                return atom.isVariable();
            case ATOM_POW_FACTOR:
            case ATOM_SQRT_FACTOR:
                return atom.isVariable() || factor.isVariable();
            case INVALID:
            default:
                throw new ExpressionFormatException("cannot parse Atom expression");
        }
    }

    public PowType getPowType() {
        return powType;
    }
}




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