@Test public void testMaxOfMostFreq() throws Exception { assertEquals(0, PeerGroup.maxOfMostFreq(Collections.<Integer>emptyList())); assertEquals(0, PeerGroup.maxOfMostFreq(Arrays.asList(0, 0, 1))); assertEquals(3, PeerGroup.maxOfMostFreq(Arrays.asList(1, 3, 1, 2, 2, 3, 3))); assertEquals(0, PeerGroup.maxOfMostFreq(Arrays.asList(1, 1, 2, 2))); assertEquals(0, PeerGroup.maxOfMostFreq(Arrays.asList(-1, 1, 1, 2, 2))); assertEquals(1, PeerGroup.maxOfMostFreq(Arrays.asList(1, 1, 2, 2, 1))); assertEquals(-1, PeerGroup.maxOfMostFreq(Arrays.asList(-1, -1, 2, 2, -1))); }
static int maxOfMostFreq(List<Integer> items) { if (items.isEmpty()) return 0; items = Ordering.natural().reverse().sortedCopy(items); LinkedList<Pair> pairs = new LinkedList<>(); pairs.add(new Pair(items.get(0))); for (int item : items) { Pair pair = pairs.getLast(); if (pair.item != item) pairs.add((pair = new Pair(item))); pair.count++; } Collections.sort(pairs); final Pair firstPair = pairs.get(0); if (pairs.size() == 1) return firstPair.item; final Pair secondPair = pairs.get(1); if (firstPair.count > secondPair.count) return firstPair.item; checkState(firstPair.count == secondPair.count); return 0; }
PeerGroup implements TransactionBroadcaster { static int maxOfMostFreq(List<Integer> items) { if (items.isEmpty()) return 0; items = Ordering.natural().reverse().sortedCopy(items); LinkedList<Pair> pairs = new LinkedList<>(); pairs.add(new Pair(items.get(0))); for (int item : items) { Pair pair = pairs.getLast(); if (pair.item != item) pairs.add((pair = new Pair(item))); pair.count++; } Collections.sort(pairs); final Pair firstPair = pairs.get(0); if (pairs.size() == 1) return firstPair.item; final Pair secondPair = pairs.get(1); if (firstPair.count > secondPair.count) return firstPair.item; checkState(firstPair.count == secondPair.count); return 0; } }
PeerGroup implements TransactionBroadcaster { static int maxOfMostFreq(List<Integer> items) { if (items.isEmpty()) return 0; items = Ordering.natural().reverse().sortedCopy(items); LinkedList<Pair> pairs = new LinkedList<>(); pairs.add(new Pair(items.get(0))); for (int item : items) { Pair pair = pairs.getLast(); if (pair.item != item) pairs.add((pair = new Pair(item))); pair.count++; } Collections.sort(pairs); final Pair firstPair = pairs.get(0); if (pairs.size() == 1) return firstPair.item; final Pair secondPair = pairs.get(1); if (firstPair.count > secondPair.count) return firstPair.item; checkState(firstPair.count == secondPair.count); return 0; } PeerGroup(NetworkParameters params);  PeerGroup(Context context);  PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain);  PeerGroup(Context context, @Nullable AbstractBlockChain chain);  PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager); private  PeerGroup(Context context, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager); }
PeerGroup implements TransactionBroadcaster { static int maxOfMostFreq(List<Integer> items) { if (items.isEmpty()) return 0; items = Ordering.natural().reverse().sortedCopy(items); LinkedList<Pair> pairs = new LinkedList<>(); pairs.add(new Pair(items.get(0))); for (int item : items) { Pair pair = pairs.getLast(); if (pair.item != item) pairs.add((pair = new Pair(item))); pair.count++; } Collections.sort(pairs); final Pair firstPair = pairs.get(0); if (pairs.size() == 1) return firstPair.item; final Pair secondPair = pairs.get(1); if (firstPair.count > secondPair.count) return firstPair.item; checkState(firstPair.count == secondPair.count); return 0; } PeerGroup(NetworkParameters params);  PeerGroup(Context context);  PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain);  PeerGroup(Context context, @Nullable AbstractBlockChain chain);  PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager); private  PeerGroup(Context context, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager); void setPeerDiscoveryTimeoutMillis(long peerDiscoveryTimeoutMillis); void setMaxConnections(int maxConnections); void setDownloadTxDependencies(int depth); int getMaxConnections(); void setVersionMessage(VersionMessage ver); VersionMessage getVersionMessage(); void setUserAgent(String name, String version, @Nullable String comments); void setUserAgent(String name, String version); void addBlocksDownloadedEventListener(BlocksDownloadedEventListener listener); void addBlocksDownloadedEventListener(Executor executor, BlocksDownloadedEventListener listener); void addChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener); void addChainDownloadStartedEventListener(Executor executor, ChainDownloadStartedEventListener listener); void addConnectedEventListener(PeerConnectedEventListener listener); void addConnectedEventListener(Executor executor, PeerConnectedEventListener listener); void addDisconnectedEventListener(PeerDisconnectedEventListener listener); void addDisconnectedEventListener(Executor executor, PeerDisconnectedEventListener listener); void addDiscoveredEventListener(PeerDiscoveredEventListener listener); void addDiscoveredEventListener(Executor executor, PeerDiscoveredEventListener listener); void addGetDataEventListener(GetDataEventListener listener); void addGetDataEventListener(final Executor executor, final GetDataEventListener listener); void addOnTransactionBroadcastListener(OnTransactionBroadcastListener listener); void addOnTransactionBroadcastListener(Executor executor, OnTransactionBroadcastListener listener); void addPreMessageReceivedEventListener(PreMessageReceivedEventListener listener); void addPreMessageReceivedEventListener(Executor executor, PreMessageReceivedEventListener listener); boolean removeBlocksDownloadedEventListener(BlocksDownloadedEventListener listener); boolean removeChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener); boolean removeConnectedEventListener(PeerConnectedEventListener listener); boolean removeDisconnectedEventListener(PeerDisconnectedEventListener listener); boolean removeDiscoveredEventListener(PeerDiscoveredEventListener listener); boolean removeGetDataEventListener(GetDataEventListener listener); boolean removeOnTransactionBroadcastListener(OnTransactionBroadcastListener listener); boolean removePreMessageReceivedEventListener(PreMessageReceivedEventListener listener); List<Peer> getConnectedPeers(); List<Peer> getPendingPeers(); void addAddress(PeerAddress peerAddress); void addAddress(PeerAddress peerAddress, int priority); void setRequiredServices(long requiredServices); void addAddress(InetAddress address); void addAddress(InetAddress address, int priority); void addPeerDiscovery(PeerDiscovery peerDiscovery); ListenableFuture startAsync(); void start(); ListenableFuture stopAsync(); void stop(); void dropAllPeers(); void addWallet(Wallet wallet); ListenableFuture<BloomFilter> addPeerFilterProvider(PeerFilterProvider provider); void removePeerFilterProvider(PeerFilterProvider provider); void removeWallet(Wallet wallet); ListenableFuture<BloomFilter> recalculateFastCatchupAndFilter(final FilterRecalculateMode mode); void setBloomFilterFalsePositiveRate(double bloomFilterFPRate); int numConnectedPeers(); @Nullable Peer connectTo(InetSocketAddress address); @Nullable Peer connectToLocalHost(); void setConnectTimeoutMillis(int connectTimeoutMillis); void startBlockChainDownload(PeerDataEventListener listener); void downloadBlockChain(); @Deprecated @Nullable TxConfidenceTable getMemoryPool(); void setFastCatchupTimeSecs(long secondsSinceEpoch); long getFastCatchupTimeSecs(); void setStallThreshold(int periodSecs, int bytesPerSecond); ListenableFuture<List<Peer>> waitForPeers(final int numPeers); ListenableFuture<List<Peer>> waitForPeersOfVersion(final int numPeers, final long protocolVersion); List<Peer> findPeersOfAtLeastVersion(long protocolVersion); ListenableFuture<List<Peer>> waitForPeersWithServiceMask(final int numPeers, final int mask); List<Peer> findPeersWithServiceMask(int mask); int getMinBroadcastConnections(); void setMinBroadcastConnections(int value); @Override TransactionBroadcast broadcastTransaction(final Transaction tx); TransactionBroadcast broadcastTransaction(final Transaction tx, final int minConnections,
                                                     final boolean dropPeersAfterBroadcast); long getPingIntervalMsec(); void setPingIntervalMsec(long pingIntervalMsec); void setMinRequiredProtocolVersion(int minRequiredProtocolVersion); int getMinRequiredProtocolVersion(); int getMostCommonChainHeight(); static int getMostCommonChainHeight(final List<Peer> peers); Peer getDownloadPeer(); int getMaxPeersToDiscoverCount(); void setMaxPeersToDiscoverCount(int maxPeersToDiscoverCount); boolean getUseLocalhostPeerWhenPossible(); void setUseLocalhostPeerWhenPossible(boolean useLocalhostPeerWhenPossible); boolean isRunning(); void setBloomFilteringEnabled(boolean bloomFilteringEnabled); boolean isBloomFilteringEnabled(); }
PeerGroup implements TransactionBroadcaster { static int maxOfMostFreq(List<Integer> items) { if (items.isEmpty()) return 0; items = Ordering.natural().reverse().sortedCopy(items); LinkedList<Pair> pairs = new LinkedList<>(); pairs.add(new Pair(items.get(0))); for (int item : items) { Pair pair = pairs.getLast(); if (pair.item != item) pairs.add((pair = new Pair(item))); pair.count++; } Collections.sort(pairs); final Pair firstPair = pairs.get(0); if (pairs.size() == 1) return firstPair.item; final Pair secondPair = pairs.get(1); if (firstPair.count > secondPair.count) return firstPair.item; checkState(firstPair.count == secondPair.count); return 0; } PeerGroup(NetworkParameters params);  PeerGroup(Context context);  PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain);  PeerGroup(Context context, @Nullable AbstractBlockChain chain);  PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager); private  PeerGroup(Context context, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager); void setPeerDiscoveryTimeoutMillis(long peerDiscoveryTimeoutMillis); void setMaxConnections(int maxConnections); void setDownloadTxDependencies(int depth); int getMaxConnections(); void setVersionMessage(VersionMessage ver); VersionMessage getVersionMessage(); void setUserAgent(String name, String version, @Nullable String comments); void setUserAgent(String name, String version); void addBlocksDownloadedEventListener(BlocksDownloadedEventListener listener); void addBlocksDownloadedEventListener(Executor executor, BlocksDownloadedEventListener listener); void addChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener); void addChainDownloadStartedEventListener(Executor executor, ChainDownloadStartedEventListener listener); void addConnectedEventListener(PeerConnectedEventListener listener); void addConnectedEventListener(Executor executor, PeerConnectedEventListener listener); void addDisconnectedEventListener(PeerDisconnectedEventListener listener); void addDisconnectedEventListener(Executor executor, PeerDisconnectedEventListener listener); void addDiscoveredEventListener(PeerDiscoveredEventListener listener); void addDiscoveredEventListener(Executor executor, PeerDiscoveredEventListener listener); void addGetDataEventListener(GetDataEventListener listener); void addGetDataEventListener(final Executor executor, final GetDataEventListener listener); void addOnTransactionBroadcastListener(OnTransactionBroadcastListener listener); void addOnTransactionBroadcastListener(Executor executor, OnTransactionBroadcastListener listener); void addPreMessageReceivedEventListener(PreMessageReceivedEventListener listener); void addPreMessageReceivedEventListener(Executor executor, PreMessageReceivedEventListener listener); boolean removeBlocksDownloadedEventListener(BlocksDownloadedEventListener listener); boolean removeChainDownloadStartedEventListener(ChainDownloadStartedEventListener listener); boolean removeConnectedEventListener(PeerConnectedEventListener listener); boolean removeDisconnectedEventListener(PeerDisconnectedEventListener listener); boolean removeDiscoveredEventListener(PeerDiscoveredEventListener listener); boolean removeGetDataEventListener(GetDataEventListener listener); boolean removeOnTransactionBroadcastListener(OnTransactionBroadcastListener listener); boolean removePreMessageReceivedEventListener(PreMessageReceivedEventListener listener); List<Peer> getConnectedPeers(); List<Peer> getPendingPeers(); void addAddress(PeerAddress peerAddress); void addAddress(PeerAddress peerAddress, int priority); void setRequiredServices(long requiredServices); void addAddress(InetAddress address); void addAddress(InetAddress address, int priority); void addPeerDiscovery(PeerDiscovery peerDiscovery); ListenableFuture startAsync(); void start(); ListenableFuture stopAsync(); void stop(); void dropAllPeers(); void addWallet(Wallet wallet); ListenableFuture<BloomFilter> addPeerFilterProvider(PeerFilterProvider provider); void removePeerFilterProvider(PeerFilterProvider provider); void removeWallet(Wallet wallet); ListenableFuture<BloomFilter> recalculateFastCatchupAndFilter(final FilterRecalculateMode mode); void setBloomFilterFalsePositiveRate(double bloomFilterFPRate); int numConnectedPeers(); @Nullable Peer connectTo(InetSocketAddress address); @Nullable Peer connectToLocalHost(); void setConnectTimeoutMillis(int connectTimeoutMillis); void startBlockChainDownload(PeerDataEventListener listener); void downloadBlockChain(); @Deprecated @Nullable TxConfidenceTable getMemoryPool(); void setFastCatchupTimeSecs(long secondsSinceEpoch); long getFastCatchupTimeSecs(); void setStallThreshold(int periodSecs, int bytesPerSecond); ListenableFuture<List<Peer>> waitForPeers(final int numPeers); ListenableFuture<List<Peer>> waitForPeersOfVersion(final int numPeers, final long protocolVersion); List<Peer> findPeersOfAtLeastVersion(long protocolVersion); ListenableFuture<List<Peer>> waitForPeersWithServiceMask(final int numPeers, final int mask); List<Peer> findPeersWithServiceMask(int mask); int getMinBroadcastConnections(); void setMinBroadcastConnections(int value); @Override TransactionBroadcast broadcastTransaction(final Transaction tx); TransactionBroadcast broadcastTransaction(final Transaction tx, final int minConnections,
                                                     final boolean dropPeersAfterBroadcast); long getPingIntervalMsec(); void setPingIntervalMsec(long pingIntervalMsec); void setMinRequiredProtocolVersion(int minRequiredProtocolVersion); int getMinRequiredProtocolVersion(); int getMostCommonChainHeight(); static int getMostCommonChainHeight(final List<Peer> peers); Peer getDownloadPeer(); int getMaxPeersToDiscoverCount(); void setMaxPeersToDiscoverCount(int maxPeersToDiscoverCount); boolean getUseLocalhostPeerWhenPossible(); void setUseLocalhostPeerWhenPossible(boolean useLocalhostPeerWhenPossible); boolean isRunning(); void setBloomFilteringEnabled(boolean bloomFilteringEnabled); boolean isBloomFilteringEnabled(); static final int DEFAULT_CONNECTIONS; static final long DEFAULT_PING_INTERVAL_MSEC; static final double DEFAULT_BLOOM_FILTER_FP_RATE; static final double MAX_FP_RATE_INCREASE; static final int DEFAULT_CONNECT_TIMEOUT_MILLIS; }