Variables refactor
This commit is contained in:
@@ -1,15 +1,171 @@
|
||||
package org.solovyev.android.calculator.variables;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.function.CppFunction;
|
||||
import org.solovyev.android.calculator.json.Json;
|
||||
import org.solovyev.android.calculator.json.Jsonable;
|
||||
import org.solovyev.common.JBuilder;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
|
||||
public class CppVariable implements Jsonable {
|
||||
|
||||
public static final Json.Creator<CppVariable> JSON_CREATOR = new Json.Creator<CppVariable>() {
|
||||
@NonNull
|
||||
@Override
|
||||
public CppVariable create(@NonNull JSONObject json) throws JSONException {
|
||||
return new CppVariable(json);
|
||||
}
|
||||
};
|
||||
|
||||
private static final String JSON_NAME = "n";
|
||||
private static final String JSON_VALUE = "v";
|
||||
private static final String JSON_DESCRIPTION = "d";
|
||||
|
||||
protected int id = CppFunction.NO_ID;
|
||||
@Nonnull
|
||||
protected String name;
|
||||
@Nonnull
|
||||
protected String value = "";
|
||||
@Nonnull
|
||||
protected String description = "";
|
||||
protected boolean system;
|
||||
|
||||
private CppVariable(@Nonnull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
protected CppVariable(@Nonnull CppVariable that) {
|
||||
this.id = that.id;
|
||||
this.name = that.name;
|
||||
this.value = that.value;
|
||||
this.description = that.description;
|
||||
this.system = that.system;
|
||||
}
|
||||
|
||||
protected CppVariable(@NonNull IConstant that) {
|
||||
id = that.isIdDefined() ? that.getId() : CppFunction.NO_ID;
|
||||
name = that.getName();
|
||||
value = nullToEmpty(that.getValue());
|
||||
description = nullToEmpty(that.getDescription());
|
||||
system = that.isSystem();
|
||||
}
|
||||
|
||||
private CppVariable(@NonNull JSONObject json) throws JSONException {
|
||||
this.name = json.getString(JSON_NAME);
|
||||
this.value = json.optString(JSON_DESCRIPTION);
|
||||
this.description = json.optString(JSON_DESCRIPTION);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static CppVariable.Builder builder(@NonNull String name) {
|
||||
return new Builder(name);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Builder builder(@NonNull IConstant constant) {
|
||||
return new Builder(constant);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public JSONObject toJson() throws JSONException {
|
||||
return null;
|
||||
final JSONObject json = new JSONObject();
|
||||
json.put(JSON_NAME, name);
|
||||
if (!TextUtils.isEmpty(value)) {
|
||||
json.put(JSON_VALUE, value);
|
||||
}
|
||||
if (!TextUtils.isEmpty(description)) {
|
||||
json.put(JSON_DESCRIPTION, description);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public JBuilder<? extends IConstant> toJsclBuilder() {
|
||||
return new JBuilder<IConstant>() {
|
||||
@Nonnull
|
||||
@Override
|
||||
public IConstant create() {
|
||||
return new JsclConstant(CppVariable.this);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof CppVariable)) return false;
|
||||
|
||||
CppVariable that = (CppVariable) o;
|
||||
|
||||
if (id != that.id) return false;
|
||||
if (!name.equals(that.name)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id;
|
||||
result = 31 * result + name.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
@NonNull
|
||||
private final CppVariable variable;
|
||||
private boolean built;
|
||||
|
||||
private Builder(@NonNull String name) {
|
||||
variable = new CppVariable(name);
|
||||
}
|
||||
|
||||
private Builder(@NonNull IConstant constant) {
|
||||
variable = new CppVariable(constant);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder withDescription(@Nonnull String description) {
|
||||
Check.isTrue(!built);
|
||||
variable.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder withValue(@NonNull String value) {
|
||||
Check.isTrue(!built);
|
||||
variable.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder withSystem(boolean system) {
|
||||
Check.isTrue(!built);
|
||||
variable.system = system;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder withId(int id) {
|
||||
Check.isTrue(!built);
|
||||
variable.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CppVariable build() {
|
||||
built = true;
|
||||
return variable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,116 @@
|
||||
package org.solovyev.android.calculator.variables;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import org.solovyev.android.calculator.function.CppFunction;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
class JsclConstant extends CppVariable implements IConstant {
|
||||
|
||||
private Double doubleValue;
|
||||
private Constant constant;
|
||||
|
||||
JsclConstant(@Nonnull CppVariable variable) {
|
||||
super(variable);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Constant getConstant() {
|
||||
if (constant == null) {
|
||||
constant = new Constant(name);
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefined() {
|
||||
return !Strings.isNullOrEmpty(value);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Double getDoubleValue() {
|
||||
if (doubleValue != null) {
|
||||
return doubleValue;
|
||||
}
|
||||
if (!Strings.isNullOrEmpty(value)) {
|
||||
try {
|
||||
doubleValue = Double.valueOf(value);
|
||||
} catch (NumberFormatException e) {
|
||||
// do nothing - string is not a double
|
||||
}
|
||||
}
|
||||
return doubleValue;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String toJava() {
|
||||
return Strings.nullToEmpty(value);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return id == CppFunction.NO_ID ? null : id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(@Nonnull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdDefined() {
|
||||
return id != CppFunction.NO_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copy(@Nonnull MathEntity o) {
|
||||
if (!(o instanceof IConstant)) {
|
||||
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass());
|
||||
}
|
||||
final IConstant that = ((IConstant) o);
|
||||
this.name = that.getName();
|
||||
this.value = that.getValue();
|
||||
this.description = that.getDescription();
|
||||
this.system = that.isSystem();
|
||||
if (that.isIdDefined()) {
|
||||
this.id = that.getId();
|
||||
} else {
|
||||
this.id = CppFunction.NO_ID;
|
||||
}
|
||||
this.doubleValue = null;
|
||||
this.constant = null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* 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.variables;
|
||||
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.ExtendedConstant;
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
@Root(name = "var")
|
||||
public class OldVar implements IConstant {
|
||||
|
||||
@Transient
|
||||
private Integer id;
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
private String name;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String value;
|
||||
|
||||
@Element
|
||||
private boolean system;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Transient
|
||||
private Constant constant;
|
||||
|
||||
private OldVar() {
|
||||
}
|
||||
|
||||
private OldVar(@Nonnull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void copy(@Nonnull MathEntity o) {
|
||||
if (o instanceof IConstant) {
|
||||
final IConstant that = ((IConstant) o);
|
||||
this.name = that.getName();
|
||||
this.value = that.getValue();
|
||||
this.description = that.getDescription();
|
||||
this.system = that.isSystem();
|
||||
if (that.isIdDefined()) {
|
||||
this.id = that.getId();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Double getDoubleValue() {
|
||||
Double result = null;
|
||||
if (value != null) {
|
||||
try {
|
||||
result = Double.valueOf(value);
|
||||
} catch (NumberFormatException e) {
|
||||
// do nothing - string is not a double
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String toJava() {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public boolean isSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(@Nonnull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdDefined() {
|
||||
return this.id != null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Constant getConstant() {
|
||||
if (constant == null) {
|
||||
constant = new Constant(this.name);
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefined() {
|
||||
return !Strings.isEmpty(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ExtendedConstant.toString(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
OldVar var = (OldVar) o;
|
||||
|
||||
if (!name.equals(var.name)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
|
||||
public static class Builder implements JBuilder<OldVar>, MathEntityBuilder<OldVar> {
|
||||
|
||||
@Nonnull
|
||||
private String name;
|
||||
|
||||
@Nullable
|
||||
private String value;
|
||||
|
||||
private boolean system = false;
|
||||
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Nullable
|
||||
private Integer id;
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
|
||||
public Builder(@Nonnull OldVar var) {
|
||||
this.name = var.name;
|
||||
this.value = var.value;
|
||||
this.system = var.system;
|
||||
this.description = var.description;
|
||||
this.id = var.id;
|
||||
}
|
||||
|
||||
public Builder(@Nonnull IConstant iConstant) {
|
||||
this.name = iConstant.getName();
|
||||
|
||||
this.value = iConstant.getValue();
|
||||
|
||||
this.system = iConstant.isSystem();
|
||||
this.description = iConstant.getDescription();
|
||||
if (iConstant.isIdDefined()) {
|
||||
this.id = iConstant.getId();
|
||||
}
|
||||
}
|
||||
|
||||
public Builder(@Nonnull String name, @Nonnull Double value) {
|
||||
this(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public Builder(@Nonnull String name, @Nullable String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
public Builder setName(@Nonnull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder setValue(@Nullable String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setSystem(boolean system) {
|
||||
this.system = system;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder setDescription(@Nullable String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public OldVar create() {
|
||||
final OldVar result;
|
||||
if (id != null) {
|
||||
result = new OldVar(id);
|
||||
} else {
|
||||
result = new OldVar();
|
||||
}
|
||||
|
||||
result.name = name;
|
||||
result.value = value;
|
||||
result.system = system;
|
||||
result.description = description;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.variables;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
|
||||
@Root(name = "vars")
|
||||
public class OldVars {
|
||||
|
||||
public static final String PREFS_KEY = "org.solovyev.android.calculator.CalculatorModel_vars";
|
||||
|
||||
@ElementList(type = OldVar.class, name = "vars")
|
||||
public List<OldVar> list = new ArrayList<OldVar>();
|
||||
|
||||
public OldVars() {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static List<CppVariable> toCppVariables(@Nonnull OldVars oldVariables) {
|
||||
final List<CppVariable> variables = new ArrayList<>();
|
||||
for (OldVar oldVar : oldVariables.list) {
|
||||
final String name = oldVar.getName();
|
||||
if (TextUtils.isEmpty(name)) {
|
||||
continue;
|
||||
}
|
||||
variables.add(CppVariable.builder(name)
|
||||
.withValue(nullToEmpty(oldVar.getValue()))
|
||||
.withDescription(nullToEmpty(oldVar.getDescription())).build());
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.variables;
|
||||
|
||||
import android.support.annotation.StringRes;
|
||||
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.entities.Category;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
public enum VariablesCategory implements Category<IConstant> {
|
||||
|
||||
my(R.string.c_var_my) {
|
||||
@Override
|
||||
public boolean isInCategory(@Nonnull IConstant variable) {
|
||||
return !variable.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
system(R.string.c_var_system) {
|
||||
@Override
|
||||
public boolean isInCategory(@Nonnull IConstant variable) {
|
||||
return variable.isSystem();
|
||||
}
|
||||
};
|
||||
|
||||
@StringRes
|
||||
private final int title;
|
||||
|
||||
VariablesCategory(@StringRes int title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int title() {
|
||||
return title;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user