
com.malinskiy.sheldon.adapter.impl.EnumAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Reactive preferences abstraction
package com.malinskiy.sheldon.adapter.impl;
import com.malinskiy.sheldon.IGateway;
import com.malinskiy.sheldon.adapter.IPreferenceAdapter;
import javax.annotation.Nonnull;
import rx.Observable;
import rx.functions.Func1;
public class EnumAdapter> implements IPreferenceAdapter {
@Nonnull private final Class clazz;
public EnumAdapter(@Nonnull Class clazz) {
this.clazz = clazz;
}
@Nonnull @Override
public Observable observe(@Nonnull String key, @Nonnull T defaultValue, @Nonnull IGateway gateway) {
return gateway.observeString(key, toString(defaultValue))
.map(new Func1() {
@Override public T call(String s) {
return fromString(s);
}
});
}
@Override public void put(@Nonnull String key, @Nonnull T value, IGateway gateway) {
gateway.putString(key, toString(value));
}
@Nonnull private T fromString(@Nonnull String string) {
return Enum.valueOf(clazz, string);
}
@Nonnull private String toString(@Nonnull T value) {
return value != null ? value.name() : "";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy