master
Max Ammann 2016-06-14 15:48:04 +02:00
parent 839758f0a4
commit 5632bd8c23
1 changed files with 33 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
/** /**
@ -20,29 +21,51 @@ import java.util.Scanner;
*/ */
public class SynchronizeConfig implements Parcelable { public class SynchronizeConfig implements Parcelable {
public static final Random RANDOM = new Random();
/** /**
* The name of this config * The name of this config
*/ */
private final String name; private String name;
/** /**
* The data structure * The data structure
*/ */
private final JSONObject json; private JSONObject json;
public SynchronizeConfig(String name, JSONObject json) { public SynchronizeConfig(String name, JSONObject json) {
this.name = name; this.name = name;
this.json = json; this.json = json;
} }
public SynchronizeConfig(String name) { public SynchronizeConfig(String name, long id) {
this(name, new JSONObject()); this(name, new JSONObject());
try {
json.put("id", id);
} catch (JSONException e) {
throw new RuntimeException("Failed setting id!");
}
}
public SynchronizeConfig(String name) {
this(name, randomID(name));
}
public long getID() {
try {
return json.getLong("id");
} catch (JSONException e) {
throw new RuntimeException("Config has no id!");
}
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
public int getSize(Resources resources) { public int getSize(Resources resources) {
return json.optInt("size", resources.getInteger(R.integer.size)); return json.optInt("size", resources.getInteger(R.integer.size));
} }
@ -115,6 +138,13 @@ public class SynchronizeConfig implements Parcelable {
return configs; return configs;
} }
public static long randomID(String name) {
int rnd = RANDOM.nextInt();
int hash = name.hashCode();
return rnd + hash;
}
public static void save(Iterable<SynchronizeConfig> configs, OutputStream fos) throws JSONException, IOException { public static void save(Iterable<SynchronizeConfig> configs, OutputStream fos) throws JSONException, IOException {
JSONObject jsonConfigs = new JSONObject(); JSONObject jsonConfigs = new JSONObject();