new store completed
This commit is contained in:
parent
57621cc6e8
commit
af3631d043
|
|
@ -1,23 +0,0 @@
|
||||||
package ru.touchin.roboswag.components.storables;
|
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import ru.touchin.roboswag.core.data.storable.SameTypesConverter;
|
|
||||||
import ru.touchin.roboswag.core.data.storable.Storable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Gavriil Sitnikov on 12/04/16.
|
|
||||||
* TODO: description
|
|
||||||
*/
|
|
||||||
public class BooleanPreferenceStorable extends Storable<String, Boolean, Boolean> {
|
|
||||||
|
|
||||||
public BooleanPreferenceStorable(@NonNull final String name,
|
|
||||||
@NonNull final SharedPreferences preferences,
|
|
||||||
@Nullable final Boolean defaultValue) {
|
|
||||||
super(name, Boolean.class, Boolean.class, new PreferenceStore<>(preferences), new SameTypesConverter<>(),
|
|
||||||
false, null, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
package ru.touchin.roboswag.components.storables;
|
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import ru.touchin.roboswag.core.data.exceptions.ConversionException;
|
|
||||||
import ru.touchin.roboswag.core.data.storable.Converter;
|
|
||||||
import ru.touchin.roboswag.core.data.storable.Storable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Gavriil Sitnikov on 12/04/16.
|
|
||||||
* TODO: description
|
|
||||||
*/
|
|
||||||
public class EnumPreferenceStorable<T extends Enum<T>> extends Storable<String, T, String> {
|
|
||||||
|
|
||||||
public EnumPreferenceStorable(@NonNull final String name,
|
|
||||||
@NonNull final Class<T> enumClass,
|
|
||||||
@NonNull final SharedPreferences preferences,
|
|
||||||
@Nullable final T defaultValue) {
|
|
||||||
super(name, enumClass, String.class, new PreferenceStore<>(preferences), new EnumToStringConverter<>(),
|
|
||||||
false, null, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class EnumToStringConverter<T extends Enum<T>> implements Converter<T, String> {
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public String toStoreObject(@NonNull final Class<T> objectClass, @NonNull final Class<String> stringClass,
|
|
||||||
@Nullable final T object) throws ConversionException {
|
|
||||||
return object != null ? object.name() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public T toObject(@NonNull final Class<T> objectClass, @NonNull final Class<String> stringClass,
|
|
||||||
@Nullable final String stringObject) throws ConversionException {
|
|
||||||
return stringObject != null ? Enum.valueOf(objectClass, stringObject) : null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,105 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov)
|
|
||||||
*
|
|
||||||
* This file is part of RoboSwag library.
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package ru.touchin.roboswag.components.storables;
|
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import com.google.api.client.json.JsonFactory;
|
|
||||||
import com.google.api.client.json.JsonGenerator;
|
|
||||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
|
||||||
import com.google.api.client.util.Charsets;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
|
|
||||||
import ru.touchin.roboswag.core.data.exceptions.ConversionException;
|
|
||||||
import ru.touchin.roboswag.core.data.storable.Converter;
|
|
||||||
import ru.touchin.roboswag.core.data.storable.Storable;
|
|
||||||
import ru.touchin.roboswag.core.log.Lc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Gavriil Sitnikov on 18/03/16.
|
|
||||||
* TODO: description
|
|
||||||
*/
|
|
||||||
public class JsonPreferenceStorable<T> extends Storable<String, T, String> {
|
|
||||||
|
|
||||||
private static final JsonFactory DEFAULT_JSON_FACTORY = new JacksonFactory();
|
|
||||||
|
|
||||||
public JsonPreferenceStorable(@NonNull final String name,
|
|
||||||
@NonNull final Class<T> objectClass,
|
|
||||||
@NonNull final SharedPreferences preferences,
|
|
||||||
@Nullable final T defaultValue) {
|
|
||||||
super(name, objectClass, String.class, new PreferenceStore<>(preferences), new JsonConverter<>(),
|
|
||||||
false, null, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class JsonConverter<T> implements Converter<T, String> {
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public String toStoreObject(@NonNull final Class<T> objectClass,
|
|
||||||
@NonNull final Class<String> stringClass,
|
|
||||||
@Nullable final T object)
|
|
||||||
throws ConversionException {
|
|
||||||
if (object == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final StringWriter stringWriter = new StringWriter();
|
|
||||||
JsonGenerator generator = null;
|
|
||||||
try {
|
|
||||||
generator = DEFAULT_JSON_FACTORY.createJsonGenerator(stringWriter);
|
|
||||||
generator.serialize(object);
|
|
||||||
generator.flush();
|
|
||||||
return stringWriter.toString();
|
|
||||||
} catch (final IOException exception) {
|
|
||||||
throw new ConversionException("Object generation error", exception);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (generator != null) {
|
|
||||||
generator.close();
|
|
||||||
}
|
|
||||||
} catch (final IOException exception) {
|
|
||||||
Lc.assertion(exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public T toObject(@NonNull final Class<T> objectClass, @NonNull final Class<String> stringClass, @Nullable final String source)
|
|
||||||
throws ConversionException {
|
|
||||||
if (source == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(source.getBytes(Charsets.UTF_8));
|
|
||||||
try {
|
|
||||||
return DEFAULT_JSON_FACTORY.createJsonObjectParser().parseAndClose(byteArrayInputStream, Charsets.UTF_8, objectClass);
|
|
||||||
} catch (final Exception exception) {
|
|
||||||
throw new ConversionException("Parsing error", exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,241 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov)
|
||||||
|
*
|
||||||
|
* This file is part of RoboSwag library.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package ru.touchin.roboswag.components.storables;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.api.client.json.JsonFactory;
|
||||||
|
import com.google.api.client.json.JsonGenerator;
|
||||||
|
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||||
|
import com.google.api.client.util.Charsets;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
import ru.touchin.roboswag.core.data.storable.SafeConverter;
|
||||||
|
import ru.touchin.roboswag.core.data.storable.SameTypesConverter;
|
||||||
|
import ru.touchin.roboswag.core.data.storable.Storable;
|
||||||
|
import ru.touchin.roboswag.core.data.storable.concrete.NonNullSafeStorable;
|
||||||
|
import ru.touchin.roboswag.core.data.storable.concrete.SafeStorable;
|
||||||
|
import ru.touchin.roboswag.core.log.Lc;
|
||||||
|
import ru.touchin.roboswag.core.utils.ShouldNotHappenException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Gavriil Sitnikov on 03/05/2016.
|
||||||
|
* TODO: fill description
|
||||||
|
*/
|
||||||
|
public final class PreferenceStorables {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static SafeStorable<String, String, String> stringStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) {
|
||||||
|
return new Storable.Builder<String, String, String>(name, String.class, false)
|
||||||
|
.setSafeStore(String.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static NonNullSafeStorable<String, String, String> stringStorable(@NonNull final String name,
|
||||||
|
@NonNull final SharedPreferences preferences,
|
||||||
|
@NonNull final String defaultValue) {
|
||||||
|
return new Storable.Builder<String, String, String>(name, String.class, false)
|
||||||
|
.setSafeStore(String.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.setDefaultValue(defaultValue)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static SafeStorable<String, Long, Long> longStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) {
|
||||||
|
return new Storable.Builder<String, Long, Long>(name, Long.class, false)
|
||||||
|
.setSafeStore(Long.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static NonNullSafeStorable<String, Long, Long> longStorable(@NonNull final String name,
|
||||||
|
@NonNull final SharedPreferences preferences,
|
||||||
|
final long defaultValue) {
|
||||||
|
return new Storable.Builder<String, Long, Long>(name, Long.class, false)
|
||||||
|
.setSafeStore(Long.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.setDefaultValue(defaultValue)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static SafeStorable<String, Boolean, Boolean> booleanStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) {
|
||||||
|
return new Storable.Builder<String, Boolean, Boolean>(name, Boolean.class, false)
|
||||||
|
.setSafeStore(Boolean.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static NonNullSafeStorable<String, Boolean, Boolean> booleanStorable(@NonNull final String name,
|
||||||
|
@NonNull final SharedPreferences preferences,
|
||||||
|
final boolean defaultValue) {
|
||||||
|
return new Storable.Builder<String, Boolean, Boolean>(name, Boolean.class, false)
|
||||||
|
.setSafeStore(Boolean.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.setDefaultValue(defaultValue)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static SafeStorable<String, Integer, Integer> integerStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) {
|
||||||
|
return new Storable.Builder<String, Integer, Integer>(name, Integer.class, false)
|
||||||
|
.setSafeStore(Integer.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static NonNullSafeStorable<String, Integer, Integer> integerStorable(@NonNull final String name,
|
||||||
|
@NonNull final SharedPreferences preferences,
|
||||||
|
final int defaultValue) {
|
||||||
|
return new Storable.Builder<String, Integer, Integer>(name, Integer.class, false)
|
||||||
|
.setSafeStore(Integer.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.setDefaultValue(defaultValue)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static SafeStorable<String, Float, Float> floatStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) {
|
||||||
|
return new Storable.Builder<String, Float, Float>(name, Float.class, false)
|
||||||
|
.setSafeStore(Float.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static NonNullSafeStorable<String, Float, Float> floatStorable(@NonNull final String name,
|
||||||
|
@NonNull final SharedPreferences preferences,
|
||||||
|
final float defaultValue) {
|
||||||
|
return new Storable.Builder<String, Float, Float>(name, Float.class, false)
|
||||||
|
.setSafeStore(Float.class, new PreferenceStore<>(preferences), new SameTypesConverter<>())
|
||||||
|
.setDefaultValue(defaultValue)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static <T extends Enum<T>> SafeStorable<String, T, String> enumStorable(@NonNull final String name,
|
||||||
|
@NonNull final Class<T> enumClass,
|
||||||
|
@NonNull final SharedPreferences preferences) {
|
||||||
|
return new Storable.Builder<String, T, String>(name, enumClass, false)
|
||||||
|
.setSafeStore(String.class, new PreferenceStore<>(preferences), new EnumToStringConverter<>())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static <T extends Enum<T>> NonNullSafeStorable<String, T, String> enumStorable(@NonNull final String name,
|
||||||
|
@NonNull final Class<T> enumClass,
|
||||||
|
@NonNull final SharedPreferences preferences,
|
||||||
|
final T defaultValue) {
|
||||||
|
return new Storable.Builder<String, T, String>(name, enumClass, false)
|
||||||
|
.setSafeStore(String.class, new PreferenceStore<>(preferences), new EnumToStringConverter<>())
|
||||||
|
.setDefaultValue(defaultValue)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static <T> SafeStorable<String, T, String> jsonStorable(@NonNull final String name,
|
||||||
|
@NonNull final Class<T> jsonClass,
|
||||||
|
@NonNull final SharedPreferences preferences) {
|
||||||
|
return new Storable.Builder<String, T, String>(name, jsonClass, false)
|
||||||
|
.setSafeStore(String.class, new PreferenceStore<>(preferences), new JsonConverter<>())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static <T> NonNullSafeStorable<String, T, String> jsonStorable(@NonNull final String name,
|
||||||
|
@NonNull final Class<T> jsonClass,
|
||||||
|
@NonNull final SharedPreferences preferences,
|
||||||
|
final T defaultValue) {
|
||||||
|
return new Storable.Builder<String, T, String>(name, jsonClass, false)
|
||||||
|
.setSafeStore(String.class, new PreferenceStore<>(preferences), new JsonConverter<>())
|
||||||
|
.setDefaultValue(defaultValue)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class EnumToStringConverter<T extends Enum<T>> implements SafeConverter<T, String> {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public String toStoreObject(@NonNull final Class<T> objectClass, @NonNull final Class<String> stringClass, @Nullable final T object) {
|
||||||
|
return object != null ? object.name() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public T toObject(@NonNull final Class<T> objectClass, @NonNull final Class<String> stringClass, @Nullable final String stringObject) {
|
||||||
|
return stringObject != null ? Enum.valueOf(objectClass, stringObject) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class JsonConverter<T> implements SafeConverter<T, String> {
|
||||||
|
|
||||||
|
private static final JsonFactory DEFAULT_JSON_FACTORY = new JacksonFactory();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public String toStoreObject(@NonNull final Class<T> objectClass,
|
||||||
|
@NonNull final Class<String> stringClass,
|
||||||
|
@Nullable final T object) {
|
||||||
|
if (object == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final StringWriter stringWriter = new StringWriter();
|
||||||
|
JsonGenerator generator = null;
|
||||||
|
try {
|
||||||
|
generator = DEFAULT_JSON_FACTORY.createJsonGenerator(stringWriter);
|
||||||
|
generator.serialize(object);
|
||||||
|
generator.flush();
|
||||||
|
return stringWriter.toString();
|
||||||
|
} catch (final IOException exception) {
|
||||||
|
throw new ShouldNotHappenException(exception);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (generator != null) {
|
||||||
|
generator.close();
|
||||||
|
}
|
||||||
|
} catch (final IOException exception) {
|
||||||
|
Lc.assertion(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public T toObject(@NonNull final Class<T> objectClass, @NonNull final Class<String> stringClass, @Nullable final String source) {
|
||||||
|
if (source == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(source.getBytes(Charsets.UTF_8));
|
||||||
|
try {
|
||||||
|
return DEFAULT_JSON_FACTORY.createJsonObjectParser().parseAndClose(byteArrayInputStream, Charsets.UTF_8, objectClass);
|
||||||
|
} catch (final Exception exception) {
|
||||||
|
throw new ShouldNotHappenException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private PreferenceStorables() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -23,14 +23,14 @@ import android.content.SharedPreferences;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import ru.touchin.roboswag.core.data.storable.Store;
|
import ru.touchin.roboswag.core.data.storable.SafeStore;
|
||||||
import ru.touchin.roboswag.core.data.exceptions.StoreException;
|
import ru.touchin.roboswag.core.log.Lc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Gavriil Sitnikov on 18/03/16.
|
* Created by Gavriil Sitnikov on 18/03/16.
|
||||||
* TODO: description
|
* TODO: description
|
||||||
*/
|
*/
|
||||||
public class PreferenceStore<T> implements Store<String, T> {
|
public class PreferenceStore<T> implements SafeStore<String, T> {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final SharedPreferences preferences;
|
private final SharedPreferences preferences;
|
||||||
|
|
@ -45,8 +45,7 @@ public class PreferenceStore<T> implements Store<String, T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void storeObject(@NonNull final Class<T> storeObjectClass, @NonNull final String key, @Nullable final T storeObject)
|
public void storeObject(@NonNull final Class<T> storeObjectClass, @NonNull final String key, @Nullable final T storeObject) {
|
||||||
throws StoreException {
|
|
||||||
if (storeObject == null) {
|
if (storeObject == null) {
|
||||||
preferences.edit().remove(key).apply();
|
preferences.edit().remove(key).apply();
|
||||||
return;
|
return;
|
||||||
|
|
@ -63,14 +62,14 @@ public class PreferenceStore<T> implements Store<String, T> {
|
||||||
} else if (storeObjectClass.equals(Float.class)) {
|
} else if (storeObjectClass.equals(Float.class)) {
|
||||||
preferences.edit().putFloat(key, (Float) storeObject).apply();
|
preferences.edit().putFloat(key, (Float) storeObject).apply();
|
||||||
} else {
|
} else {
|
||||||
throw new StoreException("Unsupported type of object " + storeObjectClass);
|
Lc.assertion("Unsupported type of object " + storeObjectClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T loadObject(@NonNull final Class<T> storeObjectClass, @NonNull final String key) throws StoreException {
|
public T loadObject(@NonNull final Class<T> storeObjectClass, @NonNull final String key) {
|
||||||
if (!contains(key)) {
|
if (!contains(key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +85,8 @@ public class PreferenceStore<T> implements Store<String, T> {
|
||||||
} else if (storeObjectClass.equals(Float.class)) {
|
} else if (storeObjectClass.equals(Float.class)) {
|
||||||
return (T) ((Float) preferences.getFloat(key, 0f));
|
return (T) ((Float) preferences.getFloat(key, 0f));
|
||||||
}
|
}
|
||||||
throw new StoreException("Unsupported type of object " + storeObjectClass);
|
Lc.assertion("Unsupported type of object " + storeObjectClass);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
package ru.touchin.roboswag.components.storables;
|
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import ru.touchin.roboswag.core.data.storable.SameTypesConverter;
|
|
||||||
import ru.touchin.roboswag.core.data.storable.Storable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Gavriil Sitnikov on 12/04/16.
|
|
||||||
* TODO: description
|
|
||||||
*/
|
|
||||||
public class StringPreferenceStorable extends Storable<String, String, String> {
|
|
||||||
|
|
||||||
public StringPreferenceStorable(@NonNull final String name,
|
|
||||||
@NonNull final SharedPreferences preferences,
|
|
||||||
@Nullable final String defaultValue) {
|
|
||||||
super(name, String.class, String.class, new PreferenceStore<>(preferences), new SameTypesConverter<>(),
|
|
||||||
false, null, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue