Remove deps
This commit is contained in:
		@@ -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')
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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<C extends Collection<T>, T> extends AbstractPreference<C> {
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private final Mapper<C> mapper;
 | 
			
		||||
 | 
			
		||||
    private CollectionToStringPreference(@Nonnull String key, @Nullable C defaultValue, @Nonnull Mapper<C> mapper) {
 | 
			
		||||
        super(key, defaultValue);
 | 
			
		||||
        this.mapper = mapper;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <T> CollectionToStringPreference<List<T>, T> forList(@Nonnull String key, @Nullable List<T> defaultValue, @Nonnull Mapper<List<T>> mapper) {
 | 
			
		||||
        return new CollectionToStringPreference<List<T>, T>(key, defaultValue, mapper);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <T> CollectionToStringPreference<List<T>, T> forTypedList(@Nonnull String key, @Nullable List<T> defaultValue, @Nonnull Mapper<T> mapper) {
 | 
			
		||||
        return new CollectionToStringPreference<List<T>, T>(key, defaultValue, ListMapper.newInstance(mapper));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static CollectionToStringPreference<List<String>, String> forStringList(@Nonnull String key, @Nullable List<String> defaultValue) {
 | 
			
		||||
        return new CollectionToStringPreference<List<String>, 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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<N extends Number & Comparable<N>> extends AbstractPreference<Interval<N>> {
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private final Mapper<Interval<N>> mapper;
 | 
			
		||||
 | 
			
		||||
    private NumberIntervalPreference(@Nonnull String key, @Nullable Interval<N> defaultValue, @Nonnull Class<N> clazz) {
 | 
			
		||||
        super(key, defaultValue);
 | 
			
		||||
        this.mapper = NumberIntervalMapper.of(clazz);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <N extends Number & Comparable<N>> NumberIntervalPreference<N> of(@Nonnull String key, @Nullable Interval<N> defaultValue, @Nonnull Class<N> clazz) {
 | 
			
		||||
        return new NumberIntervalPreference<N>(key, defaultValue, clazz);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected Interval<N> 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<N> value) {
 | 
			
		||||
        editor.putString(getKey(), mapper.formatValue(value));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										62
									
								
								app/src/main/java/org/solovyev/common/text/EnumMapper.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								app/src/main/java/org/solovyev/common/text/EnumMapper.java
									
									
									
									
									
										Normal file
									
								
							@@ -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<T extends Enum> implements Mapper<T> {
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private final static Map<Class<? extends Enum>, Mapper<?>> cachedMappers = new HashMap<Class<? extends Enum>, Mapper<?>>();
 | 
			
		||||
 | 
			
		||||
    private final Class<T> enumClass;
 | 
			
		||||
 | 
			
		||||
    private EnumMapper(@Nonnull Class<T> enumClass) {
 | 
			
		||||
        this.enumClass = enumClass;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <T extends Enum> Mapper<T> of(@Nonnull Class<T> enumClass) {
 | 
			
		||||
        Mapper<T> result = (Mapper<T>) cachedMappers.get(enumClass);
 | 
			
		||||
        if (result == null) {
 | 
			
		||||
            // do not care about synchronization
 | 
			
		||||
            result = new EnumMapper<T>(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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								app/src/main/java/org/solovyev/common/text/Formatter.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								app/src/main/java/org/solovyev/common/text/Formatter.java
									
									
									
									
									
										Normal file
									
								
							@@ -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<T> {
 | 
			
		||||
    /**
 | 
			
		||||
     * 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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								app/src/main/java/org/solovyev/common/text/Mapper.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								app/src/main/java/org/solovyev/common/text/Mapper.java
									
									
									
									
									
										Normal file
									
								
							@@ -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 <T>
 | 
			
		||||
 * @see Formatter
 | 
			
		||||
 * @see Parser
 | 
			
		||||
 */
 | 
			
		||||
public interface Mapper<T> extends Formatter<T>, Parser<T> {
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										90
									
								
								app/src/main/java/org/solovyev/common/text/NumberMapper.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								app/src/main/java/org/solovyev/common/text/NumberMapper.java
									
									
									
									
									
										Normal file
									
								
							@@ -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<N extends Number> implements Mapper<N> {
 | 
			
		||||
 | 
			
		||||
    private static final List<Class<? extends Number>> supportedClasses = NumberParser.supportedClasses;
 | 
			
		||||
 | 
			
		||||
    private static final Map<Class<?>, Mapper<?>> mappers = new HashMap<>(supportedClasses.size());
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        for (Class<? extends Number> supportedClass : supportedClasses) {
 | 
			
		||||
            mappers.put(supportedClass, newInstance(supportedClass));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private final Formatter<N> formatter;
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private final Parser<? extends N> parser;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Use org.solovyev.common.text.NumberMapper#getMapper(java.lang.Class<N>) instead
 | 
			
		||||
     *
 | 
			
		||||
     * @param clazz class representing parsed object
 | 
			
		||||
     */
 | 
			
		||||
    private NumberMapper(@Nonnull Class<? extends N> clazz) {
 | 
			
		||||
        this(NumberParser.of(clazz), ValueOfFormatter.<N>getNotNullFormatter());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private NumberMapper(@Nonnull Parser<? extends N> parser,
 | 
			
		||||
                         @Nonnull Formatter<N> formatter) {
 | 
			
		||||
        this.parser = parser;
 | 
			
		||||
        this.formatter = formatter;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <N extends Number> Mapper<N> newInstance(@Nonnull Parser<? extends N> parser,
 | 
			
		||||
                                                           @Nonnull Formatter<N> formatter) {
 | 
			
		||||
        return new NumberMapper<>(parser, formatter);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private static <N extends Number> Mapper<N> newInstance(@Nonnull Class<? extends N> clazz) {
 | 
			
		||||
        return new NumberMapper<>(clazz);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <N extends Number> Mapper<N> of(@Nonnull Class<? extends N> clazz) {
 | 
			
		||||
        assert supportedClasses.contains(clazz) : "Class " + clazz + " is not supported by " + NumberMapper.class;
 | 
			
		||||
        return (Mapper<N>) 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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								app/src/main/java/org/solovyev/common/text/NumberParser.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								app/src/main/java/org/solovyev/common/text/NumberParser.java
									
									
									
									
									
										Normal file
									
								
							@@ -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<N extends Number> implements Parser<N> {
 | 
			
		||||
 | 
			
		||||
    static final List<Class<? extends Number>> supportedClasses = Arrays.<Class<? extends Number>>asList(Integer.class, Float.class, Long.class, Double.class);
 | 
			
		||||
 | 
			
		||||
    private static final Map<Class<?>, Parser<?>> parsers = new HashMap<>(supportedClasses.size());
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        for (Class<? extends Number> supportedClass : supportedClasses) {
 | 
			
		||||
            parsers.put(supportedClass, new NumberParser<>(supportedClass));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private final Class<? extends N> clazz;
 | 
			
		||||
 | 
			
		||||
    private NumberParser(@Nonnull Class<? extends N> clazz) {
 | 
			
		||||
        this.clazz = clazz;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <N extends Number> Parser<N> of(@Nonnull Class<N> clazz) {
 | 
			
		||||
        assert supportedClasses.contains(clazz) : "Class " + clazz + " is not supported by " + NumberParser.class;
 | 
			
		||||
        return (Parser<N>) 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								app/src/main/java/org/solovyev/common/text/Parser.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								app/src/main/java/org/solovyev/common/text/Parser.java
									
									
									
									
									
										Normal file
									
								
							@@ -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<T> {
 | 
			
		||||
    /**
 | 
			
		||||
     * 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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								app/src/main/java/org/solovyev/common/text/StringMapper.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								app/src/main/java/org/solovyev/common/text/StringMapper.java
									
									
									
									
									
										Normal file
									
								
							@@ -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<String> {
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private static final Mapper<String> instance = new StringMapper();
 | 
			
		||||
 | 
			
		||||
    private StringMapper() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static Mapper<String> getInstance() {
 | 
			
		||||
        return instance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String formatValue(@Nullable String value) {
 | 
			
		||||
        return value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String parseValue(@Nullable String value) {
 | 
			
		||||
        return value;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<T> implements Formatter<T> {
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private static final ValueOfFormatter<Object> notNullFormatter = new ValueOfFormatter<Object>(false);
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    private static final ValueOfFormatter<Object> nullableFormatter = new ValueOfFormatter<Object>(true);
 | 
			
		||||
    private final boolean processNulls;
 | 
			
		||||
 | 
			
		||||
    private ValueOfFormatter() {
 | 
			
		||||
        this(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ValueOfFormatter(boolean processNulls) {
 | 
			
		||||
        this.processNulls = processNulls;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <T> ValueOfFormatter<T> getNotNullFormatter() {
 | 
			
		||||
        return (ValueOfFormatter<T>) notNullFormatter;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public static <T> ValueOfFormatter<T> getNullableFormatter() {
 | 
			
		||||
        return (ValueOfFormatter<T>) 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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user