From b6bb69f8b8ddb8a39dfccc1d44d57b74dfdfea7d Mon Sep 17 00:00:00 2001 From: magenoxx Date: Mon, 25 Jun 2012 15:01:31 +0400 Subject: [PATCH] New file based cache for creature types and card names. +Test for cache consistency. --- Mage.Sets/pom.xml | 6 ++ Mage.Sets/src/mage/cache/Cache.java | 36 +++++++ Mage.Sets/src/mage/cache/CacheDataHelper.java | 96 +++++++++++++++++++ Mage.Sets/src/mage/cache/CacheTest.java | 35 +++++++ 4 files changed, 173 insertions(+) create mode 100644 Mage.Sets/src/mage/cache/Cache.java create mode 100644 Mage.Sets/src/mage/cache/CacheDataHelper.java create mode 100644 Mage.Sets/src/mage/cache/CacheTest.java diff --git a/Mage.Sets/pom.xml b/Mage.Sets/pom.xml index 21b9940e0e..8c4554d899 100644 --- a/Mage.Sets/pom.xml +++ b/Mage.Sets/pom.xml @@ -28,6 +28,12 @@ 1.2.14 jar + + + junit + junit + 4.8.2 + diff --git a/Mage.Sets/src/mage/cache/Cache.java b/Mage.Sets/src/mage/cache/Cache.java new file mode 100644 index 0000000000..eb9c207b44 --- /dev/null +++ b/Mage.Sets/src/mage/cache/Cache.java @@ -0,0 +1,36 @@ +package mage.cache; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +/** + * Cache model + * + * @author noxx + */ +public class Cache implements Serializable { + + private int version; + private String name; + private Map cacheObjects = new HashMap(); + + public Cache(String name, int version) { + this.name = name; + this.version = version; + } + + public int getVersion() { + return version; + } + + public String getName() { + return name; + } + + public Map getCacheObjects() { + return cacheObjects; + } + + private static final long serialVersionUID = 1L; +} diff --git a/Mage.Sets/src/mage/cache/CacheDataHelper.java b/Mage.Sets/src/mage/cache/CacheDataHelper.java new file mode 100644 index 0000000000..e2d3bdae11 --- /dev/null +++ b/Mage.Sets/src/mage/cache/CacheDataHelper.java @@ -0,0 +1,96 @@ +package mage.cache; + +import org.apache.log4j.Logger; + +import java.io.*; + +/** + * @author noxx + */ +public class CacheDataHelper { + + private static final Logger log = Logger.getLogger(CacheDataHelper.class); + + /** + * Save object on disk. + * + * @param cache Cache object to save. + * @param name Part of name that will be used to form original filename to save object to. + */ + public static void cacheObject(Cache cache, String name) { + ObjectOutputStream oos = null; + try { + File dir = new File("cache"); + if (!dir.exists() || dir.exists() && dir.isFile()) { + boolean bCreated = dir.mkdir(); + if (!bCreated) { + log.error("Couldn't create directory for cache."); + return; + } + } + File f = new File("cache" + File.separator + name + ".obj"); + if (!f.exists()) { + f.createNewFile(); + } + oos = new ObjectOutputStream(new FileOutputStream(f)); + oos.writeObject(cache); + oos.close(); + + } catch (FileNotFoundException e) { + log.error("Error while caching data: ", e); + return; + } catch (IOException io) { + log.error("Error while caching data: ", io); + return; + } + } + + /** + * Gets Cache object from cache folder. + * + * @param name + * @return + */ + public static Cache getCachedObject(String name) { + ObjectInputStream ois = null; + try { + File dir = new File("cache"); + if (!dir.exists() || dir.exists() && dir.isFile()) { + return null; + } + File f = new File("cache" + File.separator + name + ".obj"); + if (!f.exists()) { + log.warn("Couldn't find cache for name: " + name); + return null; + } + ois = new ObjectInputStream(new FileInputStream(f)); + Object object = ois.readObject(); + + if (!(object instanceof Cache)) { + log.error("Cached object has wrong type: " + object.getClass().getName()); + return null; + } + + return (Cache)object; + + } catch (FileNotFoundException e) { + log.error("Error while reading cached data: ", e); + return null; + } catch (IOException io) { + log.error("Error while reading cached data: ", io); + return null; + } catch (ClassNotFoundException e) { + log.error("Error while reading cached data: ", e); + return null; + } finally { + try { + if (ois != null) { + ois.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/Mage.Sets/src/mage/cache/CacheTest.java b/Mage.Sets/src/mage/cache/CacheTest.java new file mode 100644 index 0000000000..92c0639644 --- /dev/null +++ b/Mage.Sets/src/mage/cache/CacheTest.java @@ -0,0 +1,35 @@ +package mage.cache; + +import mage.Constants; +import mage.cards.Card; +import mage.cards.ExpansionSet; +import mage.sets.Sets; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Set; + +/** + * @author noxx + */ +public class CacheTest { + + @Test + public void testCacheConsistency() { + //Set names = Sets.getCardNames(); + //Set nonLandNames = Sets.getNonLandCardNames(); + Set creatureTypes = Sets.getCreatureTypes(); + + for (ExpansionSet set : Sets.getInstance().values()) { + for (Card card : set.getCards()) { + if (card.getCardType().contains(Constants.CardType.CREATURE)) { + for (String type : card.getSubtype()) { + if (!creatureTypes.contains(type)) { + Assert.assertTrue("Couldn't find a creature type in the cache: " + type, false); + } + } + } + } + } + } +}