Merge the pull request #432 manually
- Add UniqueNameTest - Add JavaDoc for UniqueName
This commit is contained in:
parent
9c17304755
commit
aef7a14852
@ -18,6 +18,9 @@ package io.netty.util;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Defines a name that must be unique in the map that is provided during construction.
|
||||
*/
|
||||
public class UniqueName implements Comparable<UniqueName> {
|
||||
|
||||
private static final AtomicInteger nextId = new AtomicInteger();
|
||||
@ -25,6 +28,13 @@ public class UniqueName implements Comparable<UniqueName> {
|
||||
private final int id;
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Constructs a new {@link UniqueName}
|
||||
*
|
||||
* @param map the map of names to compare with
|
||||
* @param name the name of this {@link UniqueName}
|
||||
* @param args the arguments to process
|
||||
*/
|
||||
public UniqueName(ConcurrentMap<String, Boolean> map, String name, Object... args) {
|
||||
if (map == null) {
|
||||
throw new NullPointerException("map");
|
||||
@ -37,7 +47,7 @@ public class UniqueName implements Comparable<UniqueName> {
|
||||
}
|
||||
|
||||
if (map.putIfAbsent(name, Boolean.TRUE) != null) {
|
||||
throw new IllegalArgumentException(String.format("'%s' already in use", name));
|
||||
throw new IllegalArgumentException(String.format("'%s' is already in use", name));
|
||||
}
|
||||
|
||||
id = nextId.incrementAndGet();
|
||||
@ -45,16 +55,29 @@ public class UniqueName implements Comparable<UniqueName> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given arguments. This method does not do anything on its own, but must be
|
||||
* overridden by its subclasses.
|
||||
*
|
||||
* @param args arguments to validate
|
||||
*/
|
||||
protected void validateArgs(Object... args) {
|
||||
// Subclasses will override.
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this {@link UniqueName}'s name
|
||||
*
|
||||
* @return the name
|
||||
*/
|
||||
public final String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this {@link UniqueName}'s ID
|
||||
*
|
||||
* @return the id
|
||||
*/
|
||||
public final int id() {
|
||||
return id;
|
||||
}
|
||||
@ -70,19 +93,19 @@ public class UniqueName implements Comparable<UniqueName> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(UniqueName o) {
|
||||
if (this == o) {
|
||||
public int compareTo(UniqueName other) {
|
||||
if (this == other) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = name.compareTo(o.name);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
int returnCode = name.compareTo(other.name);
|
||||
if (returnCode != 0) {
|
||||
return returnCode;
|
||||
}
|
||||
|
||||
if (id < o.id) {
|
||||
if (id < other.id) {
|
||||
return -1;
|
||||
} else if (id > o.id) {
|
||||
} else if (id > other.id) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
|
87
common/src/test/java/io/netty/util/UniqueNameTest.java
Normal file
87
common/src/test/java/io/netty/util/UniqueNameTest.java
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2012 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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UniqueNameTest {
|
||||
|
||||
/**
|
||||
* A {@link ConcurrentHashMap} of registered names.
|
||||
* This is set up before each test
|
||||
*/
|
||||
private ConcurrentHashMap<String, Boolean> names;
|
||||
|
||||
/**
|
||||
* Registers a {@link UniqueName}
|
||||
*
|
||||
* @param name the name being registered
|
||||
* @return the unique name
|
||||
*/
|
||||
public UniqueName registerName(String name) {
|
||||
return new UniqueName(names, name);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initializeTest() {
|
||||
this.names = new ConcurrentHashMap<String, Boolean>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisteringName() {
|
||||
registerName("Abcedrian");
|
||||
|
||||
assertTrue(this.names.get("Abcedrian"));
|
||||
assertTrue(this.names.get("Hellyes") == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNameUniqueness() {
|
||||
registerName("Leroy");
|
||||
boolean failed = false;
|
||||
try {
|
||||
registerName("Leroy");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
failed = true;
|
||||
}
|
||||
assertTrue(failed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIDUniqueness() {
|
||||
UniqueName one = registerName("one");
|
||||
UniqueName two = registerName("two");
|
||||
assertNotSame(one.id(), two.id());
|
||||
|
||||
ArrayList<UniqueName> nameList = new ArrayList<UniqueName>();
|
||||
|
||||
for (int index = 0; index < 2500; index++) {
|
||||
UniqueName currentName = registerName("test" + index);
|
||||
nameList.add(currentName);
|
||||
for (UniqueName otherName : nameList) {
|
||||
if (!currentName.name().equals(otherName.name())) {
|
||||
assertNotSame(currentName.id(), otherName.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user