diff --git a/app/build.gradle b/app/build.gradle index 06cfb995..9a1bbe4e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -82,8 +82,6 @@ dependencies { compile 'com.google.android.gms:play-services-base:8.4.0' compile 'com.google.android.gms:play-services-analytics:8.4.0' compile(name: 'plotter', ext: 'aar') - compile 'org.solovyev:common-text:1.0.7' - compile 'org.solovyev:common-listeners:1.0.7' compile('com.google.guava:guava:11.0.2') { exclude(module: 'jsr305') } diff --git a/app/src/main/java/org/solovyev/android/prefs/CollectionToStringPreference.java b/app/src/main/java/org/solovyev/android/prefs/CollectionToStringPreference.java deleted file mode 100644 index 0088d12a..00000000 --- a/app/src/main/java/org/solovyev/android/prefs/CollectionToStringPreference.java +++ /dev/null @@ -1,69 +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.SharedPreferences; -import org.solovyev.common.text.ListMapper; -import org.solovyev.common.text.Mapper; -import org.solovyev.common.text.StringMapper; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; -import java.util.List; - -public class CollectionToStringPreference, T> extends AbstractPreference { - - @Nonnull - private final Mapper mapper; - - private CollectionToStringPreference(@Nonnull String key, @Nullable C defaultValue, @Nonnull Mapper mapper) { - super(key, defaultValue); - this.mapper = mapper; - } - - @Nonnull - public static CollectionToStringPreference, T> forList(@Nonnull String key, @Nullable List defaultValue, @Nonnull Mapper> mapper) { - return new CollectionToStringPreference, T>(key, defaultValue, mapper); - } - - @Nonnull - public static CollectionToStringPreference, T> forTypedList(@Nonnull String key, @Nullable List defaultValue, @Nonnull Mapper mapper) { - return new CollectionToStringPreference, T>(key, defaultValue, ListMapper.newInstance(mapper)); - } - - @Nonnull - public static CollectionToStringPreference, String> forStringList(@Nonnull String key, @Nullable List defaultValue) { - return new CollectionToStringPreference, String>(key, defaultValue, ListMapper.newInstance(StringMapper.getInstance())); - } - - @Override - protected C getPersistedValue(@Nonnull SharedPreferences preferences) { - return mapper.parseValue(preferences.getString(getKey(), null)); - } - - @Override - protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull C values) { - editor.putString(getKey(), mapper.formatValue(values)); - } -} diff --git a/app/src/main/java/org/solovyev/android/prefs/NumberIntervalPreference.java b/app/src/main/java/org/solovyev/android/prefs/NumberIntervalPreference.java deleted file mode 100644 index 0dabe873..00000000 --- a/app/src/main/java/org/solovyev/android/prefs/NumberIntervalPreference.java +++ /dev/null @@ -1,64 +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.SharedPreferences; -import org.solovyev.common.interval.Interval; -import org.solovyev.common.text.Mapper; -import org.solovyev.common.text.NumberIntervalMapper; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -public final class NumberIntervalPreference> extends AbstractPreference> { - - @Nonnull - private final Mapper> mapper; - - private NumberIntervalPreference(@Nonnull String key, @Nullable Interval defaultValue, @Nonnull Class clazz) { - super(key, defaultValue); - this.mapper = NumberIntervalMapper.of(clazz); - - } - - @Nonnull - public static > NumberIntervalPreference of(@Nonnull String key, @Nullable Interval defaultValue, @Nonnull Class clazz) { - return new NumberIntervalPreference(key, defaultValue, clazz); - } - - @Override - protected Interval getPersistedValue(@Nonnull SharedPreferences preferences) { - final String result = preferences.getString(getKey(), null); - if (result == null) { - return null; - } else { - return mapper.parseValue(result); - } - } - - @Override - protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull Interval value) { - editor.putString(getKey(), mapper.formatValue(value)); - } - -} diff --git a/app/src/main/java/org/solovyev/common/text/EnumMapper.java b/app/src/main/java/org/solovyev/common/text/EnumMapper.java new file mode 100644 index 00000000..594aa101 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/EnumMapper.java @@ -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.common.text; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.Map; + +public class EnumMapper implements Mapper { + + @Nonnull + private final static Map, Mapper> cachedMappers = new HashMap, Mapper>(); + + private final Class enumClass; + + private EnumMapper(@Nonnull Class enumClass) { + this.enumClass = enumClass; + } + + @Nonnull + public static Mapper of(@Nonnull Class enumClass) { + Mapper result = (Mapper) cachedMappers.get(enumClass); + if (result == null) { + // do not care about synchronization + result = new EnumMapper(enumClass); + cachedMappers.put(enumClass, result); + } + + return result; + } + + @Override + public String formatValue(@Nullable T value) throws IllegalArgumentException { + return value == null ? null : value.name(); + } + + @Override + public T parseValue(@Nullable String value) throws IllegalArgumentException { + return value == null ? null : (T) Enum.valueOf(enumClass, value); + } +} diff --git a/app/src/main/java/org/solovyev/common/text/Formatter.java b/app/src/main/java/org/solovyev/common/text/Formatter.java new file mode 100644 index 00000000..d83f6c39 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/Formatter.java @@ -0,0 +1,37 @@ +/* + * 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.common.text; + +import javax.annotation.Nullable; + +public interface Formatter { + /** + * Method formats string value of specified object + * + * @param value object to be converted to string + * @return string representation of current object + * @throws IllegalArgumentException illegal argument exception in case of any error (AND ONLY ONE EXCEPTION, I.E. NO NUMBER FORMAT EXCEPTIONS AND SO ON) + */ + @Nullable + String formatValue(@Nullable T value) throws IllegalArgumentException; +} diff --git a/app/src/main/java/org/solovyev/common/text/Mapper.java b/app/src/main/java/org/solovyev/common/text/Mapper.java new file mode 100644 index 00000000..aa69b6c0 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/Mapper.java @@ -0,0 +1,33 @@ +/* + * 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.common.text; + +/** + * Class represents an interface for mapping string value to the typed object. + * + * @param + * @see Formatter + * @see Parser + */ +public interface Mapper extends Formatter, Parser { +} diff --git a/app/src/main/java/org/solovyev/common/text/NumberMapper.java b/app/src/main/java/org/solovyev/common/text/NumberMapper.java new file mode 100644 index 00000000..b82f5c29 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/NumberMapper.java @@ -0,0 +1,90 @@ +/* + * 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.common.text; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class NumberMapper implements Mapper { + + private static final List> supportedClasses = NumberParser.supportedClasses; + + private static final Map, Mapper> mappers = new HashMap<>(supportedClasses.size()); + + static { + for (Class supportedClass : supportedClasses) { + mappers.put(supportedClass, newInstance(supportedClass)); + } + } + + @Nonnull + private final Formatter formatter; + + @Nonnull + private final Parser parser; + + /** + * Use org.solovyev.common.text.NumberMapper#getMapper(java.lang.Class) instead + * + * @param clazz class representing parsed object + */ + private NumberMapper(@Nonnull Class clazz) { + this(NumberParser.of(clazz), ValueOfFormatter.getNotNullFormatter()); + } + + private NumberMapper(@Nonnull Parser parser, + @Nonnull Formatter formatter) { + this.parser = parser; + this.formatter = formatter; + } + + @Nonnull + public static Mapper newInstance(@Nonnull Parser parser, + @Nonnull Formatter formatter) { + return new NumberMapper<>(parser, formatter); + } + + @Nonnull + private static Mapper newInstance(@Nonnull Class clazz) { + return new NumberMapper<>(clazz); + } + + @Nonnull + public static Mapper of(@Nonnull Class clazz) { + assert supportedClasses.contains(clazz) : "Class " + clazz + " is not supported by " + NumberMapper.class; + return (Mapper) mappers.get(clazz); + } + + @Override + public String formatValue(@Nullable N value) throws IllegalArgumentException { + return formatter.formatValue(value); + } + + @Override + public N parseValue(@Nullable String value) throws IllegalArgumentException { + return this.parser.parseValue(value); + } +} diff --git a/app/src/main/java/org/solovyev/common/text/NumberParser.java b/app/src/main/java/org/solovyev/common/text/NumberParser.java new file mode 100644 index 00000000..232cbbc9 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/NumberParser.java @@ -0,0 +1,79 @@ +/* + * 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.common.text; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class NumberParser implements Parser { + + static final List> supportedClasses = Arrays.>asList(Integer.class, Float.class, Long.class, Double.class); + + private static final Map, Parser> parsers = new HashMap<>(supportedClasses.size()); + + static { + for (Class supportedClass : supportedClasses) { + parsers.put(supportedClass, new NumberParser<>(supportedClass)); + } + } + + @Nonnull + private final Class clazz; + + private NumberParser(@Nonnull Class clazz) { + this.clazz = clazz; + } + + @Nonnull + public static Parser of(@Nonnull Class clazz) { + assert supportedClasses.contains(clazz) : "Class " + clazz + " is not supported by " + NumberParser.class; + return (Parser) parsers.get(clazz); + } + + @Override + public N parseValue(@Nullable String value) throws IllegalArgumentException { + N result; + + if (value != null) { + if (this.clazz.equals(Integer.class)) { + result = (N) Integer.valueOf(value); + } else if (this.clazz.equals(Float.class)) { + result = (N) Float.valueOf(value); + } else if (this.clazz.equals(Long.class)) { + result = (N) Long.valueOf(value); + } else if (this.clazz.equals(Double.class)) { + result = (N) Double.valueOf(value); + } else { + throw new UnsupportedOperationException(this.clazz + " is not supported!"); + } + } else { + result = null; + } + + return result; + } +} diff --git a/app/src/main/java/org/solovyev/common/text/Parser.java b/app/src/main/java/org/solovyev/common/text/Parser.java new file mode 100644 index 00000000..ce0b36c8 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/Parser.java @@ -0,0 +1,37 @@ +/* + * 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.common.text; + +import javax.annotation.Nullable; + +public interface Parser { + /** + * Method parses specified value and returns converted object + * + * @param value string to be parsed + * @return parsed object + * @throws IllegalArgumentException illegal argument exception in case of any error (AND ONLY ONE EXCEPTION, I.E. NO NUMBER FORMAT EXCEPTIONS AND SO ON) + */ + @Nullable + T parseValue(@Nullable String value) throws IllegalArgumentException; +} diff --git a/app/src/main/java/org/solovyev/common/text/StringMapper.java b/app/src/main/java/org/solovyev/common/text/StringMapper.java new file mode 100644 index 00000000..a81b0921 --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/StringMapper.java @@ -0,0 +1,50 @@ +/* + * 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.common.text; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class StringMapper implements Mapper { + + @Nonnull + private static final Mapper instance = new StringMapper(); + + private StringMapper() { + } + + @Nonnull + public static Mapper getInstance() { + return instance; + } + + @Override + public String formatValue(@Nullable String value) { + return value; + } + + @Override + public String parseValue(@Nullable String value) { + return value; + } +} diff --git a/app/src/main/java/org/solovyev/common/text/ValueOfFormatter.java b/app/src/main/java/org/solovyev/common/text/ValueOfFormatter.java new file mode 100644 index 00000000..dc965c9c --- /dev/null +++ b/app/src/main/java/org/solovyev/common/text/ValueOfFormatter.java @@ -0,0 +1,68 @@ +/* + * 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.common.text; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class ValueOfFormatter implements Formatter { + + @Nonnull + private static final ValueOfFormatter notNullFormatter = new ValueOfFormatter(false); + + @Nonnull + private static final ValueOfFormatter nullableFormatter = new ValueOfFormatter(true); + private final boolean processNulls; + + private ValueOfFormatter() { + this(false); + } + + private ValueOfFormatter(boolean processNulls) { + this.processNulls = processNulls; + } + + @Nonnull + public static ValueOfFormatter getNotNullFormatter() { + return (ValueOfFormatter) notNullFormatter; + } + + @Nonnull + public static ValueOfFormatter getNullableFormatter() { + return (ValueOfFormatter) nullableFormatter; + } + + @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = {"NP_LOAD_OF_KNOWN_NULL_VALUE"}, justification = "If 'processNulls' is true => must result 'null'") + @Override + public String formatValue(@Nullable T t) throws IllegalArgumentException { + if (t == null) { + if (processNulls) { + return String.valueOf(t); + } else { + return null; + } + } else { + return String.valueOf(t); + } + } +}