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:
Dmitriy Dumanskiy 2019-01-22 11:40:40 +02:00 committed by Norman Maurer
parent 8fdf373557
commit 45c0ea543f
3 changed files with 7 additions and 75 deletions

View File

@ -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();
}
}

View File

@ -22,6 +22,7 @@ import java.lang.ref.WeakReference;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import static io.netty.util.internal.SystemPropertyUtil.getInt;
@ -38,8 +39,8 @@ public final class ObjectCleaner {
// Package-private for testing
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()
private static final Set<AutomaticCleanerReference> LIVE_SET = new ConcurrentSet<AutomaticCleanerReference>();
private static final ReferenceQueue<Object> REFERENCE_QUEUE = new ReferenceQueue<Object>();
private static final Set<AutomaticCleanerReference> LIVE_SET = ConcurrentHashMap.newKeySet();
private static final ReferenceQueue<Object> REFERENCE_QUEUE = new ReferenceQueue<>();
private static final AtomicBoolean CLEANER_RUNNING = new AtomicBoolean(false);
private static final Runnable CLEANER_TASK = new Runnable() {
@Override

View File

@ -20,11 +20,11 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.ConcurrentSet;
import java.net.InetAddress;
import java.net.InetSocketAddress;
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
@ -33,17 +33,17 @@ import java.util.Set;
@ChannelHandler.Sharable
public class UniqueIpFilter extends AbstractRemoteAddressFilter<InetSocketAddress> {
private final Set<InetAddress> connected = new ConcurrentSet<InetAddress>();
private final Set<InetAddress> connected = ConcurrentHashMap.newKeySet();
@Override
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) throws Exception {
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) {
final InetAddress remoteIp = remoteAddress.getAddress();
if (!connected.add(remoteIp)) {
return false;
} else {
ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
public void operationComplete(ChannelFuture future) {
connected.remove(remoteIp);
}
});