new plotter

This commit is contained in:
Sergey Solovyev
2013-01-19 02:47:33 +04:00
parent 6af5239c6e
commit 05e9026048
13 changed files with 165 additions and 71 deletions

View File

@@ -62,8 +62,4 @@ public interface CalculatorPlotter {
void removeAllUnpinned();
void setPlotImag(boolean plotImag);
void setRealLineColor(@NotNull PlotLineColor realLineColor);
void setImagLineColor(@NotNull PlotLineColor imagLineColor);
}

View File

@@ -27,7 +27,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
@NotNull
private final Calculator calculator;
private final PlotResourceManager plotResourceManager = new PlotResourceManager();
private final PlotResourceManager resourceManager = new MapPlotResourceManager();
private boolean plot3d = false;
@@ -35,12 +35,6 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
private int arity = 0;
@NotNull
private PlotLineColor realLineColor;
@NotNull
private PlotLineColor imagLineColor;
public CalculatorPlotterImpl(@NotNull Calculator calculator) {
this.calculator = calculator;
}
@@ -84,13 +78,14 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
@NotNull
private PlotFunction newPlotFunction(@NotNull XyFunction xyFunction) {
return new PlotFunction(xyFunction);
return new PlotFunction(xyFunction, resourceManager.generateAndRegister());
}
@Override
public boolean addFunction(@NotNull PlotFunction plotFunction) {
synchronized (functions) {
if (!functions.contains(plotFunction)) {
resourceManager.register(plotFunction.getPlotLineDef());
functions.add(plotFunction);
onFunctionsChanged();
return true;
@@ -115,6 +110,8 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
}
}
resourceManager.unregister(function.getPlotLineDef());
return true;
} else {
return false;
@@ -137,7 +134,13 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
boolean changed = Iterables.removeIf(functions, new Predicate<PlotFunction>() {
@Override
public boolean apply(@Nullable PlotFunction function) {
return function != null && !function.isPinned();
boolean removed = function != null && !function.isPinned();
if ( removed ) {
resourceManager.unregister(function.getPlotLineDef());
}
return removed;
}
});
@@ -152,6 +155,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
synchronized (functions) {
boolean changed = functions.remove(plotFunction);
if (changed) {
resourceManager.unregister(plotFunction.getPlotLineDef());
onFunctionsChanged();
}
return changed;
@@ -191,6 +195,10 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
for (int i = 0; i < functions.size(); i++) {
final PlotFunction plotFunction = functions.get(i);
if (plotFunction.equals(newFunction)) {
resourceManager.unregister(plotFunction.getPlotLineDef());
resourceManager.register(newFunction.getPlotLineDef());
// update old function
functions.set(i, newFunction);
changed = true;
@@ -246,6 +254,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
@Override
public void clearAllFunctions() {
synchronized (functions) {
resourceManager.unregisterAll();
functions.clear();
onFunctionsChanged();
}
@@ -357,16 +366,6 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
}
}
@Override
public void setRealLineColor(@NotNull PlotLineColor realLineColor) {
this.realLineColor = realLineColor;
}
@Override
public void setImagLineColor(@NotNull PlotLineColor imagLineColor) {
this.imagLineColor = imagLineColor;
}
private boolean toggleImagFunctions(boolean show) {
boolean changed = false;

View File

@@ -0,0 +1,99 @@
package org.solovyev.android.calculator.plot;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* User: serso
* Date: 1/19/13
* Time: 12:48 AM
*/
public class MapPlotResourceManager implements PlotResourceManager {
@NotNull
private Map<PlotLineDef, Integer> counters = new HashMap<PlotLineDef, Integer>();
@NotNull
private final List<PlotLineDef> preparedLineDefs = new ArrayList<PlotLineDef>(PlotLineStyle.values().length * PlotLineColor.values().length);
public MapPlotResourceManager() {
for (PlotLineStyle plotLineStyle : PlotLineStyle.values()) {
for (PlotLineColor plotLineColor : PlotLineColor.values()) {
preparedLineDefs.add(PlotLineDef.newInstance(plotLineColor.getColor(), plotLineStyle));
}
}
}
@NotNull
@Override
public PlotLineDef generateAndRegister() {
synchronized (this) {
for (PlotLineDef lineDef : preparedLineDefs) {
final Integer counter = counters.get(lineDef);
if ( counter == null || counter.equals(0) ) {
register0(lineDef);
return lineDef;
}
}
return preparedLineDefs.get(0);
}
}
private void increaseCounter(@NotNull PlotLineDef lineDef) {
assert Thread.holdsLock(this);
Integer counter = counters.get(lineDef);
if ( counter == null ) {
counter = 0;
}
counter++;
counters.put(lineDef, counter);
}
private void decreaseCounter(@NotNull PlotLineDef lineDef) {
assert Thread.holdsLock(this);
Integer counter = counters.get(lineDef);
if (counter != null) {
counter--;
counters.put(lineDef, Math.max(counter, 0));
}
}
@Override
public void register(@NotNull PlotLineDef lineDef) {
synchronized (this) {
// we should check if specified line def is not ours, i.e. created by this class
for (PlotLineDef preparedLineDef : preparedLineDefs) {
if ( preparedLineDef == lineDef ) {
return;
}
}
register0(lineDef);
}
}
private void register0(@NotNull PlotLineDef lineDef) {
increaseCounter(lineDef);
}
@Override
public void unregister(@NotNull PlotLineDef lineDef) {
synchronized (this) {
decreaseCounter(lineDef);
}
}
@Override
public void unregisterAll() {
synchronized (this) {
counters.clear();
}
}
}

View File

@@ -32,9 +32,8 @@ public class PlotFunction {
@NotNull
private PlotFunction copy() {
final PlotFunction copy = new PlotFunction(this.xyFunction);
final PlotFunction copy = new PlotFunction(this.xyFunction, this.plotLineDef);
copy.plotLineDef = this.plotLineDef;
copy.pinned = this.pinned;
copy.visible = this.visible;

View File

@@ -141,4 +141,28 @@ public class PlotLineDef {
public PlotLineColorType getLineColorType() {
return lineColorType;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PlotLineDef)) return false;
PlotLineDef that = (PlotLineDef) o;
if (lineColor != that.lineColor) return false;
if (Float.compare(that.lineWidth, lineWidth) != 0) return false;
if (lineColorType != that.lineColorType) return false;
if (lineStyle != that.lineStyle) return false;
return true;
}
@Override
public int hashCode() {
int result = lineColorType.hashCode();
result = 31 * result + lineColor;
result = 31 * result + lineStyle.hashCode();
result = 31 * result + (lineWidth != +0.0f ? Float.floatToIntBits(lineWidth) : 0);
return result;
}
}

View File

@@ -1,10 +1,20 @@
package org.solovyev.android.calculator.plot;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 1/13/13
* Time: 8:19 PM
*/
class PlotResourceManager {
interface PlotResourceManager {
@NotNull
PlotLineDef generateAndRegister();
void register(@NotNull PlotLineDef lineDef);
void unregister(@NotNull PlotLineDef lineDef);
void unregisterAll();
}