* Beautify PetNameDB API.

* Start of syndie auto address export.
This commit is contained in:
ragnarok
2005-10-09 03:32:34 +00:00
committed by zzz
parent ad574c8504
commit e7ad516685
13 changed files with 100 additions and 78 deletions

View File

@ -16,14 +16,20 @@ public class PetNameDB {
_names = Collections.synchronizedMap(new HashMap());
}
public PetName get(String name) { return (PetName)_names.get(name); }
public boolean exists(String name) { return _names.containsKey(name); }
public void set(String name, PetName pn) { _names.put(name, pn); }
public void remove(String name) { _names.remove(name); }
public PetName getName(String name) { return (PetName)_names.get(name); }
public void add(PetName pn) { _names.put(pn.getName(), pn); }
public void clear() { _names.clear(); }
public boolean contains(PetName pn) { return _names.containsValue(pn); }
public boolean containsName(String name) { return _names.containsKey(name); }
public boolean isEmpty() { return _names.isEmpty(); }
public Iterator iterator() { return new LinkedList(_names.values()).iterator(); }
public void remove(PetName pn) { _names.values().remove(pn); }
public void removeName(String name) { _names.remove(name); }
public int size() { return _names.size(); }
public Set getNames() { return new HashSet(_names.keySet()); }
public List getGroups() {
List rv = new ArrayList();
for (Iterator iter = new HashSet(_names.values()).iterator(); iter.hasNext(); ) {
for (Iterator iter = iterator(); iter.hasNext(); ) {
PetName name = (PetName)iter.next();
for (int i = 0; i < name.getGroupCount(); i++)
if (!rv.contains(name.getGroup(i)))
@ -32,13 +38,13 @@ public class PetNameDB {
return rv;
}
public String getNameByLocation(String location) {
public PetName getLocation(String location) {
if (location == null) return null;
synchronized (_names) {
for (Iterator iter = _names.values().iterator(); iter.hasNext(); ) {
for (Iterator iter = iterator(); iter.hasNext(); ) {
PetName name = (PetName)iter.next();
if ( (name.getLocation() != null) && (name.getLocation().trim().equals(location.trim())) )
return name.getName();
return name;
}
}
return null;
@ -55,7 +61,7 @@ public class PetNameDB {
while ( (line = in.readLine()) != null) {
PetName name = new PetName(line);
if (name.getName() != null)
_names.put(name.getName(), name);
add(name);
}
} finally {
in.close();
@ -66,8 +72,8 @@ public class PetNameDB {
Writer out = null;
try {
out = new OutputStreamWriter(new FileOutputStream(location), "UTF-8");
for (Iterator names = getNames().iterator(); names.hasNext(); ) {
PetName name = get((String)names.next());
for (Iterator iter = iterator(); iter.hasNext(); ) {
PetName name = (PetName)iter.next();
if (name != null)
out.write(name.toString() + "\n");
}

View File

@ -51,7 +51,7 @@ public class PetNameNamingService extends NamingService {
}
public Destination lookup(String hostname) {
PetName name = _petnameDb.get(hostname);
PetName name = _petnameDb.getName(hostname);
if (name != null && name.getNetwork().equalsIgnoreCase("i2p")) {
return lookupBase64(name.getLocation());
} else {
@ -60,6 +60,6 @@ public class PetNameNamingService extends NamingService {
}
public String reverseLookup(Destination dest) {
return _petnameDb.getNameByLocation(dest.toBase64());
return _petnameDb.getLocation(dest.toBase64()).getName();
}
}