forked from I2P_Developers/i2p.i2p
Cleanups: Collections.singleton(), COWAS - includes ticket #388
This commit is contained in:
@ -43,13 +43,13 @@ import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.I2PException;
|
||||
@ -99,7 +99,7 @@ public class I2PTunnel implements Logging, EventDispatcher {
|
||||
private final List tasks = new ArrayList();
|
||||
private int next_task_id = 1;
|
||||
|
||||
private final Set listeners = new HashSet();
|
||||
private final Set listeners = new CopyOnWriteArraySet();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
new I2PTunnel(args);
|
||||
@ -1626,16 +1626,12 @@ public class I2PTunnel implements Logging, EventDispatcher {
|
||||
|
||||
public void addConnectionEventListener(ConnectionEventListener lsnr) {
|
||||
if (lsnr == null) return;
|
||||
synchronized (listeners) {
|
||||
listeners.add(lsnr);
|
||||
}
|
||||
listeners.add(lsnr);
|
||||
}
|
||||
|
||||
public void removeConnectionEventListener(ConnectionEventListener lsnr) {
|
||||
if (lsnr == null) return;
|
||||
synchronized (listeners) {
|
||||
listeners.remove(lsnr);
|
||||
}
|
||||
listeners.remove(lsnr);
|
||||
}
|
||||
|
||||
private String getPrefix() { return "[" + _tunnelId + "]: "; }
|
||||
@ -1649,12 +1645,10 @@ public class I2PTunnel implements Logging, EventDispatcher {
|
||||
*/
|
||||
void routerDisconnected() {
|
||||
_log.error(getPrefix() + "Router disconnected - firing notification events");
|
||||
synchronized (listeners) {
|
||||
for (Iterator iter = listeners.iterator(); iter.hasNext();) {
|
||||
ConnectionEventListener lsnr = (ConnectionEventListener) iter.next();
|
||||
if (lsnr != null) lsnr.routerDisconnected();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -310,7 +310,7 @@ public class TunnelControllerGroup {
|
||||
synchronized (_sessions) {
|
||||
Set<TunnelController> owners = _sessions.get(session);
|
||||
if (owners == null) {
|
||||
owners = new HashSet(1);
|
||||
owners = new HashSet(2);
|
||||
_sessions.put(session, owners);
|
||||
}
|
||||
owners.add(controller);
|
||||
|
@ -2,13 +2,13 @@ package net.i2p.client.streaming;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.I2PSession;
|
||||
import net.i2p.client.I2PSessionException;
|
||||
import net.i2p.client.I2PSessionListener;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.util.ConcurrentHashSet;
|
||||
|
||||
/**
|
||||
* Receive raw information from the I2PSession and turn it into
|
||||
@ -24,7 +24,7 @@ class MessageHandler implements I2PSessionListener {
|
||||
public MessageHandler(I2PAppContext ctx, ConnectionManager mgr) {
|
||||
_manager = mgr;
|
||||
_context = ctx;
|
||||
_listeners = new ConcurrentHashSet(1);
|
||||
_listeners = new CopyOnWriteArraySet();
|
||||
_log = ctx.logManager().getLog(MessageHandler.class);
|
||||
_context.statManager().createRateStat("stream.packetReceiveFailure", "When do we fail to decrypt or otherwise receive a packet sent to us?", "Stream", new long[] { 60*60*1000, 24*60*60*1000 });
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ package net.i2p.crypto;
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -527,8 +528,6 @@ public class ElGamalAESEngine {
|
||||
return aesEncr;
|
||||
}
|
||||
|
||||
private final static Set EMPTY_SET = new HashSet();
|
||||
|
||||
/**
|
||||
* For both scenarios, this method encrypts the AES area using the given key, iv
|
||||
* and making sure the resulting data is at least as long as the paddedSize and
|
||||
@ -552,7 +551,7 @@ public class ElGamalAESEngine {
|
||||
long paddedSize, int prefixBytes) {
|
||||
//_log.debug("iv for encryption: " + DataHelper.toString(iv, 16));
|
||||
//_log.debug("Encrypting AES");
|
||||
if (tagsForDelivery == null) tagsForDelivery = EMPTY_SET;
|
||||
if (tagsForDelivery == null) tagsForDelivery = Collections.EMPTY_SET;
|
||||
int size = 2 // sizeof(tags)
|
||||
+ tagsForDelivery.size()
|
||||
+ SessionTag.BYTE_LENGTH*tagsForDelivery.size()
|
||||
|
@ -1,8 +1,8 @@
|
||||
package net.i2p.util;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.time.Timestamper;
|
||||
@ -19,19 +19,19 @@ import net.i2p.time.Timestamper;
|
||||
*
|
||||
*/
|
||||
public class Clock implements Timestamper.UpdateListener {
|
||||
protected I2PAppContext _context;
|
||||
private Timestamper _timestamper;
|
||||
protected long _startedOn;
|
||||
protected final I2PAppContext _context;
|
||||
private final Timestamper _timestamper;
|
||||
protected final long _startedOn;
|
||||
protected boolean _statCreated;
|
||||
protected volatile long _offset;
|
||||
protected boolean _alreadyChanged;
|
||||
private final Set _listeners;
|
||||
|
||||
public Clock(I2PAppContext context) {
|
||||
_context = context;
|
||||
_offset = 0;
|
||||
_alreadyChanged = false;
|
||||
_listeners = new HashSet(1);
|
||||
_listeners = new CopyOnWriteArraySet();
|
||||
_timestamper = new Timestamper(context, this);
|
||||
_startedOn = System.currentTimeMillis();
|
||||
_statCreated = false;
|
||||
}
|
||||
public static Clock getInstance() {
|
||||
return I2PAppContext.getGlobalContext().clock();
|
||||
@ -41,10 +41,6 @@ public class Clock implements Timestamper.UpdateListener {
|
||||
|
||||
/** we fetch it on demand to avoid circular dependencies (logging uses the clock) */
|
||||
protected Log getLog() { return _context.logManager().getLog(Clock.class); }
|
||||
|
||||
protected volatile long _offset;
|
||||
protected boolean _alreadyChanged;
|
||||
private final Set _listeners;
|
||||
|
||||
/** if the clock is skewed by 3+ days, fuck 'em */
|
||||
public final static long MAX_OFFSET = 3 * 24 * 60 * 60 * 1000;
|
||||
@ -136,24 +132,18 @@ public class Clock implements Timestamper.UpdateListener {
|
||||
}
|
||||
|
||||
public void addUpdateListener(ClockUpdateListener lsnr) {
|
||||
synchronized (_listeners) {
|
||||
_listeners.add(lsnr);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeUpdateListener(ClockUpdateListener lsnr) {
|
||||
synchronized (_listeners) {
|
||||
_listeners.remove(lsnr);
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireOffsetChanged(long delta) {
|
||||
synchronized (_listeners) {
|
||||
for (Iterator iter = _listeners.iterator(); iter.hasNext();) {
|
||||
ClockUpdateListener lsnr = (ClockUpdateListener) iter.next();
|
||||
lsnr.offsetChanged(delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static interface ClockUpdateListener {
|
||||
|
@ -10,9 +10,9 @@ package net.i2p.util;
|
||||
*/
|
||||
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/**
|
||||
* Like I2PThread but with per-thread OOM listeners,
|
||||
@ -22,7 +22,7 @@ import java.util.Set;
|
||||
*/
|
||||
public class I2PAppThread extends I2PThread {
|
||||
|
||||
private Set _threadListeners = new HashSet(0);
|
||||
private final Set _threadListeners = new CopyOnWriteArraySet();
|
||||
|
||||
public I2PAppThread() {
|
||||
super();
|
||||
|
@ -10,9 +10,9 @@ package net.i2p.util;
|
||||
*/
|
||||
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/**
|
||||
* In case its useful later...
|
||||
@ -21,7 +21,7 @@ import java.util.Set;
|
||||
*/
|
||||
public class I2PThread extends Thread {
|
||||
private static volatile Log _log;
|
||||
private static Set _listeners = new HashSet(4);
|
||||
private static final Set _listeners = new CopyOnWriteArraySet();
|
||||
private String _name;
|
||||
private Exception _createdBy;
|
||||
|
||||
|
@ -225,7 +225,7 @@ public class OutNetMessage {
|
||||
|
||||
public void transportFailed(String transportStyle) {
|
||||
if (_failedTransports == null)
|
||||
_failedTransports = new HashSet(1);
|
||||
_failedTransports = new HashSet(2);
|
||||
_failedTransports.add(transportStyle);
|
||||
}
|
||||
/** not thread safe - dont fail transports and iterate over this at the same time */
|
||||
|
@ -34,7 +34,7 @@ public class RouterClock extends Clock {
|
||||
private long _lastChanged;
|
||||
private int _lastStratum;
|
||||
|
||||
RouterContext _contextRC; // LINT field hides another field
|
||||
private final RouterContext _contextRC;
|
||||
|
||||
public RouterClock(RouterContext context) {
|
||||
super(context);
|
||||
|
@ -156,7 +156,7 @@ public class Shitlist {
|
||||
e.causeCode = reasonCode;
|
||||
e.transports = null;
|
||||
if (transport != null) {
|
||||
e.transports = new ConcurrentHashSet(1);
|
||||
e.transports = new ConcurrentHashSet(2);
|
||||
e.transports.add(transport);
|
||||
}
|
||||
|
||||
|
@ -74,8 +74,9 @@ class FloodfillPeerSelector extends PeerSelector {
|
||||
*/
|
||||
List<Hash> selectNearestExplicitThin(Hash key, int maxNumRouters, Set<Hash> peersToIgnore, KBucketSet kbuckets, boolean preferConnected) {
|
||||
if (peersToIgnore == null)
|
||||
peersToIgnore = new HashSet(1);
|
||||
peersToIgnore.add(_context.routerHash());
|
||||
peersToIgnore = Collections.singleton(_context.routerHash());
|
||||
else
|
||||
peersToIgnore.add(_context.routerHash());
|
||||
// TODO this is very slow
|
||||
FloodfillSelectionCollector matches = new FloodfillSelectionCollector(key, peersToIgnore, maxNumRouters);
|
||||
if (kbuckets == null) return new ArrayList();
|
||||
@ -94,8 +95,7 @@ class FloodfillPeerSelector extends PeerSelector {
|
||||
* List is not sorted and not shuffled.
|
||||
*/
|
||||
List<Hash> selectFloodfillParticipants(KBucketSet kbuckets) {
|
||||
Set<Hash> ignore = new HashSet(1);
|
||||
ignore.add(_context.routerHash());
|
||||
Set<Hash> ignore = Collections.singleton(_context.routerHash());
|
||||
return selectFloodfillParticipants(ignore, kbuckets);
|
||||
}
|
||||
|
||||
@ -132,8 +132,7 @@ class FloodfillPeerSelector extends PeerSelector {
|
||||
* Group 3: All others
|
||||
*/
|
||||
List<Hash> selectFloodfillParticipants(Hash key, int maxNumRouters, KBucketSet kbuckets) {
|
||||
Set<Hash> ignore = new HashSet(1);
|
||||
ignore.add(_context.routerHash());
|
||||
Set<Hash> ignore = Collections.singleton(_context.routerHash());
|
||||
return selectFloodfillParticipants(key, maxNumRouters, ignore, kbuckets);
|
||||
}
|
||||
|
||||
@ -152,8 +151,7 @@ class FloodfillPeerSelector extends PeerSelector {
|
||||
*/
|
||||
List<Hash> selectFloodfillParticipants(Hash key, int howMany, Set<Hash> toIgnore, KBucketSet kbuckets) {
|
||||
if (toIgnore == null) {
|
||||
toIgnore = new HashSet(1);
|
||||
toIgnore.add(_context.routerHash());
|
||||
toIgnore = Collections.singleton(_context.routerHash());
|
||||
} else if (!toIgnore.contains(_context.routerHash())) {
|
||||
// copy the Set so we don't confuse StoreJob
|
||||
toIgnore = new HashSet(toIgnore);
|
||||
|
@ -31,8 +31,8 @@ import net.i2p.util.Log;
|
||||
* Mostly unused, see overrides in FloodfillPeerSelector
|
||||
*/
|
||||
class PeerSelector {
|
||||
protected Log _log;
|
||||
protected RouterContext _context;
|
||||
protected final Log _log;
|
||||
protected final RouterContext _context;
|
||||
|
||||
public PeerSelector(RouterContext ctx) {
|
||||
_context = ctx;
|
||||
|
@ -9,7 +9,7 @@ package net.i2p.router.networkdb.kademlia;
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -800,8 +800,7 @@ class SearchJob extends JobImpl {
|
||||
if (rv) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug(getJobId() + ": Queueing up for next time: " + peer);
|
||||
Set s = new HashSet(1);
|
||||
s.add(peer);
|
||||
Set<Hash> s = Collections.singleton(peer);
|
||||
_facade.queueForExploration(s);
|
||||
}
|
||||
return rv;
|
||||
|
@ -11,6 +11,7 @@ package net.i2p.router.peermanager;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -115,8 +116,7 @@ class PeerManager {
|
||||
*/
|
||||
List<Hash> selectPeers(PeerSelectionCriteria criteria) {
|
||||
Set<Hash> peers = new HashSet(criteria.getMinimumRequired());
|
||||
Set<Hash> exclude = new HashSet(1);
|
||||
exclude.add(_context.routerHash());
|
||||
Set<Hash> exclude = Collections.singleton(_context.routerHash());
|
||||
switch (criteria.getPurpose()) {
|
||||
case PeerSelectionCriteria.PURPOSE_TEST:
|
||||
// for now, the peers we test will be the reliable ones
|
||||
|
@ -1,6 +1,6 @@
|
||||
package net.i2p.router.tunnel.pool;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import net.i2p.crypto.SessionKeyManager;
|
||||
@ -144,8 +144,7 @@ class TestJob extends JobImpl {
|
||||
scheduleRetest();
|
||||
return;
|
||||
}
|
||||
Set encryptTags = new HashSet(1);
|
||||
encryptTags.add(encryptTag);
|
||||
Set<SessionTag> encryptTags = Collections.singleton(encryptTag);
|
||||
// Register the single tag with the appropriate SKM
|
||||
if (_cfg.isInbound() && !_pool.getSettings().isExploratory()) {
|
||||
SessionKeyManager skm = getContext().clientManager().getClientSessionKeyManager(_pool.getSettings().getDestination());
|
||||
|
Reference in New Issue
Block a user