From 769d8068f22c9913a76936d1a153855727b28c3d Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 26 Sep 2011 23:46:47 +0400 Subject: [PATCH] preferences added --- res/values/default_values.xml | 6 ++ res/xml/preferences.xml | 16 ++-- .../calculator/CalculatorActivity.java | 14 ++- .../calculator/CalculatorEditText.java | 50 ++++++---- .../android/calculator/CalculatorModel.java | 11 ++- .../android/calculator/CalculatorView.java | 5 + .../DragButtonCalibrationActivity.java | 6 +- .../calculator/EditorHistoryState.java | 3 +- .../calculator/ToJsclPreprocessor.java | 1 + .../android/calculator/math/Factorial.java | 7 +- .../view/prefs/AbstractDialogPreference.java | 22 ++++- .../prefs/FloatRangeSeekBarPreference.java | 4 +- .../prefs/IntegerRangeSeekBarPreference.java | 4 +- .../prefs/NumberPickerDialogPreference.java | 92 +++++++++++++++++++ .../view/prefs/RangeSeekBarPreference.java | 2 - .../android/view/prefs/SeekBarPreference.java | 14 +-- .../org/solovyev/common/BooleanMapper.java | 34 +++++++ .../solovyev/common/FloatIntervalMapper.java | 37 -------- .../common/GenericIntervalMapper.java | 39 ++++++++ .../common/IntegerIntervalMapper.java | 35 ------- .../solovyev/common/NumberIntervalMapper.java | 21 +++++ .../org/solovyev/common/NumberMapper.java | 42 +++++++++ .../org/solovyev/common/NumberParser.java | 45 +++++++++ .../common/math/ValueOfFormatter.java | 13 ++- .../calculator/CalculatorModelTest.java | 4 - .../calculator/ToJsclPreprocessorTest.java | 6 ++ .../common/FloatIntervalMapperTest.java | 4 +- 27 files changed, 404 insertions(+), 133 deletions(-) create mode 100644 src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java create mode 100644 src/main/java/org/solovyev/common/BooleanMapper.java delete mode 100644 src/main/java/org/solovyev/common/FloatIntervalMapper.java create mode 100644 src/main/java/org/solovyev/common/GenericIntervalMapper.java delete mode 100644 src/main/java/org/solovyev/common/IntegerIntervalMapper.java create mode 100644 src/main/java/org/solovyev/common/NumberIntervalMapper.java create mode 100644 src/main/java/org/solovyev/common/NumberMapper.java create mode 100644 src/main/java/org/solovyev/common/NumberParser.java diff --git a/res/values/default_values.xml b/res/values/default_values.xml index 751c789c..735826ac 100644 --- a/res/values/default_values.xml +++ b/res/values/default_values.xml @@ -12,4 +12,10 @@ 0;45 org.solovyev.android.calculator.DragButtonCalibrationActivity_duration 40;2500 + + org.solovyev.android.calculator.CalculatorModel_result_precision + 5 + + org.solovyev.android.calculator.CalculatorModel_color_display + true \ No newline at end of file diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 899bdca0..eef77cd2 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -4,14 +4,16 @@ xmlns:range="http://schemas.android.com/apk/res/org.solovyev.android.calculator"> - + + integerNumberMapper = new NumberMapper(Integer.class); + this.calculatorModel.setNumberOfFractionDigits(integerNumberMapper.parseValue(sharedPreferences.getString(this.getString(R.string.p_calc_result_precision_key), this.getString(R.string.p_calc_result_precision)))); + + final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display)); + assert colorExpressionsInBracketsDefault != null; + this.calculatorView.getEditor().setHighlightExpressionInBrackets(sharedPreferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault)); } } \ No newline at end of file diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java b/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java index c060a77a..02c8b140 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorEditText.java @@ -22,6 +22,7 @@ import org.solovyev.android.calculator.math.MathEntityType; */ public class CalculatorEditText extends EditText { + private boolean highlightExpressionInBrackets = true; public CalculatorEditText(Context context) { super(context); @@ -42,30 +43,36 @@ public class CalculatorEditText extends EditText { public void redraw() { String text = getText().toString(); + int selectionStart = getSelectionStart(); int selectionEnd = getSelectionEnd(); - int maxNumberOfOpenGroupSymbols = 0; - int numberOfOpenGroupSymbols = 0; - for (int i = 0; i < text.length(); i++) { - char ch = text.charAt(i); - if (MathEntityType.openGroupSymbols.contains(ch)) { - numberOfOpenGroupSymbols++; - maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols); - } else if (MathEntityType.closeGroupSymbols.contains(ch)) { - numberOfOpenGroupSymbols--; + if (highlightExpressionInBrackets) { + + int maxNumberOfOpenGroupSymbols = 0; + int numberOfOpenGroupSymbols = 0; + for (int i = 0; i < text.length(); i++) { + char ch = text.charAt(i); + if (MathEntityType.openGroupSymbols.contains(ch)) { + numberOfOpenGroupSymbols++; + maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols); + } else if (MathEntityType.closeGroupSymbols.contains(ch)) { + numberOfOpenGroupSymbols--; + } } - } - if (maxNumberOfOpenGroupSymbols > 0) { + if (maxNumberOfOpenGroupSymbols > 0) { - final StringBuilder sb = new StringBuilder(); + final StringBuilder sb = new StringBuilder(); - processGroup(sb, text, 0, 0, maxNumberOfOpenGroupSymbols); + processGroup(sb, text, 0, 0, maxNumberOfOpenGroupSymbols); - Log.d(CalculatorEditText.class.getName(), sb.toString()); + Log.d(CalculatorEditText.class.getName(), sb.toString()); - super.setText(Html.fromHtml(sb.toString()), BufferType.EDITABLE); + super.setText(Html.fromHtml(sb.toString()), BufferType.EDITABLE); + } else { + super.setText(text, BufferType.EDITABLE); + } } else { super.setText(text, BufferType.EDITABLE); } @@ -106,10 +113,19 @@ public class CalculatorEditText extends EditText { double c = 1; - int i = ((int)(255 * c)) * numberOfOpenings / (numberOfOpenGroupSymbols + 1); + int i = ((int) (255 * c)) * numberOfOpenings / (numberOfOpenGroupSymbols + 1); - int result = Color.rgb( Color.red(baseColor) - i, Color.green(baseColor) - i, Color.blue(baseColor) - i); + int result = Color.rgb(Color.red(baseColor) - i, Color.green(baseColor) - i, Color.blue(baseColor) - i); return "#" + Integer.toHexString(result).substring(2); } + + public boolean isHighlightExpressionInBrackets() { + return highlightExpressionInBrackets; + } + + public void setHighlightExpressionInBrackets(boolean highlightExpressionInBrackets) { + this.highlightExpressionInBrackets = highlightExpressionInBrackets; + redraw(); + } } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java index d87a0a2b..419c9c3c 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java @@ -23,7 +23,7 @@ public class CalculatorModel { @NotNull private Interpreter interpreter; - private int NUMBER_OF_FRACTION_DIGITS = 5; + private int numberOfFractionDigits = 5; @NotNull public Preprocessor preprocessor = new ToJsclPreprocessor(); @@ -96,7 +96,7 @@ public class CalculatorModel { private Double round(@NotNull String result) { final Double dResult = Double.valueOf(result); - return MathUtils.round(dResult, NUMBER_OF_FRACTION_DIGITS); + return MathUtils.round(dResult, numberOfFractionDigits); } public static class ParseException extends SersoException { @@ -105,4 +105,11 @@ public class CalculatorModel { } } + public int getNumberOfFractionDigits() { + return numberOfFractionDigits; + } + + public void setNumberOfFractionDigits(int numberOfFractionDigits) { + this.numberOfFractionDigits = numberOfFractionDigits; + } } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorView.java b/src/main/java/org/solovyev/android/calculator/CalculatorView.java index 54d288e1..2824c692 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorView.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorView.java @@ -285,4 +285,9 @@ public class CalculatorView implements CursorControl, HistoryControl statData, @NotNull SharedPreferences.Editor editor, @NotNull PreferenceType preferenceType) { - final Mapper> mapper = new FloatIntervalMapper(); + final Mapper> mapper = new NumberIntervalMapper(Float.class); for (Map.Entry entry : statData.entrySet()) { final float min = (float) entry.getValue().getMean() - 2 * (float) entry.getValue().getStandardDeviation(); final float max = (float) entry.getValue().getMean() + 2 * (float) entry.getValue().getStandardDeviation(); @@ -195,7 +195,7 @@ public class DragButtonCalibrationActivity extends Activity { public static Preferences getPreferences(@NotNull Context context) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - final Mapper> mapper = new FloatIntervalMapper(); + final Mapper> mapper = new NumberIntervalMapper(Float.class); final Preferences result = new Preferences(); diff --git a/src/main/java/org/solovyev/android/calculator/EditorHistoryState.java b/src/main/java/org/solovyev/android/calculator/EditorHistoryState.java index a97dbc6a..4135bc3e 100644 --- a/src/main/java/org/solovyev/android/calculator/EditorHistoryState.java +++ b/src/main/java/org/solovyev/android/calculator/EditorHistoryState.java @@ -22,10 +22,11 @@ public class EditorHistoryState { this.text = text; } - public void setText(String text) { + public void setText(@Nullable String text) { this.text = text; } + @Nullable public String getText() { return text; } diff --git a/src/main/java/org/solovyev/android/calculator/ToJsclPreprocessor.java b/src/main/java/org/solovyev/android/calculator/ToJsclPreprocessor.java index d1d6d1b5..116a2862 100644 --- a/src/main/java/org/solovyev/android/calculator/ToJsclPreprocessor.java +++ b/src/main/java/org/solovyev/android/calculator/ToJsclPreprocessor.java @@ -1,6 +1,7 @@ /* * Copyright (c) 2009-2011. Created by serso aka se.solovyev. * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org */ package org.solovyev.android.calculator; diff --git a/src/main/java/org/solovyev/android/calculator/math/Factorial.java b/src/main/java/org/solovyev/android/calculator/math/Factorial.java index 03cfba3f..15f35452 100644 --- a/src/main/java/org/solovyev/android/calculator/math/Factorial.java +++ b/src/main/java/org/solovyev/android/calculator/math/Factorial.java @@ -1,7 +1,12 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + package org.solovyev.android.calculator.math; import jscl.math.Generic; -import jscl.math.JSCLInteger; import jscl.math.NotIntegrableException; import jscl.math.Variable; import jscl.math.function.Function; diff --git a/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java b/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java index 809a93f6..78f9a187 100644 --- a/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/AbstractDialogPreference.java @@ -3,6 +3,7 @@ package org.solovyev.android.view.prefs; import android.content.Context; import android.preference.DialogPreference; import android.util.AttributeSet; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.LinearLayout; @@ -19,7 +20,10 @@ import org.solovyev.common.utils.Mapper; public abstract class AbstractDialogPreference extends DialogPreference { @NotNull - protected static final String androidns = "http://schemas.android.com/apk/res/android"; + protected final static String localNameSpace = "http://schemas.android.com/apk/res/org.solovyev.android.calculator"; + + @NotNull + protected final static String androidns = "http://schemas.android.com/apk/res/android"; @NotNull protected TextView valueTextView; @@ -127,18 +131,26 @@ public abstract class AbstractDialogPreference extends DialogPreference { } protected void persistValue(@Nullable T value) { + Log.d(AbstractDialogPreference.class.getName(), "Trying to persist value: " + value); + this.value = value; + + Log.d(AbstractDialogPreference.class.getName(), "android.preference.Preference.callChangeListener()"); if (callChangeListener(value)) { + Log.d(AbstractDialogPreference.class.getName(), "android.preference.Preference.shouldPersist()"); if (shouldPersist()) { + Log.d(AbstractDialogPreference.class.getName(), "org.solovyev.android.view.prefs.AbstractDialogPreference.persist()"); persist(value); } } } private void persist(@Nullable T value) { - final String toBePersistedString = getMapper().formatValue(value); - if (toBePersistedString != null) { - if ( callChangeListener(value) ) { - persistString(toBePersistedString); + if (value != null) { + final String toBePersistedString = getMapper().formatValue(value); + if (toBePersistedString != null) { + if ( callChangeListener(toBePersistedString) ) { + persistString(toBePersistedString); + } } } } diff --git a/src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java b/src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java index 90e804eb..91072fbb 100644 --- a/src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java @@ -9,7 +9,7 @@ package org.solovyev.android.view.prefs; import android.content.Context; import android.util.AttributeSet; import org.jetbrains.annotations.NotNull; -import org.solovyev.common.FloatIntervalMapper; +import org.solovyev.common.NumberIntervalMapper; import org.solovyev.common.utils.Interval; import org.solovyev.common.utils.Mapper; @@ -27,6 +27,6 @@ public class FloatRangeSeekBarPreference extends RangeSeekBarPreference { @NotNull @Override protected Mapper> getMapper() { - return new FloatIntervalMapper() ; + return new NumberIntervalMapper(Float.class); } } diff --git a/src/main/java/org/solovyev/android/view/prefs/IntegerRangeSeekBarPreference.java b/src/main/java/org/solovyev/android/view/prefs/IntegerRangeSeekBarPreference.java index e0183685..c24dd8fa 100644 --- a/src/main/java/org/solovyev/android/view/prefs/IntegerRangeSeekBarPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/IntegerRangeSeekBarPreference.java @@ -9,7 +9,7 @@ package org.solovyev.android.view.prefs; import android.content.Context; import android.util.AttributeSet; import org.jetbrains.annotations.NotNull; -import org.solovyev.common.IntegerIntervalMapper; +import org.solovyev.common.NumberIntervalMapper; import org.solovyev.common.utils.*; /** @@ -26,7 +26,7 @@ public class IntegerRangeSeekBarPreference extends RangeSeekBarPreference> getMapper() { - return new IntegerIntervalMapper(); + return new NumberIntervalMapper(Integer.class); } } diff --git a/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java b/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java new file mode 100644 index 00000000..861817a3 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.view.prefs; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.Gravity; +import android.widget.LinearLayout; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.view.widgets.NumberPicker; +import org.solovyev.common.GenericIntervalMapper; +import org.solovyev.common.utils.Interval; +import org.solovyev.common.utils.Mapper; + +/** + * User: serso + * Date: 9/26/11 + * Time: 10:31 PM + */ +public class NumberPickerDialogPreference extends AbstractDialogPreference implements NumberPicker.OnChangedListener { + + @NotNull + private NumberPicker numberPicker; + + @NotNull + private final Interval boundaries; + + public NumberPickerDialogPreference(Context context, AttributeSet attrs) { + super(context, attrs, null); + + //noinspection ConstantConditions + boundaries = new GenericIntervalMapper(getMapper()).parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); + + createPreferenceView(); + } + + @NotNull + @Override + protected LinearLayout onCreateDialogView() { + final LinearLayout result = super.onCreateDialogView(); + + createPreferenceView(); + initPreferenceView(); + + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); + params.gravity = Gravity.CENTER; + result.addView(numberPicker, params); + + return result; + } + + protected void createPreferenceView() { + this.numberPicker = new NumberPicker(context); + this.numberPicker.setOnChangeListener(this); + + initPreferenceView(); + } + + @Override + protected void initPreferenceView() { + if (value != null) { + numberPicker.setRange(boundaries.getLeftBorder(), boundaries.getRightBorder()); + numberPicker.setCurrent(value); + } + } + + @NotNull + @Override + protected Mapper getMapper() { + return new Mapper() { + @Override + public String formatValue(@Nullable Integer value) throws IllegalArgumentException { + return String.valueOf(value); + } + + @Override + public Integer parseValue(@Nullable String value) throws IllegalArgumentException { + return Integer.valueOf(value); + } + }; + } + + @Override + public void onChanged(NumberPicker picker, int oldVal, int newVal) { + persistValue(newVal); + } +} diff --git a/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java b/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java index bd3c1bac..c5019b01 100644 --- a/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java @@ -16,8 +16,6 @@ import org.solovyev.common.utils.NumberInterval; */ public abstract class RangeSeekBarPreference extends AbstractDialogPreference> implements AbstractRangeSeekBar.OnRangeSeekBarChangeListener { - public final static String localNameSpace = "http://schemas.android.com/apk/res/org.solovyev.android.calculator"; - @NotNull private AbstractRangeSeekBar rangeSeekBar; diff --git a/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java b/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java index 67efa38b..bd3621ec 100644 --- a/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/SeekBarPreference.java @@ -11,7 +11,7 @@ import android.util.AttributeSet; import android.widget.LinearLayout; import android.widget.SeekBar; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import org.solovyev.common.NumberMapper; import org.solovyev.common.utils.Mapper; @@ -60,17 +60,7 @@ public class SeekBarPreference extends AbstractDialogPreference impleme @NotNull @Override protected Mapper getMapper() { - return new Mapper() { - @Override - public String formatValue(@Nullable Integer integer) throws IllegalArgumentException { - return integer == null ? null : String.valueOf(integer); - } - - @Override - public Integer parseValue(@Nullable String s) throws IllegalArgumentException { - return s == null ? null : Integer.valueOf(s); - } - }; + return new NumberMapper(Integer.class); } public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) { diff --git a/src/main/java/org/solovyev/common/BooleanMapper.java b/src/main/java/org/solovyev/common/BooleanMapper.java new file mode 100644 index 00000000..fac16ace --- /dev/null +++ b/src/main/java/org/solovyev/common/BooleanMapper.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.common; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.math.ValueOfFormatter; +import org.solovyev.common.utils.Formatter; +import org.solovyev.common.utils.Mapper; + +/** + * User: serso + * Date: 9/26/11 + * Time: 11:27 PM + */ +public class BooleanMapper implements Mapper{ + + @NotNull + private final Formatter formatter = new ValueOfFormatter(); + + @Override + public String formatValue(@Nullable Boolean value) throws IllegalArgumentException { + return formatter.formatValue(value); + } + + @Override + public Boolean parseValue(@Nullable String value) throws IllegalArgumentException { + return value == null ? null : Boolean.valueOf(value); + } +} diff --git a/src/main/java/org/solovyev/common/FloatIntervalMapper.java b/src/main/java/org/solovyev/common/FloatIntervalMapper.java deleted file mode 100644 index 75d7940a..00000000 --- a/src/main/java/org/solovyev/common/FloatIntervalMapper.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.common; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.common.math.ValueOfFormatter; -import org.solovyev.common.utils.Formatter; -import org.solovyev.common.utils.Parser; - -/** - * User: serso - * Date: 9/21/11 - * Time: 12:06 AM - */ -public class FloatIntervalMapper extends AbstractIntervalMapper { - @NotNull - @Override - protected Formatter getFormatter() { - return new ValueOfFormatter(); - } - - @NotNull - @Override - protected Parser getParser() { - return new Parser() { - @Override - public Float parseValue(@Nullable String s) throws IllegalArgumentException { - return Float.valueOf(s); - } - }; - } -} diff --git a/src/main/java/org/solovyev/common/GenericIntervalMapper.java b/src/main/java/org/solovyev/common/GenericIntervalMapper.java new file mode 100644 index 00000000..f2a6414b --- /dev/null +++ b/src/main/java/org/solovyev/common/GenericIntervalMapper.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.common; + +import org.jetbrains.annotations.NotNull; +import org.solovyev.common.utils.Formatter; +import org.solovyev.common.utils.Mapper; +import org.solovyev.common.utils.Parser; + +/** + * User: serso + * Date: 9/26/11 + * Time: 10:45 PM + */ +public class GenericIntervalMapper extends AbstractIntervalMapper { + + @NotNull + private final Mapper mapper; + + public GenericIntervalMapper(@NotNull Mapper mapper) { + this.mapper = mapper; + } + + @NotNull + @Override + protected Formatter getFormatter() { + return mapper; + } + + @NotNull + @Override + protected Parser getParser() { + return mapper; + } +} diff --git a/src/main/java/org/solovyev/common/IntegerIntervalMapper.java b/src/main/java/org/solovyev/common/IntegerIntervalMapper.java deleted file mode 100644 index b87141b1..00000000 --- a/src/main/java/org/solovyev/common/IntegerIntervalMapper.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.common; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.common.math.ValueOfFormatter; -import org.solovyev.common.utils.*; - -/** -* User: serso -* Date: 9/20/11 -* Time: 11:56 PM -*/ -public class IntegerIntervalMapper extends AbstractIntervalMapper { - - @NotNull - protected Formatter getFormatter() { - return new ValueOfFormatter(); - } - - @NotNull - protected Parser getParser() { - return new Parser() { - @Override - public Integer parseValue(@Nullable String s) throws IllegalArgumentException { - return Integer.valueOf(s); - } - }; - } -} diff --git a/src/main/java/org/solovyev/common/NumberIntervalMapper.java b/src/main/java/org/solovyev/common/NumberIntervalMapper.java new file mode 100644 index 00000000..2f55d1e0 --- /dev/null +++ b/src/main/java/org/solovyev/common/NumberIntervalMapper.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.common; + +import org.jetbrains.annotations.NotNull; + +/** +* User: serso +* Date: 9/20/11 +* Time: 11:56 PM +*/ +public class NumberIntervalMapper extends GenericIntervalMapper { + + public NumberIntervalMapper(@NotNull Class clazz) { + super(new NumberMapper(clazz)); + } +} diff --git a/src/main/java/org/solovyev/common/NumberMapper.java b/src/main/java/org/solovyev/common/NumberMapper.java new file mode 100644 index 00000000..5814f124 --- /dev/null +++ b/src/main/java/org/solovyev/common/NumberMapper.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.common; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.math.ValueOfFormatter; +import org.solovyev.common.utils.Formatter; +import org.solovyev.common.utils.Mapper; +import org.solovyev.common.utils.Parser; + +/** + * User: serso + * Date: 9/26/11 + * Time: 11:10 PM + */ +public class NumberMapper implements Mapper{ + + @NotNull + private final Formatter formatter = new ValueOfFormatter(); + + @NotNull + private final Parser parser; + + public NumberMapper(@NotNull Class clazz) { + this.parser = new NumberParser(clazz); + } + + @Override + public String formatValue(@Nullable T value) throws IllegalArgumentException { + return formatter.formatValue(value); + } + + @Override + public T parseValue(@Nullable String value) throws IllegalArgumentException { + return this.parser.parseValue(value); + } +} diff --git a/src/main/java/org/solovyev/common/NumberParser.java b/src/main/java/org/solovyev/common/NumberParser.java new file mode 100644 index 00000000..ce4bdb33 --- /dev/null +++ b/src/main/java/org/solovyev/common/NumberParser.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.common; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.utils.Parser; + +/** + * User: serso + * Date: 9/26/11 + * Time: 11:07 PM + */ +public class NumberParser implements Parser { + + @NotNull + private final Class clazz; + + public NumberParser(@NotNull Class clazz) { + this.clazz = clazz; + } + + @Override + public T parseValue(@Nullable String value) throws IllegalArgumentException { + T result; + + if (value != null) { + if (this.clazz.equals(Integer.class)) { + result = (T) Integer.valueOf(value); + } else if (this.clazz.equals(Float.class)) { + result = (T) Float.valueOf(value); + } else { + throw new UnsupportedOperationException(this.clazz + " is not supported!"); + } + } else { + result = null; + } + + return result; + } +} diff --git a/src/main/java/org/solovyev/common/math/ValueOfFormatter.java b/src/main/java/org/solovyev/common/math/ValueOfFormatter.java index 82f3f401..03db764d 100644 --- a/src/main/java/org/solovyev/common/math/ValueOfFormatter.java +++ b/src/main/java/org/solovyev/common/math/ValueOfFormatter.java @@ -16,8 +16,19 @@ import org.solovyev.common.utils.Formatter; */ public class ValueOfFormatter implements Formatter{ + private final boolean processNulls; + + public ValueOfFormatter() { + this(false); + } + + public ValueOfFormatter(boolean processNulls) { + this.processNulls = processNulls; + } + + @Override public String formatValue(@Nullable T t) throws IllegalArgumentException { - return String.valueOf(t); + return t == null ? (processNulls ? String.valueOf(t) : null) : String.valueOf(t); } } diff --git a/src/test/java/org/solovyev/android/calculator/CalculatorModelTest.java b/src/test/java/org/solovyev/android/calculator/CalculatorModelTest.java index 444dd996..1b397501 100644 --- a/src/test/java/org/solovyev/android/calculator/CalculatorModelTest.java +++ b/src/test/java/org/solovyev/android/calculator/CalculatorModelTest.java @@ -5,10 +5,6 @@ package org.solovyev.android.calculator; -import de.congrace.exp4j.Calculable; -import de.congrace.exp4j.ExpressionBuilder; -import de.congrace.exp4j.UnknownFunctionException; -import de.congrace.exp4j.UnparsableExpressionException; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/org/solovyev/android/calculator/ToJsclPreprocessorTest.java b/src/test/java/org/solovyev/android/calculator/ToJsclPreprocessorTest.java index 6a2a0f58..ac878822 100644 --- a/src/test/java/org/solovyev/android/calculator/ToJsclPreprocessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/ToJsclPreprocessorTest.java @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + package org.solovyev.android.calculator; import org.junit.Assert; diff --git a/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java b/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java index 0db5633f..5c93cc98 100644 --- a/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java +++ b/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java @@ -2,7 +2,9 @@ package org.solovyev.common; import junit.framework.Assert; import org.junit.Test; +import org.solovyev.common.utils.Interval; import org.solovyev.common.utils.IntervalImpl; +import org.solovyev.common.utils.Mapper; /** * User: serso @@ -13,7 +15,7 @@ public class FloatIntervalMapperTest { @Test public void testParse() throws Exception { - final FloatIntervalMapper mapper = new FloatIntervalMapper(); + final Mapper> mapper = new NumberIntervalMapper(Float.class); Assert.assertEquals(new IntervalImpl(1.2f, 12.2f), mapper.parseValue("1.2;12.2")); Assert.assertEquals(new IntervalImpl(0f, 0f), mapper.parseValue("0;0"));