diff --git a/source/de/anomic/plasma/plasmaWebStructure.java b/source/de/anomic/plasma/plasmaWebStructure.java index 59729de1b..ab8a86be9 100644 --- a/source/de/anomic/plasma/plasmaWebStructure.java +++ b/source/de/anomic/plasma/plasmaWebStructure.java @@ -29,7 +29,6 @@ package de.anomic.plasma; import java.io.File; import java.io.IOException; -import java.util.ConcurrentModificationException; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -74,11 +73,8 @@ public class plasmaWebStructure { if (this.structure.size() > maxhosts) { // fill a set with last-modified - dates of the structure TreeSet delset = new TreeSet(); - Map.Entry entry; - Iterator> i = this.structure.entrySet().iterator(); String key, value; - while (i.hasNext()) { - entry = i.next(); + for (Map.Entry entry : this.structure.entrySet()) { key = entry.getKey(); value = entry.getValue(); delset.add(value.substring(0, 8) + key); @@ -196,11 +192,8 @@ public class plasmaWebStructure { private static String map2refstr(Map map) { StringBuffer s = new StringBuffer(map.size() * 10); s.append(serverDate.formatShortDay(new Date())); - Iterator> i = map.entrySet().iterator(); - Map.Entry entry; String h; - while (i.hasNext()) { - entry = i.next(); + for (Map.Entry entry : map.entrySet()) { s.append(entry.getKey()); h = Integer.toHexString(entry.getValue().intValue()); if (h.length() == 0) { @@ -223,50 +216,45 @@ public class plasmaWebStructure { public Map references(String domhash) { // returns a map with a domhash(String):refcount(Integer) relation assert domhash.length() == 6; - SortedMap tailMap = structure.tailMap(domhash); - if ((tailMap == null) || (tailMap.size() == 0)) return new HashMap(); - String key = tailMap.firstKey(); - if (key.startsWith(domhash)) { - return refstr2map(tailMap.get(key)); - } else { - return new HashMap(); + synchronized(structure) { + SortedMap tailMap = structure.tailMap(domhash); + if ((tailMap == null) || (tailMap.isEmpty())) return new HashMap(); + String key = tailMap.firstKey(); + if (key.startsWith(domhash)) { + return refstr2map(tailMap.get(key)); + } else { + return new HashMap(); + } } } public int referencesCount(String domhash) { // returns the number of domains that are referenced by this domhash assert domhash.length() == 6 : "domhash = " + domhash; - try { + synchronized(structure) { SortedMap tailMap = structure.tailMap(domhash); - if ((tailMap == null) || (tailMap.size() == 0)) return 0; + if ((tailMap == null) || (tailMap.isEmpty())) return 0; String key = tailMap.firstKey(); if (key.startsWith(domhash)) { return refstr2count(tailMap.get(key)); } else { return 0; } - } catch (ConcurrentModificationException e) { - return 0; } } public String resolveDomHash2DomString(String domhash) { // returns the domain as string, null if unknown assert domhash.length() == 6; - try { + synchronized(structure) { SortedMap tailMap = structure.tailMap(domhash); - if ((tailMap == null) || (tailMap.size() == 0)) return null; + if ((tailMap == null) || (tailMap.isEmpty())) return null; String key = tailMap.firstKey(); if (key.startsWith(domhash)) { return key.substring(7); } else { return null; } - } catch (ConcurrentModificationException e) { - // we don't want to implement a synchronization here, - // because this is 'only' used for a graphics application - // just return null - return null; } } @@ -294,10 +282,7 @@ public class plasmaWebStructure { // shrink the references: the entry with the smallest number of references is removed int minrefcount = Integer.MAX_VALUE; String minrefkey = null; - Iterator> i = refs.entrySet().iterator(); - Map.Entry entry; - findloop: while (i.hasNext()) { - entry = i.next(); + findloop: for (Map.Entry entry : refs.entrySet()) { if (entry.getValue().intValue() < minrefcount) { minrefcount = entry.getValue().intValue(); minrefkey = entry.getKey(); @@ -312,12 +297,16 @@ public class plasmaWebStructure { } // store the map back to the structure - structure.put(domhash + "," + url.getHost(), map2refstr(refs)); + synchronized(structure) { + structure.put(domhash + "," + url.getHost(), map2refstr(refs)); + } } public void saveWebStructure() { try { - serverFileUtils.saveMap(this.structureFile, this.structure, "Web Structure Syntax: ',' to {}*"); + synchronized(structure) { + serverFileUtils.saveMap(this.structureFile, this.structure, "Web Structure Syntax: ',' to {}*"); + } } catch (IOException e) { e.printStackTrace(); } @@ -325,16 +314,15 @@ public class plasmaWebStructure { public String hostWithMaxReferences() { // find domain with most references - Iterator> i = structure.entrySet().iterator(); - int refsize, maxref = 0; String maxhost = null; - Map.Entry entry; - while (i.hasNext()) { - entry = i.next(); - refsize = entry.getValue().length(); - if (refsize > maxref) { - maxref = refsize; - maxhost = entry.getKey().substring(7); + int refsize, maxref = 0; + synchronized(structure) { + for (Map.Entry entry : structure.entrySet()) { + refsize = entry.getValue().length(); + if (refsize > maxref) { + maxref = refsize; + maxhost = entry.getKey().substring(7); + } } } return maxhost;