some classes moved to common
This commit is contained in:
parent
99605d989c
commit
a2fc94f9b6
2
pom.xml
2
pom.xml
@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.solovyev</groupId>
|
<groupId>org.solovyev</groupId>
|
||||||
<artifactId>common</artifactId>
|
<artifactId>common</artifactId>
|
||||||
<version>0.1</version>
|
<version>0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -16,8 +16,9 @@ import bsh.EvalError;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.view.*;
|
import org.solovyev.android.view.*;
|
||||||
import org.solovyev.util.StringUtils;
|
import org.solovyev.common.utils.Point2d;
|
||||||
import org.solovyev.util.math.Point2d;
|
import org.solovyev.common.utils.StringUtils;
|
||||||
|
import org.solovyev.common.utils.history.HistoryAction;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.Toast;
|
|
||||||
import bsh.EvalError;
|
import bsh.EvalError;
|
||||||
import bsh.Interpreter;
|
import bsh.Interpreter;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.solovyev.util.math.MathUtils;
|
import org.solovyev.common.utils.MathUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
|
@ -13,12 +13,13 @@ import android.widget.Toast;
|
|||||||
import bsh.EvalError;
|
import bsh.EvalError;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.util.StringUtils;
|
import org.solovyev.common.utils.MutableObject;
|
||||||
import org.solovyev.util.date.MutableObject;
|
import org.solovyev.common.utils.StringUtils;
|
||||||
|
import org.solovyev.common.utils.history.HistoryAction;
|
||||||
|
import org.solovyev.common.utils.history.HistoryHelper;
|
||||||
|
import org.solovyev.common.utils.history.SimpleHistoryHelper;
|
||||||
import org.solovyev.util.math.MathEntityType;
|
import org.solovyev.util.math.MathEntityType;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
* Date: 9/12/11
|
* Date: 9/12/11
|
||||||
@ -111,7 +112,7 @@ public class CalculatorView implements CursorControl{
|
|||||||
// do only if nothing was post delayed before current instance was posted
|
// do only if nothing was post delayed before current instance was posted
|
||||||
if (currentRunner.getObject() == this) {
|
if (currentRunner.getObject() == this) {
|
||||||
// actually nothing shall be logged while text operations are done
|
// actually nothing shall be logged while text operations are done
|
||||||
evaluate(editorStateAfter, false);
|
evaluate(editorStateAfter, true);
|
||||||
|
|
||||||
if (history.isRedoAvailable()) {
|
if (history.isRedoAvailable()) {
|
||||||
history.redo(getCurrentHistoryState());
|
history.redo(getCurrentHistoryState());
|
||||||
|
@ -16,8 +16,8 @@ import org.solovyev.android.view.*;
|
|||||||
import org.solovyev.common.collections.ManyValuedHashMap;
|
import org.solovyev.common.collections.ManyValuedHashMap;
|
||||||
import org.solovyev.common.collections.ManyValuedMap;
|
import org.solovyev.common.collections.ManyValuedMap;
|
||||||
import org.solovyev.common.utils.Interval;
|
import org.solovyev.common.utils.Interval;
|
||||||
import org.solovyev.util.math.MathUtils;
|
import org.solovyev.common.utils.MathUtils;
|
||||||
import org.solovyev.util.math.Point2d;
|
import org.solovyev.common.utils.Point2d;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
package org.solovyev.android.calculator;
|
|
||||||
|
|
||||||
public enum HistoryAction {
|
|
||||||
|
|
||||||
redo,
|
|
||||||
undo;
|
|
||||||
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package org.solovyev.android.calculator;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public interface HistoryHelper<T> {
|
|
||||||
|
|
||||||
boolean isUndoAvailable();
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
T undo (@Nullable T currentState);
|
|
||||||
|
|
||||||
boolean isRedoAvailable();
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
T redo (@Nullable T currentState);
|
|
||||||
|
|
||||||
boolean isActionAvailable(@NotNull HistoryAction historyAction);
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
T doAction(@NotNull HistoryAction historyAction, @Nullable T currentState);
|
|
||||||
|
|
||||||
void addState(@Nullable T currentState);
|
|
||||||
}
|
|
@ -1,91 +0,0 @@
|
|||||||
package org.solovyev.android.calculator;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public class SimpleHistoryHelper<T> implements HistoryHelper<T> {
|
|
||||||
|
|
||||||
private List<T> history = new ArrayList<T>();
|
|
||||||
|
|
||||||
private int currentStateIndex = -1;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T undo(@Nullable T currentState) {
|
|
||||||
if ( !isUndoAvailable() ) {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
}
|
|
||||||
|
|
||||||
currentStateIndex--;
|
|
||||||
|
|
||||||
return history.get(currentStateIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T redo(@Nullable T currentState) {
|
|
||||||
if (!isRedoAvailable()) {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
}
|
|
||||||
currentStateIndex++;
|
|
||||||
return history.get(currentStateIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addState(@Nullable T currentState) {
|
|
||||||
if (currentStateIndex == history.size() - 1) {
|
|
||||||
currentStateIndex++;
|
|
||||||
history.add(currentState);
|
|
||||||
} else {
|
|
||||||
assert currentStateIndex < history.size() - 1 : "Invalid history state index!";
|
|
||||||
currentStateIndex++;
|
|
||||||
history.set(currentStateIndex, currentState);
|
|
||||||
while( history.size() > currentStateIndex + 1 ) {
|
|
||||||
history.remove(history.size() - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isUndoAvailable() {
|
|
||||||
return currentStateIndex > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRedoAvailable() {
|
|
||||||
return currentStateIndex < history.size() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isActionAvailable(@NotNull HistoryAction historyAction) {
|
|
||||||
boolean result = false;
|
|
||||||
|
|
||||||
switch (historyAction) {
|
|
||||||
case undo:
|
|
||||||
result = isUndoAvailable();
|
|
||||||
break;
|
|
||||||
case redo:
|
|
||||||
result = isRedoAvailable();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T doAction(@NotNull HistoryAction historyAction, @Nullable T currentState) {
|
|
||||||
T result = null;
|
|
||||||
|
|
||||||
switch (historyAction) {
|
|
||||||
case undo:
|
|
||||||
result = undo(currentState);
|
|
||||||
break;
|
|
||||||
case redo:
|
|
||||||
result = redo(currentState);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,15 +17,15 @@
|
|||||||
package org.solovyev.android.view;
|
package org.solovyev.android.view;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.graphics.Paint.Style;
|
import android.graphics.Paint.Style;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.widget.Button;
|
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.content.res.Resources;
|
import android.widget.Button;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.solovyev.util.math.Point2d;
|
import org.solovyev.common.utils.Point2d;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTE: copied from com.android.calculator2.ColorButton
|
* NOTE: copied from com.android.calculator2.ColorButton
|
||||||
|
@ -8,9 +8,8 @@ import android.text.TextPaint;
|
|||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.calculator.R;
|
import org.solovyev.common.utils.Point2d;
|
||||||
import org.solovyev.util.StringUtils;
|
import org.solovyev.common.utils.StringUtils;
|
||||||
import org.solovyev.util.math.Point2d;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
package org.solovyev.android.view;
|
package org.solovyev.android.view;
|
||||||
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.widget.Button;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.solovyev.util.math.Point2d;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.solovyev.common.utils.Point2d;
|
||||||
|
|
||||||
public class DragButton extends ColorButton {
|
public class DragButton extends ColorButton {
|
||||||
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package org.solovyev.android.view;
|
package org.solovyev.android.view;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.solovyev.util.math.Point2d;
|
|
||||||
|
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.solovyev.common.utils.Point2d;
|
||||||
|
|
||||||
public class DragEvent {
|
public class DragEvent {
|
||||||
|
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package org.solovyev.android.view;
|
package org.solovyev.android.view;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.solovyev.android.calculator.DragButtonCalibrationActivity;
|
|
||||||
import org.solovyev.common.utils.Interval;
|
|
||||||
import org.solovyev.util.math.MathUtils;
|
|
||||||
import org.solovyev.util.math.Point2d;
|
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.solovyev.android.calculator.DragButtonCalibrationActivity;
|
||||||
|
import org.solovyev.common.utils.Interval;
|
||||||
|
import org.solovyev.common.utils.MathUtils;
|
||||||
|
import org.solovyev.common.utils.Point2d;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
package org.solovyev.util;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public class StringUtils {
|
|
||||||
|
|
||||||
public static boolean isEmpty ( @Nullable CharSequence s ){
|
|
||||||
return s == null || s.length() == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static String getNotEmpty ( @Nullable CharSequence s, @NotNull String defaultValue ){
|
|
||||||
return isEmpty(s) ? defaultValue : s.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
package org.solovyev.util.date;
|
|
||||||
|
|
||||||
public class DateUtils {
|
|
||||||
|
|
||||||
public static long MS_IN_SECONDS = 1000l;
|
|
||||||
|
|
||||||
public static long msToSeconds (long ms) {
|
|
||||||
return ms / 1000l;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package org.solovyev.util.date;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 9/13/11
|
|
||||||
* Time: 6:46 PM
|
|
||||||
*/
|
|
||||||
public class MutableObject<T> {
|
|
||||||
|
|
||||||
private T object;
|
|
||||||
|
|
||||||
public MutableObject() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public MutableObject(T object) {
|
|
||||||
this.object = object;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T getObject() {
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setObject(T object) {
|
|
||||||
this.object = object;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,102 +0,0 @@
|
|||||||
package org.solovyev.util.math;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class MathUtils {
|
|
||||||
|
|
||||||
public static final float MIN_AMOUNT = 0.05f;
|
|
||||||
|
|
||||||
public static double round(@NotNull Double value, int numberOfFractionDigits) {
|
|
||||||
double roundFactor = Math.pow(10, numberOfFractionDigits);
|
|
||||||
|
|
||||||
if (value < Double.MAX_VALUE / roundFactor) {
|
|
||||||
return ((double)Math.round(value * roundFactor)) / roundFactor;
|
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float getDistance(@NotNull Point2d startPoint,
|
|
||||||
@NotNull Point2d endPoint) {
|
|
||||||
return getNorm(subtract(endPoint, startPoint));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Point2d subtract(@NotNull Point2d p1, @NotNull Point2d p2) {
|
|
||||||
return new Point2d(p1.getX() - p2.getX(), p1.getY() - p2.getY());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Point2d sum(@NotNull Point2d p1, @NotNull Point2d p2) {
|
|
||||||
return new Point2d(p1.getX() + p2.getX(), p1.getY() + p2.getY());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float getNorm(@NotNull Point2d point) {
|
|
||||||
return (float) Math.pow(
|
|
||||||
Math.pow(point.getX(), 2) + Math.pow(point.getY(), 2), 0.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float getAngle(@NotNull Point2d startPoint,
|
|
||||||
@NotNull Point2d axisEndPoint, @NotNull Point2d endPoint) {
|
|
||||||
final Point2d axisVector = subtract(axisEndPoint, startPoint);
|
|
||||||
final Point2d vector = subtract(endPoint, startPoint);
|
|
||||||
|
|
||||||
double a_2 = Math.pow(getDistance(vector, axisVector), 2);
|
|
||||||
double b = getNorm(vector);
|
|
||||||
double b_2 = Math.pow(b, 2);
|
|
||||||
double c = getNorm(axisVector);
|
|
||||||
double c_2 = Math.pow(c, 2);
|
|
||||||
|
|
||||||
return (float) Math.acos((-a_2 + b_2 + c_2) / (2 * b * c));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static double countMean(@NotNull List<Double> objects) {
|
|
||||||
|
|
||||||
double sum = 0d;
|
|
||||||
for (Double object : objects) {
|
|
||||||
sum += object;
|
|
||||||
}
|
|
||||||
|
|
||||||
return objects.size() == 0 ? 0d : (sum / objects.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static double countStandardDeviation(@NotNull Double mean, @NotNull List<Double> objects) {
|
|
||||||
double sum = 0d;
|
|
||||||
|
|
||||||
for (Double object : objects) {
|
|
||||||
sum += Math.pow(object - mean, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return objects.size() == 0 ? 0d : Math.sqrt(sum / objects.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static StatData getStatData(@NotNull List<Double> objects) {
|
|
||||||
|
|
||||||
final double mean = countMean(objects);
|
|
||||||
final double standardDeviation = countStandardDeviation(mean, objects);
|
|
||||||
|
|
||||||
return new StatData(mean, standardDeviation);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class StatData {
|
|
||||||
|
|
||||||
private final double mean;
|
|
||||||
|
|
||||||
private final double standardDeviation;
|
|
||||||
|
|
||||||
public StatData(double mean, double standardDeviation) {
|
|
||||||
this.mean = mean;
|
|
||||||
this.standardDeviation = standardDeviation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getMean() {
|
|
||||||
return mean;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getStandardDeviation() {
|
|
||||||
return standardDeviation;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
package org.solovyev.util.math;
|
|
||||||
|
|
||||||
public class Point2d {
|
|
||||||
|
|
||||||
private float x = 0;
|
|
||||||
|
|
||||||
private float y = 0;
|
|
||||||
|
|
||||||
public Point2d() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Point2d( float x, float y ) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getX() {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setX(float x) {
|
|
||||||
this.x = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getY() {
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setY(float y) {
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Point2d [x=" + x + ", y=" + y + "]";
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user