summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-04-12 13:56:02 (EDT)
committer Chris Aniszczyk2011-04-12 14:13:55 (EDT)
commitffacd2c4a10a0afcb280f3ed0d249fdb28aa686a (patch)
treec925602706d33dd7a175b4a2773bcf299efee18a
parent259830869feaa6a1124d3c2a7e29eb7d72f71d40 (diff)
downloadegit-github-ffacd2c4a10a0afcb280f3ed0d249fdb28aa686a.zip
egit-github-ffacd2c4a10a0afcb280f3ed0d249fdb28aa686a.tar.gz
egit-github-ffacd2c4a10a0afcb280f3ed0d249fdb28aa686a.tar.bz2
Update date formatter to use new single formatrefs/changes/76/3076/3
Support date and datetime attributes in mapper. Change-Id: I207f44e8a2da322a77c258ae4e630e823f044c85 Signed-off-by: Kevin Sawicki <kevin@github.com> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java54
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java3
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubTaskAttributeMapper.java14
3 files changed, 20 insertions, 51 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java
index de356f1..aa00d91 100644
--- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/DateFormatter.java
@@ -20,53 +20,24 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
-import java.util.LinkedList;
-import java.util.List;
import java.util.TimeZone;
/**
- * Date formatter for multiple date formats present in the GitHub v2 API.
+ * Date formatter for date format present in the GitHub v3 API.
*
* @author Kevin Sawicki (kevin@github.com)
*/
public class DateFormatter implements JsonDeserializer<Date> {
- /**
- * DATE_FORMAT1
- */
- public static final String DATE_FORMAT1 = "yyyy/MM/dd HH:mm:ss Z";
-
- /**
- * DATE_FORMAT2
- */
- public static final String DATE_FORMAT2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
-
- /**
- * DATE_FORMAT3
- */
- public static final String DATE_FORMAT3 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
-
- /**
- * DATE_FORMAT4
- */
- public static final String DATE_FORMAT4 = "yyyy-MM-dd'T'HH:mm:ss";
-
- private static final String[] FORMATS = new String[] { DATE_FORMAT1,
- DATE_FORMAT2, DATE_FORMAT3, DATE_FORMAT4 };
-
- private List<DateFormat> formats;
+ private DateFormat format;
/**
* Create date formatter
*/
public DateFormatter() {
- this.formats = new LinkedList<DateFormat>();
- TimeZone timeZone = TimeZone.getTimeZone("Zulu");
- for (String format : FORMATS) {
- DateFormat dateFormat = new SimpleDateFormat(format);
- dateFormat.setTimeZone(timeZone);
- this.formats.add(dateFormat);
- }
+ this.format = new SimpleDateFormat(IGitHubConstants.DATE_FORMAT);
+ TimeZone timeZone = TimeZone.getTimeZone("Zulu"); //$NON-NLS-1$
+ this.format.setTimeZone(timeZone);
}
/**
@@ -75,18 +46,13 @@ public class DateFormatter implements JsonDeserializer<Date> {
*/
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
- String string = json.getAsString();
- Exception exception = null;
- for (DateFormat format : this.formats) {
- try {
- synchronized (format) {
- return format.parse(string);
- }
- } catch (ParseException e) {
- exception = e;
+ try {
+ synchronized (this.format) {
+ return this.format.parse(json.getAsString());
}
+ } catch (ParseException e) {
+ throw new JsonParseException(e);
}
- throw new JsonParseException(exception);
}
}
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java
index 3493d38..751f891 100644
--- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java
@@ -19,6 +19,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
+import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
@@ -51,7 +52,7 @@ public class GitHubClient {
private HttpClient client = new HttpClient();
private Gson gson = new GsonBuilder()
- .setDateFormat(IGitHubConstants.DATE_FORMAT)
+ .registerTypeAdapter(Date.class, new DateFormatter())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubTaskAttributeMapper.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubTaskAttributeMapper.java
index 7cdfd9d..776ffc0 100644
--- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubTaskAttributeMapper.java
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubTaskAttributeMapper.java
@@ -40,14 +40,16 @@ public class GitHubTaskAttributeMapper extends TaskAttributeMapper {
* @see org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper#getValueLabels(org.eclipse.mylyn.tasks.core.data.TaskAttribute)
*/
public List<String> getValueLabels(TaskAttribute taskAttribute) {
- if (TaskAttribute.TYPE_DATE.equals(taskAttribute.getMetaData()
- .getType())) {
- String date = taskAttribute.getValue();
- if (date.length() > 0) {
+ String type = taskAttribute.getMetaData().getType();
+ if (TaskAttribute.TYPE_DATE.equals(type)
+ || TaskAttribute.TYPE_DATETIME.equals(type)) {
+ String value = taskAttribute.getValue();
+ if (value.length() > 0) {
+ Date date = new Date(Long.parseLong(value));
synchronized (this.format) {
- return Collections.singletonList(this.format
- .format(new Date(Long.parseLong(date))));
+ value = this.format.format(date);
}
+ return Collections.singletonList(value);
}
}
return super.getValueLabels(taskAttribute);