Remove classes

This commit is contained in:
serso
2016-03-02 09:20:13 +01:00
parent 4fa55d6bbc
commit eac923830c
47 changed files with 191 additions and 1807 deletions

View File

@@ -48,6 +48,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.squareup.otto.Bus;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.floating.FloatingCalculatorService;
@@ -56,7 +58,6 @@ import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.calculator.view.ScreenMetrics;
import org.solovyev.android.calculator.wizard.CalculatorWizards;
import org.solovyev.android.wizard.Wizards;
import org.solovyev.common.JPredicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -245,19 +246,6 @@ public final class App {
}
}
public static <T> T find(@Nullable List<T> list, @Nonnull JPredicate<T> finder) {
if (list == null || list.isEmpty()) {
return null;
}
for (int i = 0; i < list.size(); i++) {
final T t = list.get(i);
if (finder.apply(t)) {
return t;
}
}
return null;
}
@Nullable
public static String find(@Nonnull List<String> tokens, @Nonnull String text, int position) {
for (int i = 0; i < tokens.size(); i++) {

View File

@@ -304,7 +304,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
b.withSystem(true);
b.withDescription(CalculatorMessages.getBundle().getString(CalculatorMessages.ans_description));
variablesRegistry.add(b.build().toJsclBuilder(), variable);
variablesRegistry.addOrUpdate(b.build().toJsclConstant(), variable);
}
@Subscribe

View File

@@ -22,6 +22,7 @@
package org.solovyev.android.calculator;
import com.google.common.collect.Iterables;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
import org.solovyev.android.calculator.math.MathType;
@@ -109,11 +110,11 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
startsWithFinder.setI(i);
int offset = 0;
String functionName = App.find(MathType.function.getTokens(engine), startsWithFinder);
String functionName = Iterables.find(MathType.function.getTokens(engine), startsWithFinder, null);
if (functionName == null) {
String operatorName = App.find(MathType.operator.getTokens(engine), startsWithFinder);
String operatorName = Iterables.find(MathType.operator.getTokens(engine), startsWithFinder, null);
if (operatorName == null) {
String varName = App.find(engine.getVariablesRegistry().getNames(), startsWithFinder);
String varName = Iterables.find(engine.getVariablesRegistry().getNames(), startsWithFinder, null);
if (varName != null) {
final IConstant var = engine.getVariablesRegistry().get(varName);
if (var != null) {

View File

@@ -38,7 +38,6 @@ import org.solovyev.android.calculator.variables.CppVariable;
import org.solovyev.android.calculator.variables.OldVars;
import org.solovyev.android.calculator.variables.VariableCategory;
import org.solovyev.android.io.FileSaver;
import org.solovyev.common.JBuilder;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -68,8 +67,8 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
super(mathEngine.getConstantsRegistry(), "c_var_description_");
}
public void add(@NonNull JBuilder<? extends IConstant> builder, @Nullable IConstant oldVariable) {
final IConstant variable = add(builder);
public void addOrUpdate(@Nonnull IConstant newVariable, @Nullable IConstant oldVariable) {
final IConstant variable = addOrUpdate(newVariable);
if (oldVariable == null) {
bus.post(new AddedEvent(variable));
} else {
@@ -96,7 +95,7 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
migrateOldVariables();
for (CppVariable variable : loadEntities(CppVariable.JSON_CREATOR)) {
addSafely(variable.toJsclBuilder());
addSafely(variable.toJsclConstant());
}
addSafely("x");
@@ -140,7 +139,7 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
private void addSafely(@Nonnull String name) {
if (!contains(name)) {
addSafely(CppVariable.builder(name).build().toJsclBuilder());
addSafely(CppVariable.builder(name).build().toJsclConstant());
}
}

View File

@@ -28,25 +28,22 @@ import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Handler;
import android.support.annotation.NonNull;
import com.squareup.otto.Bus;
import org.json.JSONArray;
import org.json.JSONException;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.AppModule;
import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.EntitiesRegistry;
import org.solovyev.android.calculator.ErrorReporter;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.json.Json;
import org.solovyev.android.calculator.json.Jsonable;
import org.solovyev.android.io.FileSaver;
import org.solovyev.android.io.FileSystem;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@@ -55,11 +52,6 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
public abstract class BaseEntitiesRegistry<T extends MathEntity> implements EntitiesRegistry<T> {
@Nonnull
@@ -181,8 +173,8 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
}
@Override
public T add(@Nonnull JBuilder<? extends T> builder) {
final T entity = mathRegistry.add(builder);
public T addOrUpdate(@Nonnull T t) {
final T entity = mathRegistry.addOrUpdate(t);
if (!entity.isSystem() && isInitialized()) {
save();
}
@@ -190,9 +182,9 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
}
@Nullable
protected T addSafely(@Nonnull JBuilder<? extends T> builder) {
protected T addSafely(@Nonnull T entity) {
try {
return add(builder);
return addOrUpdate(entity);
} catch (Exception e) {
errorReporter.onException(e);
}

View File

@@ -4,26 +4,21 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import jscl.math.function.CustomFunction;
import jscl.math.function.IFunction;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.json.Json;
import org.solovyev.android.calculator.json.Jsonable;
import org.solovyev.common.JBuilder;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nonnull;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.math.function.IFunction;
import static java.util.Arrays.asList;
public class CppFunction implements Jsonable, Parcelable {
@@ -185,7 +180,7 @@ public class CppFunction implements Jsonable, Parcelable {
}
@Nonnull
public JBuilder<? extends Function> toJsclBuilder() {
public CustomFunction.Builder toJsclBuilder() {
final CustomFunction.Builder builder = new CustomFunction.Builder(name, parameters, body);
builder.setDescription(description);
if (id != NO_ID) {

View File

@@ -57,7 +57,7 @@ public class EditFunctionFragment extends BaseFunctionFragment {
protected boolean applyData(@Nonnull @NonNull CppFunction function) {
try {
final Function oldFunction = isNewFunction() ? null : functionsRegistry.getById(function.id);
functionsRegistry.add(function.toJsclBuilder(), oldFunction);
functionsRegistry.addOrUpdate(function.toJsclBuilder().create(), oldFunction);
return true;
} catch (RuntimeException e) {
setError(bodyLabel, e.getLocalizedMessage());

View File

@@ -36,7 +36,6 @@ import org.solovyev.android.calculator.entities.Entities;
import org.solovyev.android.calculator.json.Json;
import org.solovyev.android.calculator.json.Jsonable;
import org.solovyev.android.io.FileSaver;
import org.solovyev.common.JBuilder;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
@@ -63,8 +62,8 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
super(mathEngine.getFunctionsRegistry(), "c_fun_description_");
}
public void add(@NonNull JBuilder<? extends Function> builder, @Nullable Function oldFunction) {
final Function function = add(builder);
public void addOrUpdate(@Nonnull Function newFunction, @Nullable Function oldFunction) {
final Function function = addOrUpdate(newFunction);
if (oldFunction == null) {
bus.post(new AddedEvent(function));
} else {
@@ -92,6 +91,15 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
setInitialized();
}
}
@Nullable
protected Function addSafely(@Nonnull CustomFunction.Builder builder) {
try {
return addSafely(builder.create());
} catch (Exception e) {
errorReporter.onException(e);
}
return null;
}
@Override
public void remove(@Nonnull Function function) {

View File

@@ -1,46 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 12/22/11
* Time: 9:21 PM
*/
public interface MathEntityBuilder<T extends MathEntity> extends JBuilder<T> {
@Nonnull
MathEntityBuilder<T> setName(@Nonnull String name);
@Nonnull
MathEntityBuilder<T> setDescription(@Nullable String description);
@Nonnull
MathEntityBuilder<T> setValue(@Nullable String value);
}

View File

@@ -11,7 +11,6 @@ import org.solovyev.android.Check;
import org.solovyev.android.calculator.functions.CppFunction;
import org.solovyev.android.calculator.json.Json;
import org.solovyev.android.calculator.json.Jsonable;
import org.solovyev.common.JBuilder;
import javax.annotation.Nonnull;
@@ -114,14 +113,8 @@ public class CppVariable implements Jsonable, Parcelable {
}
@Nonnull
public JBuilder<? extends IConstant> toJsclBuilder() {
return new JBuilder<IConstant>() {
@Nonnull
@Override
public IConstant create() {
return new JsclConstant(CppVariable.this);
}
};
public IConstant toJsclConstant() {
return new JsclConstant(CppVariable.this);
}
@Override

View File

@@ -198,7 +198,7 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
@Override
public void onClick(DialogInterface dialog, int which) {
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
variablesRegistry.remove(variable.toJsclBuilder().create());
variablesRegistry.remove(variable.toJsclConstant());
dismiss();
}
});
@@ -217,7 +217,7 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
.withValue(valueView.getText().toString())
.withDescription(descriptionView.getText().toString()).build();
final IConstant oldVariable = isNewVariable() ? null : variablesRegistry.getById(variable.id);
variablesRegistry.add(newVariable.toJsclBuilder(), oldVariable);
variablesRegistry.addOrUpdate(newVariable.toJsclConstant(), oldVariable);
return true;
} catch (RuntimeException e) {
setError(valueLabel, e.getLocalizedMessage());

View File

@@ -1,240 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.solovyev.common.Objects;
import org.solovyev.common.text.Mapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Base class for creating preferences with dialogs
*
* @param <T>
*/
public abstract class AbstractDialogPreference<T> extends DialogPreference {
@Nonnull
protected final static String localNameSpace = "http://schemas.android.com/apk/res-auto";
@Nonnull
protected final static String androidns = "http://schemas.android.com/apk/res/android";
@Nonnull
private static final String TAG = AbstractDialogPreference.class.getSimpleName();
@Nonnull
private final Context context;
@Nullable
private final String defaultStringValue;
private final boolean needValueText;
@Nonnull
private final Mapper<T> mapper;
@Nullable
private TextView valueTextView;
@Nullable
private String valueText;
@Nullable
private View preferenceView;
@Nullable
private String description;
@Nullable
private T value;
@Nullable
private T defaultValue;
public AbstractDialogPreference(Context context, AttributeSet attrs, @Nullable String defaultStringValue, boolean needValueText, @Nonnull Mapper<T> mapper) {
super(context, attrs);
this.context = context;
this.defaultStringValue = defaultStringValue;
this.needValueText = needValueText;
this.mapper = mapper;
final String defaultValueFromAttrs = attrs.getAttributeValue(androidns, "defaultValue");
if (defaultValueFromAttrs != null) {
defaultValue = getMapper().parseValue(defaultValueFromAttrs);
} else if (defaultStringValue != null) {
defaultValue = getMapper().parseValue(defaultStringValue);
} else {
throw new IllegalArgumentException();
}
description = attrs.getAttributeValue(androidns, "dialogMessage");
valueText = attrs.getAttributeValue(androidns, "text");
}
@Nullable
protected View getPreferenceView() {
return preferenceView;
}
@Nullable
public T getValue() {
return value;
}
public void setValue(@Nullable T value) {
this.value = value;
}
@Override
@Nonnull
protected final LinearLayout onCreateDialogView() {
if (shouldPersist()) {
value = getPersistedValue();
}
final LinearLayout result = new LinearLayout(context);
result.setOrientation(LinearLayout.VERTICAL);
result.setPadding(6, 6, 6, 6);
if (description != null) {
final TextView splashText = new TextView(context);
splashText.setText(description);
result.addView(splashText);
}
if (needValueText) {
valueTextView = new TextView(context);
valueTextView.setGravity(Gravity.CENTER_HORIZONTAL);
valueTextView.setTextSize(32);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
result.addView(valueTextView, params);
}
preferenceView = createPreferenceView(context);
initPreferenceView(preferenceView, value);
final LinearLayout.LayoutParams params = getParams();
if (params != null) {
result.addView(preferenceView, params);
} else {
result.addView(preferenceView);
}
return result;
}
@Nullable
protected abstract LinearLayout.LayoutParams getParams();
@Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
super.onSetInitialValue(restore, defaultValue);
if (restore) {
if (shouldPersist()) {
value = getPersistedValue();
} else {
value = this.defaultValue;
}
} else {
value = (T) defaultValue;
if (shouldPersist()) {
persist(this.value);
}
}
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
if (this.preferenceView != null) {
initPreferenceView(this.preferenceView, value);
}
}
/**
* Creates view which is responsible for changing preference value (for example, Spinner or EditText)
*
* @param context context
* @return view which changes the preference value
*/
@Nonnull
protected abstract View createPreferenceView(@Nonnull Context context);
/**
* @param v view to be filled with initial data (the one which has been created with {@link #createPreferenceView} method)
* @param value current preference value
*/
protected abstract void initPreferenceView(@Nonnull View v, @Nullable T value);
@Nullable
private T getPersistedValue() {
String persistedString = getPersistedString(defaultStringValue);
if (Objects.areEqual(persistedString, defaultStringValue)) {
return defaultValue;
} else {
return getMapper().parseValue(persistedString);
}
}
protected void persistValue(@Nullable T value) {
this.value = value;
if (!callChangeListener(value)) {
return;
}
if (!shouldPersist()) {
return;
}
persist(value);
}
private void persist(@Nullable T value) {
if (value != null) {
final String toBePersistedString = getMapper().formatValue(value);
if (toBePersistedString != null) {
if (callChangeListener(toBePersistedString)) {
persistString(toBePersistedString);
}
}
}
}
@Nullable
public String getValueText() {
return valueText;
}
protected void updateValueText(@Nonnull String text) {
if (valueTextView != null) {
valueTextView.setText(text);
}
}
@Nonnull
private Mapper<T> getMapper() {
return this.mapper;
}
}

View File

@@ -1,114 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import org.solovyev.common.text.NumberMapper;
import javax.annotation.Nonnull;
/* The following code was written by Matthew Wiggins
* and is released under the APACHE 2.0 license
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
public class SeekBarPreference extends AbstractDialogPreference<Integer> implements SeekBar.OnSeekBarChangeListener {
private int max = 0;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs, "50", true, NumberMapper.of(Integer.class));
max = attrs.getAttributeIntValue(androidns, "max", 100);
}
@Override
protected LinearLayout.LayoutParams getParams() {
return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
@Nonnull
@Override
protected View createPreferenceView(@Nonnull Context context) {
final SeekBar result = new SeekBar(context);
result.setOnSeekBarChangeListener(this);
return result;
}
@Override
protected void initPreferenceView(@Nonnull View v, Integer value) {
((SeekBar) v).setMax(max);
if (value != null) {
((SeekBar) v).setProgress(value);
setValueText(value);
}
}
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
setValueText(value);
persistValue(value);
}
private void setValueText(int value) {
String t = String.valueOf(value);
final String valueText = getValueText();
updateValueText(valueText == null ? t : t.concat(valueText));
}
public void onStartTrackingTouch(SeekBar seek) {
}
public void onStopTrackingTouch(SeekBar seek) {
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getProgress() {
final Integer value = getValue();
return value == null ? 0 : value;
}
public void setProgress(int progress) {
setValue(progress);
final View preferenceView = getPreferenceView();
if (preferenceView != null) {
((SeekBar) preferenceView).setProgress(progress);
}
}
}