mirror of
https://github.com/correl/mage.git
synced 2024-12-24 11:50:45 +00:00
New file based cache for creature types and card names. +Test for cache consistency.
This commit is contained in:
parent
7d4aaec015
commit
b6bb69f8b8
4 changed files with 173 additions and 0 deletions
|
@ -28,6 +28,12 @@
|
|||
<version>1.2.14</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
36
Mage.Sets/src/mage/cache/Cache.java
vendored
Normal file
36
Mage.Sets/src/mage/cache/Cache.java
vendored
Normal file
|
@ -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<String, Object> cacheObjects = new HashMap<String, Object>();
|
||||
|
||||
public Cache(String name, int version) {
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Map<String, Object> getCacheObjects() {
|
||||
return cacheObjects;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
96
Mage.Sets/src/mage/cache/CacheDataHelper.java
vendored
Normal file
96
Mage.Sets/src/mage/cache/CacheDataHelper.java
vendored
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
35
Mage.Sets/src/mage/cache/CacheTest.java
vendored
Normal file
35
Mage.Sets/src/mage/cache/CacheTest.java
vendored
Normal file
|
@ -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<String> names = Sets.getCardNames();
|
||||
//Set<String> nonLandNames = Sets.getNonLandCardNames();
|
||||
Set<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue