/** * Merges the given peer info with the local info. A newer contribution of the same contributor * replaces the older one. Otherwise no elements are removed by this method, ie the cache and * commands are not truncated. */ private synchronized void merge(Collective peer) { synchronized (cache) { Iterator i = peer.cache.values().iterator(); while (i.hasNext()) { ContributionBox cb = (ContributionBox) i.next(); ContributionBox x = (ContributionBox) cache.get(cb.contributor.name); if (x == null || x.timeStamp < cb.timeStamp) { cache.put(cb.contributor.name, cb); } } } synchronized (commands) { Iterator i = peer.commands.entrySet().iterator(); while (i.hasNext()) { Map.Entry e = (Map.Entry) i.next(); Comparable time = (Comparable) commands.get(e.getKey()); if (time == null || time.compareTo(e.getValue()) < 0) { commands.put(e.getKey(), e.getValue()); } } } cutToSize(); }
private void writeObject(ObjectOutputStream out) throws IOException { synchronized (cache) { synchronized (commands) { out.writeObject(name); // cache and commands are never null out.writeLong(System.currentTimeMillis()); out.writeObject(myContribution); out.writeInt(cache.size()); Iterator i = cache.values().iterator(); while (i.hasNext()) out.writeObject(i.next()); out.writeInt(commands.size()); i = commands.entrySet().iterator(); while (i.hasNext()) { Map.Entry e = (Map.Entry) i.next(); long time = ((Long) e.getValue()).longValue(); out.writeObject(e.getKey()); out.writeLong(time); } } } }