diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/PageableResponseListMapper.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/PageableResponseListMapper.java deleted file mode 100644 index a1a4ec119..000000000 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/PageableResponseListMapper.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Twidere - Twitter client for Android - * - * Copyright (C) 2012-2015 Mariotaku Lee - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.mariotaku.twidere.api.twitter.model.impl; - -import com.bluelinelabs.logansquare.JsonMapper; -import com.bluelinelabs.logansquare.LoganSquare; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -import org.mariotaku.twidere.api.twitter.util.TwitterConverter; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import org.mariotaku.twidere.api.twitter.model.Status; -import org.mariotaku.twidere.api.twitter.model.User; - -public final class PageableResponseListMapper extends JsonMapper> { - - private static final Map, PageableResponseListMapper> instanceCache = new HashMap<>(); - - private final String listField; - private final Class elementType; - - public PageableResponseListMapper(String listField, Class elementType) { - this.listField = listField; - this.elementType = elementType; - } - - @Override - public PageableResponseListImpl parse(JsonParser jsonParser) throws IOException { - PageableResponseListImpl instance = new PageableResponseListImpl<>(); - if (jsonParser.getCurrentToken() == null) { - jsonParser.nextToken(); - } - if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) { - jsonParser.skipChildren(); - return null; - } - while (jsonParser.nextToken() != JsonToken.END_OBJECT) { - String fieldName = jsonParser.getCurrentName(); - jsonParser.nextToken(); - parseField(instance, fieldName, jsonParser); - jsonParser.skipChildren(); - } - return instance; - } - - public void parseField(PageableResponseListImpl instance, String fieldName, JsonParser jsonParser) throws IOException { - if (listField.equals(fieldName)) { - instance.addAll(LoganSquare.mapperFor(elementType).parseList(jsonParser)); - } else if ("previous_cursor".equals(fieldName)) { - instance.previousCursor = jsonParser.getValueAsLong(0); - } else if ("next_cursor".equals(fieldName)) { - instance.previousCursor = jsonParser.getValueAsLong(0); - } - } - - @Override - public void serialize(PageableResponseListImpl object, JsonGenerator jsonGenerator, boolean writeStartAndEnd) throws IOException { - throw new UnsupportedOperationException(); - } - - public synchronized static PageableResponseListMapper get(Class cls) { - @SuppressWarnings("unchecked") - final PageableResponseListMapper cache = (PageableResponseListMapper) instanceCache.get(cls); - if (cache != null) return cache; - final String listField; - if (User.class.isAssignableFrom(cls)) { - listField = "users"; - } else if (Status.class.isAssignableFrom(cls)) { - listField = "statuses"; - } else { - throw new TwitterConverter.UnsupportedTypeException(cls); - } - final PageableResponseListMapper created = new PageableResponseListMapper<>(listField, cls); - instanceCache.put(cls, created); - return created; - } - -} diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/PageableResponseListWrapper.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/PageableResponseListWrapper.java index aed3762a1..edbf55bad 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/PageableResponseListWrapper.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/PageableResponseListWrapper.java @@ -22,15 +22,14 @@ package org.mariotaku.twidere.api.twitter.model.impl; import com.bluelinelabs.logansquare.annotation.JsonField; import com.bluelinelabs.logansquare.annotation.JsonObject; -import org.mariotaku.twidere.api.twitter.util.TwitterConverter; - -import java.lang.reflect.Type; -import java.util.ArrayList; - import org.mariotaku.twidere.api.twitter.model.PageableResponseList; import org.mariotaku.twidere.api.twitter.model.Status; import org.mariotaku.twidere.api.twitter.model.User; import org.mariotaku.twidere.api.twitter.model.UserList; +import org.mariotaku.twidere.api.twitter.util.TwitterConverter; + +import java.lang.reflect.Type; +import java.util.ArrayList; /** * Created by mariotaku on 15/5/7. @@ -56,13 +55,18 @@ public class PageableResponseListWrapper extends TwitterResponseImpl implements public PageableResponseList getWrapped(Object extra) { final Type[] typeArgs = (Type[]) extra; final Class elementType = (Class) typeArgs[0]; + PageableResponseListImpl list; if (User.class.isAssignableFrom(elementType)) { - return new PageableResponseListImpl<>(users); + list = new PageableResponseListImpl<>(users); } else if (Status.class.isAssignableFrom(elementType)) { - return new PageableResponseListImpl<>(statuses); + list = new PageableResponseListImpl<>(statuses); } else if (UserList.class.isAssignableFrom(elementType)) { - return new PageableResponseListImpl<>(userLists); + list = new PageableResponseListImpl<>(userLists); + } else { + throw new TwitterConverter.UnsupportedTypeException(elementType); } - throw new TwitterConverter.UnsupportedTypeException(elementType); + list.previousCursor = previousCursor; + list.nextCursor = nextCursor; + return list; } }