From 4ee655bef4cdf9e62a1b247e77754441de806f22 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 8 Jun 2015 16:57:44 +0200 Subject: Implemented sorting using reflection, not sure it was a good idea though --- src/eu/equalparts/cardbase/utils/IO.java | 35 ----------------------- src/eu/equalparts/cardbase/utils/JSON.java | 35 +++++++++++++++++++++++ src/eu/equalparts/cardbase/utils/MTGUniverse.java | 17 ++++++----- 3 files changed, 45 insertions(+), 42 deletions(-) delete mode 100644 src/eu/equalparts/cardbase/utils/IO.java create mode 100644 src/eu/equalparts/cardbase/utils/JSON.java (limited to 'src/eu/equalparts/cardbase/utils') diff --git a/src/eu/equalparts/cardbase/utils/IO.java b/src/eu/equalparts/cardbase/utils/IO.java deleted file mode 100644 index 5d4bef5..0000000 --- a/src/eu/equalparts/cardbase/utils/IO.java +++ /dev/null @@ -1,35 +0,0 @@ -package eu.equalparts.cardbase.utils; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * This class simply holds an {@code ObjectMapper} to be used whenever JSON must be parsed. - * In the future it may be removed in favour of individual mappers for each function. - * - * @author Eduardo Pedroni - */ -public final class IO { - - /** - * The Jackson {@code ObjectMapper} which parses fetched JSON files. - */ - public static final ObjectMapper jsonMapper = createMapper(); - - /** - * Private constructor, this class is not to be instantiated. - */ - private IO() {} - - /** - * Instantiate and configure Jackson mapper statically. - * - * @return the {@code ObjectMapper}, ready to use. - */ - private static ObjectMapper createMapper() { - ObjectMapper objectMapper = new ObjectMapper(); - // TODO decide what to do about this - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - return objectMapper; - } -} diff --git a/src/eu/equalparts/cardbase/utils/JSON.java b/src/eu/equalparts/cardbase/utils/JSON.java new file mode 100644 index 0000000..a5992c7 --- /dev/null +++ b/src/eu/equalparts/cardbase/utils/JSON.java @@ -0,0 +1,35 @@ +package eu.equalparts.cardbase.utils; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * This class simply holds an {@code ObjectMapper} to be used whenever JSON must be parsed. + * In the future it may be removed in favour of individual mappers for each function. + * + * @author Eduardo Pedroni + */ +public final class JSON { + + /** + * The Jackson {@code ObjectMapper} which parses fetched JSON files. + */ + public static final ObjectMapper mapper = createMapper(); + + /** + * Private constructor, this class is not to be instantiated. + */ + private JSON() {} + + /** + * Instantiate and configure Jackson mapper statically. + * + * @return the {@code ObjectMapper}, ready to use. + */ + private static ObjectMapper createMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + // TODO decide what to do about this + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return objectMapper; + } +} diff --git a/src/eu/equalparts/cardbase/utils/MTGUniverse.java b/src/eu/equalparts/cardbase/utils/MTGUniverse.java index 7211f47..0bcda5c 100644 --- a/src/eu/equalparts/cardbase/utils/MTGUniverse.java +++ b/src/eu/equalparts/cardbase/utils/MTGUniverse.java @@ -10,7 +10,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import eu.equalparts.cardbase.data.Card; -import eu.equalparts.cardbase.data.CardSet; +import eu.equalparts.cardbase.data.CardSetInformation; import eu.equalparts.cardbase.data.FullCardSet; /** @@ -37,7 +37,7 @@ public final class MTGUniverse { /** * A cache of CardSets to avoid querying the server many times for the same information. */ - private static ArrayList cardSets; + private static ArrayList cardSets; /** * A cache of {@code FullCardSets} to avoid querying the server many times for the same information. @@ -76,6 +76,9 @@ public final class MTGUniverse { /** * Returns the specified set in the form of a {@code FullCardSet} object. If the specified * set code does not correspond to a set, this returns null. + *
+ * This method takes care of case differences in set code names. + * * * @param setCode the code of the set to be returned. * @return the requested {@code FullCardSet} or null if no set matches the given code. @@ -94,7 +97,7 @@ public final class MTGUniverse { } // not cached; fetch, cache and return it else { - requestedSet = IO.jsonMapper.readValue(new URL(BASE_URL + validCode + ".json"), FullCardSet.class); + requestedSet = JSON.mapper.readValue(new URL(BASE_URL + validCode + ".json"), FullCardSet.class); // MTG JSON does not include set code in the card information, but it is useful for sorting for (Card card : requestedSet.getCards()) { card.setCode = validCode; @@ -108,17 +111,17 @@ public final class MTGUniverse { /** * @return a list of all card sets in the form of {@code CardSet} objects. */ - public static ArrayList getCardSetList() { + public static ArrayList getCardSetList() { // if the list isn't cached, fetch and cache it if (cardSets == null) { try { - cardSets = IO.jsonMapper.readValue(new URL(BASE_URL + "SetList.json"), new TypeReference>() {}); + cardSets = JSON.mapper.readValue(new URL(BASE_URL + "SetList.json"), new TypeReference>() {}); } catch (Exception e) { System.out.println("Error: could not fetch/parse set code list from upstream, loading fallback json..."); e.printStackTrace(); try { - cardSets = IO.jsonMapper.readValue(MTGUniverse.class.getResourceAsStream(FALLBACK_LIST_PATH), new TypeReference>() {}); + cardSets = JSON.mapper.readValue(MTGUniverse.class.getResourceAsStream(FALLBACK_LIST_PATH), new TypeReference>() {}); } catch (Exception f) { System.out.println("Error: could not parse fallback set code list, aborting..."); f.printStackTrace(); @@ -142,7 +145,7 @@ public final class MTGUniverse { * @return the valid form of the set code if any, null otherwise. */ public static String validateSetCode(String setCode) { - for (CardSet cardSet : getCardSetList()) { + for (CardSetInformation cardSet : getCardSetList()) { if (cardSet.getCode().equalsIgnoreCase(setCode)) { return cardSet.getCode(); } -- cgit v1.2.3