Nä, det är helt korrekt att du inte sätta om självreferensen this till något annat innifrån den instans som this representerar.
Jag skulle göra på en av följade sätt.
1. Om vi använder din kod lite:
if(cache.exists()) {
try {
final ObjectInputStream in = new ObjectInputStream(
new FileInputStream(cache)
);
CurrencyTable tbl = (CurrencyTable) in.readObject();
this.putAll(tbl);
this.lastupdated = tbl.getLastUpdated();
in.close();
return;
} catch (IOException e) {
} catch (ClassNotFoundException e) {;}
}
2. Bryt ut själva uppläsningen av objektet i en klassmetod och ladda sedan in xml-en.
package mobigear.currency;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.util.HashMap;
import java.util.Date;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.*;
import java.text.SimpleDateFormat;
public class CurrencyTable extends HashMap implements Serializable {
private String lastUpdated;
public CurrencyTable() {
super();
}
public static CurrencyTable getCachedCurrencyTable()
{
CurrencyTable tbl = null;
//Current date:
String today = (new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
File cache = new File(Config.CACHE_PATH+today+".dat");
if(cache.exists()) {
try {
final ObjectInputStream in = new ObjectInputStream(
new FileInputStream(cache)
);
tbl = (CurrencyTable) in.readObject();
in.close();
return;
} catch (IOException e) {
} catch (ClassNotFoundException e) {;}
}
return tbl;
}
public void load() throws CurrencyException {
URL url = null;
try {
url = new URL(Config.XML_URL);
} catch (MalformedURLException e) {
throw new CurrencyException("load(): "+e.getMessage());
}
URLConnection conn = null;
try {
conn = url.openConnection();
} catch (java.io.IOException e) {
throw new CurrencyException("load: "+e.getMessage());
}
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new CurrencyException("load(): "+e.getMessage());
}
Document doc = null;
try {
doc = docBuilder.parse(conn.getInputStream());
} catch (SAXException e) {
} catch (IOException e) {
throw new CurrencyException("laod: "+e.getMessage());
}
doc.getDocumentElement().normalize();
Element cube = (Element)doc.getDocumentElement().getChildNodes().item(5).getChildNodes().item(1);
lastUpdated = cube.getAttribute("time");
NodeList lst = cube.getChildNodes();
for(int i=0;i<lst.getLength();i++) {
Node node = lst.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
Element currency = (Element)node;
this.put(currency.getAttribute("currency"),new Double(Double.parseDouble(currency.getAttribute("rate"))));
}
}
this.put(Config.DEFAULT_CURRENCY, new Double(1));
//Save cache:
try {
final ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(cache)
);
out.writeObject(this);
out.close();
} catch (IOException e) {
//throw new CurrencyException("load(): unable to save cache: "+e.getMessage());
}
}
}
Jag antar att du nu anropar CurrencyTable såhär:
CurrencyTable ct = new CurrencyTable();
ct.load();
Nu blir det istället såhär:
CurrencyTable ct = CurrencyTable.getCachedCurrencyTable();
if(ct != null)
ct.load();