From 1fe67e9183eed4dc58ea8812f47c40bfdbb0acaf Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 31 Jan 2016 10:23:57 +0100 Subject: [PATCH] Functions and operators should use toString in EntitiesFragment --- .../entities/BaseEntitiesFragment.java | 5 ++- .../functions/FunctionsFragment.java | 6 ++++ .../operators/OperatorsFragment.java | 6 ++++ .../variables/VariablesFragment.java | 35 +++++++++++-------- .../jscl/math/operator/PostfixFunction.java | 20 +---------- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java index 6257436b..2c82581d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java @@ -188,6 +188,9 @@ public abstract class BaseEntitiesFragment extends BaseFra @Nullable protected abstract String getDescription(@NonNull E entity); + @NonNull + protected abstract String getName(@Nonnull E entity); + protected abstract void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull E entity, @Nonnull MenuItem.OnMenuItemClickListener listener); protected abstract boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull E entity); @@ -209,7 +212,7 @@ public abstract class BaseEntitiesFragment extends BaseFra public void bind(@Nonnull E entity) { this.entity = entity; - textView.setText(entity.getName()); + textView.setText(getName(entity)); final String description = getDescription(entity); if (!Strings.isEmpty(description)) { diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java index 14391b9e..90691338 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java @@ -148,4 +148,10 @@ public class FunctionsFragment extends BaseEntitiesFragment { protected String getDescription(@NonNull Function function) { return registry.getDescription(function.getName()); } + + @NonNull + @Override + protected String getName(@Nonnull Function function) { + return function.toString(); + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java index e5da7c52..ffb72ca9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorsFragment.java @@ -100,5 +100,11 @@ public class OperatorsFragment extends BaseEntitiesFragment { } return postfixFunctionsRegistry.getDescription(name); } + + @NonNull + @Override + protected String getName(@Nonnull Operator operator) { + return operator.toString(); + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java index 2b6f358b..9edf2512 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java @@ -62,8 +62,8 @@ public class VariablesFragment extends BaseEntitiesFragment { public static boolean isValidValue(@Nonnull String value) { try { final PreparedExpression expression = ToJsclTextProcessor.getInstance().process(value); - final List constants = expression.getUndefinedVars(); - return constants.isEmpty(); + final List variables = expression.getUndefinedVars(); + return variables.isEmpty(); } catch (RuntimeException e) { return false; } @@ -112,44 +112,44 @@ public class VariablesFragment extends BaseEntitiesFragment { } @Override - protected Category getCategory(@Nonnull IConstant var) { - return registry.getCategory(var); + protected Category getCategory(@Nonnull IConstant variable) { + return registry.getCategory(variable); } @Override - protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull IConstant constant, @Nonnull MenuItem.OnMenuItemClickListener listener) { + protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull IConstant variable, @Nonnull MenuItem.OnMenuItemClickListener listener) { addMenu(menu, R.string.c_use, listener); - if (!constant.isSystem()) { + if (!variable.isSystem()) { addMenu(menu, R.string.c_edit, listener); addMenu(menu, R.string.c_remove, listener); } - if (!Strings.isEmpty(constant.getValue())) { + if (!Strings.isEmpty(variable.getValue())) { addMenu(menu, R.string.c_copy_value, listener); } } @Override - protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final IConstant constant) { + protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final IConstant variable) { FragmentActivity activity = getActivity(); switch (item.getItemId()) { case R.string.c_use: - onClick(constant); + onClick(variable); return true; case R.string.c_edit: - EditVariableFragment.showDialog(CppVariable.builder(constant).build(), activity); + EditVariableFragment.showDialog(CppVariable.builder(variable).build(), activity); return true; case R.string.c_remove: - EntityRemovalDialog.showForVariable(getActivity(), constant.getName(), new DialogInterface.OnClickListener() { + EntityRemovalDialog.showForVariable(getActivity(), variable.getName(), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Check.isTrue(which == DialogInterface.BUTTON_POSITIVE); - registry.remove(constant); + registry.remove(variable); } }); return true; case R.string.c_copy_value: - copyText(constant.getValue()); + copyText(variable.getValue()); return true; } return false; @@ -172,8 +172,13 @@ public class VariablesFragment extends BaseEntitiesFragment { @Nullable @Override - protected String getDescription(@NonNull IConstant constant) { - return registry.getDescription(constant.getName()); + protected String getDescription(@NonNull IConstant variable) { + return registry.getDescription(variable.getName()); } + @NonNull + @Override + protected String getName(@Nonnull IConstant variable) { + return variable.getName(); + } } diff --git a/jscl/src/main/java/jscl/math/operator/PostfixFunction.java b/jscl/src/main/java/jscl/math/operator/PostfixFunction.java index 1b1e8a8c..aff2174d 100644 --- a/jscl/src/main/java/jscl/math/operator/PostfixFunction.java +++ b/jscl/src/main/java/jscl/math/operator/PostfixFunction.java @@ -15,25 +15,7 @@ abstract class PostfixFunction extends Operator { } public String toString() { - final StringBuilder result = new StringBuilder(); - - /*try {*/ - result.append(formatParameter(0)); - /*} catch (NotIntegerException e) { - try { - final Variable v = parameters[0].variableValue(); - if (v instanceof Frac || v instanceof Pow) { - result.append(GenericVariable.valueOf(parameters[0])); - } else { - result.append(v); - } - } catch (NotVariableException e2) { - result.append(GenericVariable.valueOf(parameters[0])); - } - }*/ - result.append(getName()); - - return result.toString(); + return formatParameter(0) + getName(); } public final Generic numeric() {