Android Open Source - AnkiStats Atom






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  ww. j  ava  2 s  .c  o m
import com.wildplot.android.parsing.AtomTypes.*;

/**
 *
 * @author Michael Goldbach
 * @see Pow
 */
public class Atom implements TreeElement{
    private TopLevelParser parser;
    public static enum AtomType {VARIABLE, NUMBER, EXP_IN_BRACKETS, FUNCTION_MATH, FUNCTION_X, FUNCTION_X_Y, INVALID};
    private AtomType atomType = AtomType.INVALID;
    private TreeElement atomObject;
    private Expression expression;

    public Atom(String atomString, TopLevelParser parser){
        this.parser = parser;

        if(!TopLevelParser.stringHasValidBrackets(atomString)){
            this.atomType = AtomType.INVALID;
            return;
        }

        boolean isValid = initAsExpInBrackets(atomString);
        if(!isValid)
            isValid = initAsFunctionMath(atomString);
        if(!isValid)
            isValid = initAsFunctionX(atomString);
        if(!isValid)
            isValid = initAsFunctionXY(atomString);
        if(!isValid)
            isValid = initAsNumber(atomString);
        if(!isValid)
            isValid = initAsXVariable(atomString);
        if(!isValid)
            isValid = initAsYVariable(atomString);
        if(!isValid)
            isValid = initAsVariable(atomString);
        if(!isValid)
            this.atomType = AtomType.INVALID;

    }


    private boolean initAsExpInBrackets(String atomString){
        if(atomString.length() > 0 && atomString.charAt(0) == '(' && atomString.charAt(atomString.length()-1) == ')'){
            String expressionString = atomString.substring(1, atomString.length()-1);
            Expression expressionInBrackets = new Expression(expressionString, parser);
            boolean isValidExpressionInBrackets = expressionInBrackets.getExpressionType() != Expression.ExpressionType.INVALID;
            if(isValidExpressionInBrackets){
                this.expression = expressionInBrackets;
                this.atomType = AtomType.EXP_IN_BRACKETS;
                return true;
            }
        }

        return false;
    }

    private boolean initAsFunctionMath(String atomString){
        MathFunctionAtom mathFunctionAtom = new MathFunctionAtom(atomString, parser);
        boolean isValidMathFunction = mathFunctionAtom.getMathType() != MathFunctionAtom.MathType.INVALID;
        if(isValidMathFunction){
            this.atomType = AtomType.FUNCTION_MATH;
            this.atomObject = mathFunctionAtom;
            return true;
        }

        return false;
    }

    private boolean initAsFunctionX(String atomString){
        FunctionXAtom functionXAtom = new FunctionXAtom(atomString, parser);
        boolean isValidFunctionXAtom = functionXAtom.getAtomType() != AtomType.INVALID;
        if(isValidFunctionXAtom){
            this.atomType = AtomType.FUNCTION_X;
            this.atomObject = functionXAtom;
            return true;
        }

        return false;
    }

    private boolean initAsFunctionXY(String atomString){
        FunctionXYAtom functionXYAtom = new FunctionXYAtom(atomString, parser);
        boolean isValidFunctionXYAtom = functionXYAtom.getAtomType() != AtomType.INVALID;
        if(isValidFunctionXYAtom){
            this.atomType = AtomType.FUNCTION_X_Y;
            this.atomObject = functionXYAtom;
            return true;
        }

        return false;
    }

    private boolean initAsNumber(String atomString){
        NumberAtom numberAtom = new NumberAtom(atomString);
        boolean isValidNumberAtom = numberAtom.getAtomType() != AtomType.INVALID;
        if(isValidNumberAtom){
            this.atomType = numberAtom.getAtomType();
            this.atomObject = numberAtom;
            return true;
        }
        return false;
    }

    private boolean initAsXVariable(String atomString){
        if(atomString.equals(parser.getxName())){
            this.atomType = AtomType.VARIABLE;
            this.atomObject = new XVariableAtom(parser);
            return true;
        }

        return false;
    }

    private boolean initAsYVariable(String atomString){
        if(atomString.equals(parser.getyName())){
            this.atomType = AtomType.VARIABLE;
            this.atomObject = new YVariableAtom(parser);
            return true;
        }

        return false;
    }

    private boolean initAsVariable(String atomString){
        VariableAtom variableAtom = new VariableAtom(atomString, parser);
        boolean isValidVariableAtom = variableAtom.getAtomType() != AtomType.INVALID;
        if (isValidVariableAtom){
            this.atomType = variableAtom.getAtomType();
            this.atomObject = variableAtom;
            return true;
        }
        return false;
    }

    public AtomType getAtomType() {
        return atomType;
    }

    public double getValue() throws ExpressionFormatException{
        switch (atomType) {
            case EXP_IN_BRACKETS:
                return expression.getValue();
            case VARIABLE:
            case NUMBER:
            case FUNCTION_MATH:
            case FUNCTION_X:
            case FUNCTION_X_Y:
                return atomObject.getValue();
            case INVALID:
            default:
                throw new ExpressionFormatException("cannot parse Atom object");
        }
    }

    @Override
    public boolean isVariable() throws ExpressionFormatException{
        switch (atomType) {
            case EXP_IN_BRACKETS:
                return expression.isVariable();
            case VARIABLE:
            case NUMBER:
            case FUNCTION_MATH:
            case FUNCTION_X:
            case FUNCTION_X_Y:
                return atomObject.isVariable();
            case INVALID:
            default:
                throw new ExpressionFormatException("cannot parse Atom object");
        }
    }
}




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