netty5/common/src/main/java/io/netty/util/AttributeKey.java

52 lines
1.3 KiB
Java
Raw Normal View History

package io.netty.util;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public final class AttributeKey<T> implements Serializable, Comparable<AttributeKey<T>> {
private static final long serialVersionUID = 2783354860083517323L;
private static final ConcurrentMap<String, Boolean> names = new ConcurrentHashMap<String, Boolean>();
private final String name;
private final Class<T> valueType;
2012-05-12 17:40:52 +02:00
private final String strVal;
public AttributeKey(String name, Class<T> valueType) {
if (name == null) {
throw new NullPointerException("name");
}
if (valueType == null) {
throw new NullPointerException("valueType");
}
if (names.putIfAbsent(name, Boolean.TRUE) != null) {
throw new IllegalArgumentException("key name already in use: " + name);
}
this.name = name;
this.valueType = valueType;
2012-05-12 17:40:52 +02:00
strVal = name + '[' + valueType.getSimpleName() + ']';
}
public String name() {
return name;
}
public Class<T> valueType() {
return valueType;
}
@Override
public int compareTo(AttributeKey<T> o) {
return name().compareTo(o.name());
}
@Override
public String toString() {
2012-05-12 17:40:52 +02:00
return strVal;
}
}