Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ buildscript {
}
}

repositories {
maven { url = "https://oss.sonatype.org/content/repositories/snapshots" }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

Expand Down Expand Up @@ -45,6 +49,6 @@ dependencies {
apt 'com.google.dagger:dagger-compiler:2.0'
provided 'javax.annotation:jsr250-api:1.0'

testCompile "junit:junit:4.12"
testCompile "org.mockito:mockito-core:1.9.5"
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package com.anbroidsdev.converter;

import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;

public class GsonObjectSharedPreferences implements ObjectSharedPreferences {

private final SharedPreferences delegate;
private final Editor editor;
private final Gson gson;

public GsonObjectSharedPreferences(SharedPreferences delegate, Gson gson) {
this.delegate = delegate;
this.editor = new Editor(delegate, gson);
this.gson = gson;
}

public static class Editor implements ObjectSharedPreferences.ObjectEditor {

private final SharedPreferences delegate;
private final Gson gson;

public Editor(SharedPreferences delegate, Gson gson) {
this.delegate = delegate;
this.gson = gson;
}

@Override
public Editor putBoolean(String key, boolean value) {
delegate.edit().putBoolean(key, value);

return this;
}

@Override
public Editor putFloat(String key, float value) {
delegate.edit().putFloat(key, value);

return this;
}

@Override
public Editor putInt(String key, int value) {
delegate.edit().putInt(key, value);

return this;
}

@Override
public Editor putLong(String key, long value) {
delegate.edit().putLong(key, value);

return this;
}

@Override
public Editor putString(String key, String value) {
delegate.edit().putString(key, value);

return this;
}

@Override
public Editor putStringSet(String key, Set<String> values) {
delegate.edit().putStringSet(key, values);

return this;
}

@Override
public <T> SharedPreferences.Editor putObject(String key, T object) {
delegate.edit().putString(key, object != null ? gson.toJson(object) : null);

return this;
}

@Override
public void apply() {
delegate.edit().apply();
}

@Override
public Editor clear() {
delegate.edit().clear();

return this;
}

@Override
public boolean commit() {
delegate.edit().commit();

return true;
}

@Override
public Editor remove(String s) {
delegate.edit().remove(s);

return this;
}
}

@Override
public ObjectEditor edit() {
return editor;
}

@Override
public Map<String, ?> getAll() {
return delegate.getAll();
}

@Override
public boolean getBoolean(String key, boolean defValue) {
return delegate.getBoolean(key, defValue);
}

@Override
public float getFloat(String key, float defValue) {
return delegate.getFloat(key, defValue);
}

@Override
public int getInt(String key, int defValue) {
return delegate.getInt(key, defValue);
}

@Override
public long getLong(String key, long defValue) {
return delegate.getLong(key, defValue);
}

@Nullable
@Override
public String getString(String key, String defValue) {
return delegate.getString(key, defValue);
}

@Nullable
@Override
public Set<String> getStringSet(String key, Set<String> defValues) {
return delegate.getStringSet(key, defValues);
}

@Nullable
@Override
public <T> T getObject(String key, Class<T> theClass) {
try {
final String json = delegate.getString(key, null);

return gson.fromJson(json, theClass);
} catch (JsonSyntaxException e) {
editor.remove(key);
editor.commit();

return null;
}
}

@Nullable
@Override
public <T> T getObject(String key, Type typeOf) {
try {
final String json = delegate.getString(key, null);

return gson.fromJson(json, typeOf);
} catch (JsonSyntaxException e) {
editor.remove(key);
editor.commit();

return null;
}
}

@Override
public boolean contains(String s) {
return delegate.contains(s);
}

@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
delegate.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
}

@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
delegate.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
}

}
39 changes: 19 additions & 20 deletions app/src/main/java/com/anbroidsdev/converter/MoneyConverter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.anbroidsdev.converter;

import java.util.Currency;
import java.util.Map;

/**
* Created by david on 23/2/15.
*/
public class MoneyConverter {

private Currency base;
private Map<Currency, Double> rates;
private Rates rates;

public MoneyConverter(Currency base, Map<Currency, Double> rates) {
public MoneyConverter(Currency base, Rates rates) {
setBase(base);
setRates(rates);
}
Expand All @@ -22,19 +21,19 @@ public void setBase(Currency base) {
}

if (rates != null) {
if (!rates.containsKey(base)) {
if (!rates.containsCurrency(base)) {
throw new IllegalArgumentException("Currency is not supported");
}

final Double inverseRate = 1.0 / rates.get(base);
final Double inverseRate = 1.0 / rates.get(base).getValue();

rates.remove(base);

for (Map.Entry<Currency, Double> entry : rates.entrySet()) {
entry.setValue(inverseRate * entry.getValue());
for (Rate rate : rates.list()) {
rate.setValue(inverseRate * rate.getValue());
}

rates.put(this.base, inverseRate);
rates.put(new Rate(this.base, inverseRate));
}

this.base = base;
Expand All @@ -44,13 +43,13 @@ public Currency getBase() {
return base;
}

public void setRates(Map<Currency, Double> rates) {
public void setRates(Rates rates) {
checkRates(rates);

this.rates = rates;
}

public Map<Currency, Double> getRates() {
public Rates getRates() {
return rates;
}

Expand All @@ -59,11 +58,11 @@ public double convert(double amount, Currency currency) {
}

public double convert(double amount, Currency currency, Currency base) {
if (!rates.containsKey(currency) && currency != this.base) {
if (!rates.containsCurrency(currency) && currency != this.base) {
throw new IllegalArgumentException("Currency is not supported");
}

if (!rates.containsKey(base) && base != this.base) {
if (!rates.containsCurrency(base) && base != this.base) {
throw new IllegalArgumentException("Base is not supported");
}

Expand All @@ -73,31 +72,31 @@ public double convert(double amount, Currency currency, Currency base) {

final Double rate;
if (currency == this.base) {
rate = 1.0 / rates.get(base);
rate = 1.0 / rates.get(base).getValue();
} else if (base == this.base) {
rate = rates.get(currency);
rate = rates.get(currency).getValue();
} else {
rate = 1.0 / rates.get(base) * rates.get(currency);
rate = 1.0 / rates.get(base).getValue() * rates.get(currency).getValue();
}

return rate * amount;
}

private void checkRates(Map<Currency, Double> rates) {
private void checkRates(Rates rates) {
if (rates == null || rates.isEmpty()) {
throw new IllegalArgumentException("Rates cannot be null or empty");
}

if (base != null && rates.containsKey(base)) {
if (base != null && rates.containsCurrency(base)) {
throw new IllegalArgumentException("Rates cannot contain the Base currency");
}

for (Map.Entry<Currency, Double> entry : rates.entrySet()) {
if (entry.getValue() == null) {
for (Rate rate : rates.list()) {
if (rate.getValue() == null) {
throw new IllegalArgumentException("Rates cannot have any null value");
}

if (entry.getValue() < 0) {
if (rate.getValue() < 0) {
throw new IllegalArgumentException("Rates cannot have any negative value");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.anbroidsdev.converter;

import android.content.SharedPreferences;

import java.lang.reflect.Type;

/**
* {@inheritDoc}
*/
public interface ObjectSharedPreferences extends SharedPreferences {

/**
* Retrieve an Object of a given class from the preferences.
*
* @param key The name of the preference to retrieve.
* @param theClass Type of the object to be returned.
*
* @return Returns the preference value if it exists, or {@code null}.
*/
public <T> T getObject(String key, Class<T> theClass);

/**
* Retrieve an Object of a given class from the preferences.
*
* @param key The name of the preference to retrieve.
* @param typeOf Type of the object to be returned.
*
* @return Returns the preference value if it exists, or {@code null}.
*/
public <T> T getObject(String key, Type typeOf);

public interface ObjectEditor extends SharedPreferences.Editor {

public <T> SharedPreferences.Editor putObject(String key, T object);

}

public ObjectEditor edit();

}
Loading