new plotter

This commit is contained in:
Sergey Solovyev
2013-01-14 14:00:21 +04:00
parent bb9d6f4038
commit 400d5003a3
57 changed files with 625 additions and 271 deletions

View File

@@ -2,6 +2,7 @@ package org.solovyev.android.calculator.plot;
import jscl.math.Generic;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -18,10 +19,10 @@ public interface CalculatorPlotter {
boolean addFunction(@NotNull Generic expression);
boolean addFunction(@NotNull PlotFunction plotFunction);
boolean addFunction(@NotNull XyFunction xyFunction);
boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotFunctionLineDef functionLineDef);
boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef);
boolean updateFunction(@NotNull PlotFunction newFunction);
boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotFunctionLineDef functionLineDef);
boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef);
boolean removeFunction(@NotNull PlotFunction plotFunction);
boolean removeFunction(@NotNull XyFunction xyFunction);
@@ -40,6 +41,9 @@ public interface CalculatorPlotter {
void clearAllFunctions();
@Nullable
PlotFunction getFunctionById(@NotNull String functionId);
@NotNull
List<PlotFunction> getFunctions();
@@ -59,7 +63,7 @@ public interface CalculatorPlotter {
void setPlotImag(boolean plotImag);
void setRealLineColor(@NotNull GraphLineColor realLineColor);
void setRealLineColor(@NotNull PlotLineColor realLineColor);
void setImagLineColor(@NotNull GraphLineColor imagLineColor);
void setImagLineColor(@NotNull PlotLineColor imagLineColor);
}

View File

@@ -36,10 +36,10 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
private int arity = 0;
@NotNull
private GraphLineColor realLineColor;
private PlotLineColor realLineColor;
@NotNull
private GraphLineColor imagLineColor;
private PlotLineColor imagLineColor;
public CalculatorPlotterImpl(@NotNull Calculator calculator) {
this.calculator = calculator;
@@ -164,12 +164,12 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
}
@Override
public boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotFunctionLineDef functionLineDef) {
public boolean addFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef) {
return addFunction(new PlotFunction(xyFunction, functionLineDef));
}
@Override
public boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotFunctionLineDef functionLineDef) {
public boolean updateFunction(@NotNull XyFunction xyFunction, @NotNull PlotLineDef functionLineDef) {
final PlotFunction newFunction = new PlotFunction(xyFunction, functionLineDef);
return updateFunction(newFunction);
@@ -251,7 +251,20 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
}
}
// NOTE: this method must be called from synchronized block
@org.jetbrains.annotations.Nullable
@Override
public PlotFunction getFunctionById(@NotNull final String functionId) {
synchronized (functions) {
return Iterables.find(functions, new Predicate<PlotFunction>() {
@Override
public boolean apply(@Nullable PlotFunction function) {
return function != null && function.getXyFunction().getId().equals(functionId);
}
}, null);
}
}
// NOTE: this method must be called from synchronized block
private void onFunctionsChanged() {
assert Thread.holdsLock(functions);
@@ -345,12 +358,12 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
}
@Override
public void setRealLineColor(@NotNull GraphLineColor realLineColor) {
public void setRealLineColor(@NotNull PlotLineColor realLineColor) {
this.realLineColor = realLineColor;
}
@Override
public void setImagLineColor(@NotNull GraphLineColor imagLineColor) {
public void setImagLineColor(@NotNull PlotLineColor imagLineColor) {
this.imagLineColor = imagLineColor;
}

View File

@@ -13,7 +13,7 @@ public class PlotFunction {
private XyFunction xyFunction;
@NotNull
private PlotFunctionLineDef plotFunctionLineDef;
private PlotLineDef mPlotLineDef;
private boolean pinned = false;
@@ -21,20 +21,20 @@ public class PlotFunction {
public PlotFunction(@NotNull XyFunction xyFunction) {
this.xyFunction = xyFunction;
this.plotFunctionLineDef = PlotFunctionLineDef.newDefaultInstance();
this.mPlotLineDef = PlotLineDef.newDefaultInstance();
}
public PlotFunction(@NotNull XyFunction xyFunction,
@NotNull PlotFunctionLineDef plotFunctionLineDef) {
@NotNull PlotLineDef plotLineDef) {
this.xyFunction = xyFunction;
this.plotFunctionLineDef = plotFunctionLineDef;
this.mPlotLineDef = plotLineDef;
}
@NotNull
private PlotFunction copy() {
final PlotFunction copy = new PlotFunction(this.xyFunction);
copy.plotFunctionLineDef = this.plotFunctionLineDef;
copy.mPlotLineDef = this.mPlotLineDef;
copy.pinned = this.pinned;
copy.visible = this.visible;
@@ -80,8 +80,8 @@ public class PlotFunction {
}
@NotNull
public PlotFunctionLineDef getPlotFunctionLineDef() {
return plotFunctionLineDef;
public PlotLineDef getPlotLineDef() {
return mPlotLineDef;
}
public boolean isPinned() {

View File

@@ -1,96 +0,0 @@
package org.solovyev.android.calculator.plot;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 1/5/13
* Time: 7:41 PM
*/
public class PlotFunctionLineDef {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
@NotNull
public static final Float DEFAULT_LINE_WIDTH = -1f;
private static final int WHITE = 0xFFFFFFFF;
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@NotNull
private PlotFunctionLineColorType lineColorType = PlotFunctionLineColorType.solid;
private int lineColor = WHITE;
@NotNull
private PlotLineStyle lineStyle = PlotLineStyle.solid;
private float lineWidth = -DEFAULT_LINE_WIDTH;
private PlotFunctionLineDef() {
}
@NotNull
public static PlotFunctionLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle) {
final PlotFunctionLineDef result = new PlotFunctionLineDef();
result.lineColor = lineColor;
result.lineStyle = lineStyle;
return result;
}
@NotNull
public static PlotFunctionLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth) {
final PlotFunctionLineDef result = new PlotFunctionLineDef();
result.lineColor = lineColor;
result.lineStyle = lineStyle;
result.lineWidth = lineWidth;
return result;
}
@NotNull
public static PlotFunctionLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth, @NotNull PlotFunctionLineColorType lineColorType) {
final PlotFunctionLineDef result = new PlotFunctionLineDef();
result.lineColor = lineColor;
result.lineColorType = lineColorType;
result.lineStyle = lineStyle;
result.lineWidth = lineWidth;
return result;
}
@NotNull
public static PlotFunctionLineDef newDefaultInstance() {
return new PlotFunctionLineDef();
}
public int getLineColor() {
return lineColor;
}
@NotNull
public PlotLineStyle getLineStyle() {
return lineStyle;
}
public float getLineWidth() {
return lineWidth;
}
@NotNull
public PlotFunctionLineColorType getLineColorType() {
return lineColorType;
}
}

View File

@@ -1,11 +1,13 @@
package org.solovyev.android.calculator.plot;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 10/4/12
* Time: 10:08 PM
*/
public enum GraphLineColor {
public enum PlotLineColor {
// Color.WHITE
white(0xFFFFFFFF),
@@ -23,7 +25,7 @@ public enum GraphLineColor {
private final int color;
private GraphLineColor(int color) {
private PlotLineColor(int color) {
this.color = color;
}
@@ -31,4 +33,15 @@ public enum GraphLineColor {
public int getColor() {
return this.color;
}
@NotNull
public static PlotLineColor valueOf(int color) {
for (PlotLineColor plotLineColor : PlotLineColor.values()) {
if ( plotLineColor.color == color ) {
return plotLineColor;
}
}
return PlotLineColor.white;
}
}

View File

@@ -5,8 +5,8 @@ package org.solovyev.android.calculator.plot;
* Date: 1/5/13
* Time: 10:45 PM
*/
public enum PlotFunctionLineColorType {
public enum PlotLineColorType {
color_map,
solid;
monochrome,
color_map;
}

View File

@@ -0,0 +1,137 @@
package org.solovyev.android.calculator.plot;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 1/5/13
* Time: 7:41 PM
*/
public class PlotLineDef {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
@NotNull
public static final Float DEFAULT_LINE_WIDTH = -1f;
private static final int WHITE = 0xFFFFFFFF;
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@NotNull
private PlotLineColorType lineColorType = PlotLineColorType.monochrome;
private int lineColor = WHITE;
@NotNull
private PlotLineStyle lineStyle = PlotLineStyle.solid;
private float lineWidth = -DEFAULT_LINE_WIDTH;
private PlotLineDef() {
}
@NotNull
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle) {
final PlotLineDef result = new PlotLineDef();
result.lineColor = lineColor;
result.lineStyle = lineStyle;
return result;
}
@NotNull
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth) {
final PlotLineDef result = new PlotLineDef();
result.lineColor = lineColor;
result.lineStyle = lineStyle;
result.lineWidth = lineWidth;
return result;
}
@NotNull
public static PlotLineDef newInstance(int lineColor, @NotNull PlotLineStyle lineStyle, float lineWidth, @NotNull PlotLineColorType lineColorType) {
final PlotLineDef result = new PlotLineDef();
result.lineColor = lineColor;
result.lineColorType = lineColorType;
result.lineStyle = lineStyle;
result.lineWidth = lineWidth;
return result;
}
@NotNull
private PlotLineDef copy() {
final PlotLineDef copy = new PlotLineDef();
copy.lineColor = lineColor;
copy.lineColorType = lineColorType;
copy.lineStyle = lineStyle;
copy.lineWidth = lineWidth;
return copy;
}
@NotNull
public static PlotLineDef changeLineColor(@NotNull PlotLineDef plotLineDef, int newLineColor) {
final PlotLineDef result = plotLineDef.copy();
result.lineColor = newLineColor;
return result;
}
@NotNull
public static PlotLineDef changeLineColorType(@NotNull PlotLineDef plotLineDef, @NotNull PlotLineColorType newPlotLineColorType) {
final PlotLineDef result = plotLineDef.copy();
result.lineColorType = newPlotLineColorType;
return result;
}
@NotNull
public static PlotLineDef changeLineStyle(@NotNull PlotLineDef plotLineDef, @NotNull PlotLineStyle newPlotLineStyle) {
final PlotLineDef result = plotLineDef.copy();
result.lineStyle = newPlotLineStyle;
return result;
}
@NotNull
public static PlotLineDef changeColor(@NotNull PlotLineDef plotLineDef, int newLineColor) {
final PlotLineDef result = plotLineDef.copy();
result.lineColor = newLineColor;
return result;
}
@NotNull
public static PlotLineDef newDefaultInstance() {
return new PlotLineDef();
}
public int getLineColor() {
return lineColor;
}
@NotNull
public PlotLineStyle getLineStyle() {
return lineStyle;
}
public float getLineWidth() {
return lineWidth;
}
@NotNull
public PlotLineColorType getLineColorType() {
return lineColorType;
}
}

View File

@@ -4,6 +4,7 @@ import jscl.math.Generic;
import jscl.math.function.Constant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.text.StringUtils;
public class XyFunction {
@@ -15,22 +16,25 @@ public class XyFunction {
**********************************************************************
*/
@NotNull
private final String id;
@NotNull
private final Generic expression;
private Generic expression;
@NotNull
private String expressionString;
@Nullable
private final Constant xVariable;
private Constant xVariable;
@Nullable
private String xVariableName;
@Nullable
private final Constant yVariable;
private Constant yVariable;
private final boolean imag;
private boolean imag;
@Nullable
private String yVariableName;
@@ -62,6 +66,8 @@ public class XyFunction {
this.arity--;
}
this.id = this.expressionString + "_" + StringUtils.getNotEmpty(this.xVariableName, "") + "_" + StringUtils.getNotEmpty(this.yVariableName, "");
}
public boolean isImag() {
@@ -92,6 +98,11 @@ public class XyFunction {
return expressionString;
}
@NotNull
public String getId() {
return id;
}
@Nullable
public String getXVariableName() {
return xVariableName;
@@ -102,6 +113,7 @@ public class XyFunction {
return yVariableName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -109,20 +121,13 @@ public class XyFunction {
final XyFunction that = (XyFunction) o;
if (!expressionString.equals(that.expressionString)) return false;
if (xVariableName != null ? !xVariableName.equals(that.xVariableName) : that.xVariableName != null)
return false;
if (yVariableName != null ? !yVariableName.equals(that.yVariableName) : that.yVariableName != null)
return false;
if (!id.equals(that.id)) return false;
return true;
}
@Override
public int hashCode() {
int result = expressionString.hashCode();
result = 31 * result + (xVariableName != null ? xVariableName.hashCode() : 0);
result = 31 * result + (yVariableName != null ? yVariableName.hashCode() : 0);
return result;
return id.hashCode();
}
}