Java 8 migration. Replace netty ConcurrentSet with Java KeySet. (#8745)
Motivation: The concurrent set is present in Java 8 and above so we can use it instead of own implementation. Modification: io.netty.utik.internal.ConcurrentSet replaced with ConcurrentHashMap.newKeySet(). Result: Less code to maintain.
This commit is contained in:
parent
8fdf373557
commit
45c0ea543f
@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013 The Netty Project
|
|
||||||
*
|
|
||||||
* The Netty Project licenses this file to you under the Apache License,
|
|
||||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
||||||
* with the License. You may obtain a copy of the License at:
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
* License for the specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
package io.netty.util.internal;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.AbstractSet;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated For removal in Netty 4.2. Please use {@link ConcurrentHashMap#newKeySet()} instead
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public final class ConcurrentSet<E> extends AbstractSet<E> implements Serializable {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -6761513279741915432L;
|
|
||||||
|
|
||||||
private final ConcurrentMap<E, Boolean> map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new instance which wraps the specified {@code map}.
|
|
||||||
*/
|
|
||||||
public ConcurrentSet() {
|
|
||||||
map = PlatformDependent.newConcurrentHashMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
return map.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean contains(Object o) {
|
|
||||||
return map.containsKey(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean add(E o) {
|
|
||||||
return map.putIfAbsent(o, Boolean.TRUE) == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean remove(Object o) {
|
|
||||||
return map.remove(o) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clear() {
|
|
||||||
map.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterator<E> iterator() {
|
|
||||||
return map.keySet().iterator();
|
|
||||||
}
|
|
||||||
}
|
|
@ -22,6 +22,7 @@ import java.lang.ref.WeakReference;
|
|||||||
import java.security.AccessController;
|
import java.security.AccessController;
|
||||||
import java.security.PrivilegedAction;
|
import java.security.PrivilegedAction;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import static io.netty.util.internal.SystemPropertyUtil.getInt;
|
import static io.netty.util.internal.SystemPropertyUtil.getInt;
|
||||||
@ -38,8 +39,8 @@ public final class ObjectCleaner {
|
|||||||
// Package-private for testing
|
// Package-private for testing
|
||||||
static final String CLEANER_THREAD_NAME = ObjectCleaner.class.getSimpleName() + "Thread";
|
static final String CLEANER_THREAD_NAME = ObjectCleaner.class.getSimpleName() + "Thread";
|
||||||
// This will hold a reference to the AutomaticCleanerReference which will be removed once we called cleanup()
|
// This will hold a reference to the AutomaticCleanerReference which will be removed once we called cleanup()
|
||||||
private static final Set<AutomaticCleanerReference> LIVE_SET = new ConcurrentSet<AutomaticCleanerReference>();
|
private static final Set<AutomaticCleanerReference> LIVE_SET = ConcurrentHashMap.newKeySet();
|
||||||
private static final ReferenceQueue<Object> REFERENCE_QUEUE = new ReferenceQueue<Object>();
|
private static final ReferenceQueue<Object> REFERENCE_QUEUE = new ReferenceQueue<>();
|
||||||
private static final AtomicBoolean CLEANER_RUNNING = new AtomicBoolean(false);
|
private static final AtomicBoolean CLEANER_RUNNING = new AtomicBoolean(false);
|
||||||
private static final Runnable CLEANER_TASK = new Runnable() {
|
private static final Runnable CLEANER_TASK = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -20,11 +20,11 @@ import io.netty.channel.ChannelFuture;
|
|||||||
import io.netty.channel.ChannelFutureListener;
|
import io.netty.channel.ChannelFutureListener;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.util.internal.ConcurrentSet;
|
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class allows one to ensure that at all times for every IP address there is at most one
|
* This class allows one to ensure that at all times for every IP address there is at most one
|
||||||
@ -33,17 +33,17 @@ import java.util.Set;
|
|||||||
@ChannelHandler.Sharable
|
@ChannelHandler.Sharable
|
||||||
public class UniqueIpFilter extends AbstractRemoteAddressFilter<InetSocketAddress> {
|
public class UniqueIpFilter extends AbstractRemoteAddressFilter<InetSocketAddress> {
|
||||||
|
|
||||||
private final Set<InetAddress> connected = new ConcurrentSet<InetAddress>();
|
private final Set<InetAddress> connected = ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) throws Exception {
|
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) {
|
||||||
final InetAddress remoteIp = remoteAddress.getAddress();
|
final InetAddress remoteIp = remoteAddress.getAddress();
|
||||||
if (!connected.add(remoteIp)) {
|
if (!connected.add(remoteIp)) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
|
ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
|
||||||
@Override
|
@Override
|
||||||
public void operationComplete(ChannelFuture future) throws Exception {
|
public void operationComplete(ChannelFuture future) {
|
||||||
connected.remove(remoteIp);
|
connected.remove(remoteIp);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user