blob: a452ab0e93d473497331d8183e7a789fbbc33446 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package com.camilstaps.common;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
/**
* Created by camilstaps on 11-4-15.
*/
public class DatedString {
protected final Date date;
protected final String string;
public DatedString(Date date, String string) {
this.date = date;
this.string = string;
}
public String getString() {
return string;
}
public Date getDate() {
return date;
}
@Override
public String toString() {
JSONObject json = new JSONObject();
try {
json.put("date", date.toString());
json.put("text", string);
} catch (JSONException e) {}
return json.toString();
}
public static Object fromString(String s, Class castTo) throws JSONException, ParseException {
JSONObject json = new JSONObject(s);
try {
return castTo.cast(castTo.getDeclaredConstructor(Date.class, String.class).newInstance(new Date(json.getString("date")), json.getString("text")));
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
} catch (InvocationTargetException e) {
return null;
} catch (NoSuchMethodException e) {
return null;
}
}
}
|