From b048b016adffeab4d60803d666e48f6ab010f362 Mon Sep 17 00:00:00 2001 From: zzz Date: Fri, 11 Mar 2011 02:01:20 +0000 Subject: [PATCH] generics --- .../src/net/i2p/addressbook/AddressBook.java | 6 +- .../src/net/i2p/addressbook/ConfigParser.java | 57 ++++++++----------- .../java/src/net/i2p/addressbook/Daemon.java | 32 +++++------ .../i2p/addressbook/SubscriptionIterator.java | 10 ++-- .../net/i2p/addressbook/SubscriptionList.java | 35 +++++------- 5 files changed, 63 insertions(+), 77 deletions(-) diff --git a/apps/addressbook/java/src/net/i2p/addressbook/AddressBook.java b/apps/addressbook/java/src/net/i2p/addressbook/AddressBook.java index 22bf33b5c..99d899501 100644 --- a/apps/addressbook/java/src/net/i2p/addressbook/AddressBook.java +++ b/apps/addressbook/java/src/net/i2p/addressbook/AddressBook.java @@ -42,7 +42,7 @@ class AddressBook { private String location; - private Map addresses; + private Map addresses; private boolean modified; @@ -53,7 +53,7 @@ class AddressBook { * A Map containing human readable addresses as keys, mapped to * base64 i2p destinations. */ - public AddressBook(Map addresses) { + public AddressBook(Map addresses) { this.addresses = addresses; } @@ -139,7 +139,7 @@ class AddressBook { * is a human readable name, and the value is a base64 i2p * destination. */ - public Map getAddresses() { + public Map getAddresses() { return this.addresses; } diff --git a/apps/addressbook/java/src/net/i2p/addressbook/ConfigParser.java b/apps/addressbook/java/src/net/i2p/addressbook/ConfigParser.java index 7efc27ea5..b09804f23 100644 --- a/apps/addressbook/java/src/net/i2p/addressbook/ConfigParser.java +++ b/apps/addressbook/java/src/net/i2p/addressbook/ConfigParser.java @@ -30,7 +30,6 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StringReader; import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -85,8 +84,8 @@ class ConfigParser { * if the BufferedReader cannot be read. * */ - public static Map parse(BufferedReader input) throws IOException { - Map result = new HashMap(); + public static Map parse(BufferedReader input) throws IOException { + Map result = new HashMap(); String inputLine; inputLine = input.readLine(); while (inputLine != null) { @@ -111,11 +110,11 @@ class ConfigParser { * @throws IOException * if file cannot be read. */ - public static Map parse(File file) throws IOException { + public static Map parse(File file) throws IOException { FileInputStream fileStream = new FileInputStream(file); BufferedReader input = new BufferedReader(new InputStreamReader( fileStream)); - Map rv = ConfigParser.parse(input); + Map rv = ConfigParser.parse(input); try { fileStream.close(); } catch (IOException ioe) {} @@ -132,7 +131,7 @@ class ConfigParser { * @throws IOException * if file cannot be read. */ - public static Map parse(String string) throws IOException { + public static Map parse(String string) throws IOException { StringReader stringReader = new StringReader(string); BufferedReader input = new BufferedReader(stringReader); return ConfigParser.parse(input); @@ -149,14 +148,13 @@ class ConfigParser { * @return A Map containing the key, value pairs from file, or if file * cannot be read, map. */ - public static Map parse(File file, Map map) { - Map result; + public static Map parse(File file, Map map) { + Map result; try { result = ConfigParser.parse(file); - for (Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { - String key = (String) iter.next(); - if (!result.containsKey(key)) - result.put(key, map.get(key)); + for (Map.Entry entry : map.entrySet()) { + if (!result.containsKey(entry.getKey())) + result.put(entry.getKey(), entry.getValue()); } } catch (IOException exp) { result = map; @@ -177,9 +175,9 @@ class ConfigParser { * @throws IOException * if input cannot be read. */ - public static List parseSubscriptions(BufferedReader input) + public static List parseSubscriptions(BufferedReader input) throws IOException { - List result = new LinkedList(); + List result = new LinkedList(); String inputLine = input.readLine(); while (inputLine != null) { inputLine = ConfigParser.stripComments(inputLine).trim(); @@ -201,11 +199,11 @@ class ConfigParser { * @throws IOException * if file cannot be read. */ - public static List parseSubscriptions(File file) throws IOException { + public static List parseSubscriptions(File file) throws IOException { FileInputStream fileStream = new FileInputStream(file); BufferedReader input = new BufferedReader(new InputStreamReader( fileStream)); - List rv = ConfigParser.parseSubscriptions(input); + List rv = ConfigParser.parseSubscriptions(input); try { fileStream.close(); } catch (IOException ioe) {} @@ -221,7 +219,7 @@ class ConfigParser { * @throws IOException * if string cannot be read. */ - public static List parseSubscriptions(String string) throws IOException { + public static List parseSubscriptions(String string) throws IOException { StringReader stringReader = new StringReader(string); BufferedReader input = new BufferedReader(stringReader); return ConfigParser.parseSubscriptions(input); @@ -238,8 +236,8 @@ class ConfigParser { * @return A List consisting of one element for each line in file, or if * file cannot be read, list. */ - public static List parseSubscriptions(File file, List list) { - List result; + public static List parseSubscriptions(File file, List list) { + List result; try { result = ConfigParser.parseSubscriptions(file); } catch (IOException exp) { @@ -263,12 +261,9 @@ class ConfigParser { * @throws IOException * if the BufferedWriter cannot be written to. */ - public static void write(Map map, BufferedWriter output) throws IOException { - Iterator keyIter = map.keySet().iterator(); - - while (keyIter.hasNext()) { - String key = (String) keyIter.next(); - output.write(key + "=" + (String) map.get(key)); + public static void write(Map map, BufferedWriter output) throws IOException { + for (Map.Entry entry : map.entrySet()) { + output.write(entry.getKey() + '=' + entry.getValue()); output.newLine(); } output.close(); @@ -288,7 +283,7 @@ class ConfigParser { * @throws IOException * if file cannot be written to. */ - public static void write(Map map, File file) throws IOException { + public static void write(Map map, File file) throws IOException { boolean success = false; if (!isWindows) { File tmp = SecureFile.createTempFile("temp-", ".tmp", file.getAbsoluteFile().getParentFile()); @@ -318,12 +313,10 @@ class ConfigParser { * @throws IOException * if output cannot be written to. */ - public static void writeSubscriptions(List list, BufferedWriter output) + public static void writeSubscriptions(List list, BufferedWriter output) throws IOException { - Iterator iter = list.iterator(); - - while (iter.hasNext()) { - output.write((String) iter.next()); + for (String s : list) { + output.write(s); output.newLine(); } output.close(); @@ -340,7 +333,7 @@ class ConfigParser { * @throws IOException * if output cannot be written to. */ - public static void writeSubscriptions(List list, File file) + public static void writeSubscriptions(List list, File file) throws IOException { ConfigParser.writeSubscriptions(list, new BufferedWriter( new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8"))); diff --git a/apps/addressbook/java/src/net/i2p/addressbook/Daemon.java b/apps/addressbook/java/src/net/i2p/addressbook/Daemon.java index 274fa8c4f..e728506b3 100644 --- a/apps/addressbook/java/src/net/i2p/addressbook/Daemon.java +++ b/apps/addressbook/java/src/net/i2p/addressbook/Daemon.java @@ -83,26 +83,26 @@ public class Daemon { * @param home * The directory containing addressbook's configuration files. */ - public void update(Map settings, String home) { - File masterFile = new File(home, (String) settings + public void update(Map settings, String home) { + File masterFile = new File(home, settings .get("master_addressbook")); - File routerFile = new File(home, (String) settings + File routerFile = new File(home, settings .get("router_addressbook")); File published = null; if ("true".equals(settings.get("should_publish"))) - published = new File(home, (String) settings + published = new File(home, settings .get("published_addressbook")); - File subscriptionFile = new File(home, (String) settings + File subscriptionFile = new File(home, settings .get("subscriptions")); - File logFile = new File(home, (String) settings.get("log")); - File etagsFile = new File(home, (String) settings.get("etags")); - File lastModifiedFile = new File(home, (String) settings + File logFile = new File(home, settings.get("log")); + File etagsFile = new File(home, settings.get("etags")); + File lastModifiedFile = new File(home, settings .get("last_modified")); - File lastFetchedFile = new File(home, (String) settings + File lastFetchedFile = new File(home, settings .get("last_fetched")); long delay; try { - delay = Long.parseLong((String) settings.get("update_delay")); + delay = Long.parseLong(settings.get("update_delay")); } catch (NumberFormatException nfe) { delay = 12; } @@ -111,13 +111,13 @@ public class Daemon { AddressBook master = new AddressBook(masterFile); AddressBook router = new AddressBook(routerFile); - List defaultSubs = new LinkedList(); + List defaultSubs = new LinkedList(); // defaultSubs.add("http://i2p/NF2RLVUxVulR3IqK0sGJR0dHQcGXAzwa6rEO4WAWYXOHw-DoZhKnlbf1nzHXwMEJoex5nFTyiNMqxJMWlY54cvU~UenZdkyQQeUSBZXyuSweflUXFqKN-y8xIoK2w9Ylq1k8IcrAFDsITyOzjUKoOPfVq34rKNDo7fYyis4kT5bAHy~2N1EVMs34pi2RFabATIOBk38Qhab57Umpa6yEoE~rbyR~suDRvD7gjBvBiIKFqhFueXsR2uSrPB-yzwAGofTXuklofK3DdKspciclTVzqbDjsk5UXfu2nTrC1agkhLyqlOfjhyqC~t1IXm-Vs2o7911k7KKLGjB4lmH508YJ7G9fLAUyjuB-wwwhejoWqvg7oWvqo4oIok8LG6ECR71C3dzCvIjY2QcrhoaazA9G4zcGMm6NKND-H4XY6tUWhpB~5GefB3YczOqMbHq4wi0O9MzBFrOJEOs3X4hwboKWANf7DT5PZKJZ5KorQPsYRSq0E3wSOsFCSsdVCKUGsAAAA/i2p/hosts.txt"); defaultSubs.add("http://www.i2p2.i2p/hosts.txt"); SubscriptionList subscriptions = new SubscriptionList(subscriptionFile, - etagsFile, lastModifiedFile, lastFetchedFile, delay, defaultSubs, (String) settings - .get("proxy_host"), Integer.parseInt((String) settings.get("proxy_port"))); + etagsFile, lastModifiedFile, lastFetchedFile, delay, defaultSubs, settings + .get("proxy_host"), Integer.parseInt(settings.get("proxy_port"))); Log log = new Log(logFile); update(master, router, published, subscriptions, log); @@ -149,7 +149,7 @@ public class Daemon { homeFile = new SecureDirectory(System.getProperty("user.dir")); } - Map defaultSettings = new HashMap(); + Map defaultSettings = new HashMap(); defaultSettings.put("proxy_host", "127.0.0.1"); defaultSettings.put("proxy_port", "4444"); defaultSettings.put("master_addressbook", "../userhosts.txt"); @@ -173,7 +173,7 @@ public class Daemon { File settingsFile = new File(homeFile, settingsLocation); - Map settings = ConfigParser.parse(settingsFile, defaultSettings); + Map settings = ConfigParser.parse(settingsFile, defaultSettings); // wait try { Thread.sleep(5*60*1000 + I2PAppContext.getGlobalContext().random().nextLong(5*60*1000)); @@ -181,7 +181,7 @@ public class Daemon { } catch (InterruptedException ie) {} while (_running) { - long delay = Long.parseLong((String) settings.get("update_delay")); + long delay = Long.parseLong(settings.get("update_delay")); if (delay < 1) { delay = 1; } diff --git a/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionIterator.java b/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionIterator.java index 6a362b847..d4c0ee98c 100644 --- a/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionIterator.java +++ b/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionIterator.java @@ -35,9 +35,9 @@ import net.i2p.data.DataHelper; // debug * * @author Ragnarok */ -class SubscriptionIterator implements Iterator { +class SubscriptionIterator implements Iterator { - private Iterator subIterator; + private Iterator subIterator; private String proxyHost; private int proxyPort; private final long delay; @@ -51,7 +51,7 @@ class SubscriptionIterator implements Iterator { * @param proxyHost proxy hostname * @param proxyPort proxt port number */ - public SubscriptionIterator(List subscriptions, long delay, String proxyHost, int proxyPort) { + public SubscriptionIterator(List subscriptions, long delay, String proxyHost, int proxyPort) { this.subIterator = subscriptions.iterator(); this.delay = delay; this.proxyHost = proxyHost; @@ -72,8 +72,8 @@ class SubscriptionIterator implements Iterator { * see java.util.Iterator#next() * @return an AddressBook (empty if the minimum delay has not been met) */ - public Object next() { - Subscription sub = (Subscription) this.subIterator.next(); + public AddressBook next() { + Subscription sub = this.subIterator.next(); if (sub.getLastFetched() + this.delay < I2PAppContext.getGlobalContext().clock().now()) { //System.err.println("Fetching addressbook from " + sub.getLocation()); return new AddressBook(sub, this.proxyHost, this.proxyPort); diff --git a/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionList.java b/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionList.java index d67cd9af5..9b64bb316 100644 --- a/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionList.java +++ b/apps/addressbook/java/src/net/i2p/addressbook/SubscriptionList.java @@ -24,7 +24,6 @@ package net.i2p.addressbook; import java.io.File; import java.io.IOException; import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -37,7 +36,7 @@ import java.util.Map; */ class SubscriptionList { - private List subscriptions; + private List subscriptions; private File etagsFile; @@ -68,7 +67,7 @@ class SubscriptionList { * @param proxyPort proxy port number */ public SubscriptionList(File locationsFile, File etagsFile, - File lastModifiedFile, File lastFetchedFile, long delay, List defaultSubs, String proxyHost, + File lastModifiedFile, File lastFetchedFile, long delay, List defaultSubs, String proxyHost, int proxyPort) { this.subscriptions = new LinkedList(); this.etagsFile = etagsFile; @@ -77,11 +76,10 @@ class SubscriptionList { this.delay = delay; this.proxyHost = proxyHost; this.proxyPort = proxyPort; - Map etags; - Map lastModified; - Map lastFetched; - String location; - List locations = ConfigParser.parseSubscriptions(locationsFile, + Map etags; + Map lastModified; + Map lastFetched; + List locations = ConfigParser.parseSubscriptions(locationsFile, defaultSubs); try { etags = ConfigParser.parse(etagsFile); @@ -98,12 +96,10 @@ class SubscriptionList { } catch (IOException exp) { lastFetched = new HashMap(); } - Iterator iter = locations.iterator(); - while (iter.hasNext()) { - location = (String) iter.next(); - this.subscriptions.add(new Subscription(location, (String) etags.get(location), - (String) lastModified.get(location), - (String) lastFetched.get(location))); + for (String location : locations) { + this.subscriptions.add(new Subscription(location, etags.get(location), + lastModified.get(location), + lastFetched.get(location))); } } @@ -125,13 +121,10 @@ class SubscriptionList { * won't be read back correctly; the '=' should be escaped. */ public void write() { - Iterator iter = this.subscriptions.iterator(); - Subscription sub; - Map etags = new HashMap(); - Map lastModified = new HashMap(); - Map lastFetched = new HashMap(); - while (iter.hasNext()) { - sub = (Subscription) iter.next(); + Map etags = new HashMap(); + Map lastModified = new HashMap(); + Map lastFetched = new HashMap(); + for (Subscription sub : this.subscriptions) { if (sub.getEtag() != null) { etags.put(sub.getLocation(), sub.getEtag()); }