From aef7a14852f6a7b6aa266ffa0b1738a0dc9caedb Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 17 Aug 2012 12:42:30 +0900 Subject: [PATCH] Merge the pull request #432 manually - Add UniqueNameTest - Add JavaDoc for UniqueName --- .../main/java/io/netty/util/UniqueName.java | 39 +++++++-- .../java/io/netty/util/UniqueNameTest.java | 87 +++++++++++++++++++ 2 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 common/src/test/java/io/netty/util/UniqueNameTest.java diff --git a/common/src/main/java/io/netty/util/UniqueName.java b/common/src/main/java/io/netty/util/UniqueName.java index 9eb3e8b4ba..525bb7bfe6 100644 --- a/common/src/main/java/io/netty/util/UniqueName.java +++ b/common/src/main/java/io/netty/util/UniqueName.java @@ -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 { private static final AtomicInteger nextId = new AtomicInteger(); @@ -25,6 +28,13 @@ public class UniqueName implements Comparable { 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 map, String name, Object... args) { if (map == null) { throw new NullPointerException("map"); @@ -37,7 +47,7 @@ public class UniqueName implements Comparable { } 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 { } /** + * 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 { } @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; diff --git a/common/src/test/java/io/netty/util/UniqueNameTest.java b/common/src/test/java/io/netty/util/UniqueNameTest.java new file mode 100644 index 0000000000..ac04603cb6 --- /dev/null +++ b/common/src/test/java/io/netty/util/UniqueNameTest.java @@ -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 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(); + } + + @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 nameList = new ArrayList(); + + 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()); + } + } + } + } + +}