netty5/common/src/main/java/io/netty/util/AbstractConstant.java
Trustin Lee dc009b2c2c Replace UniqueName with Constant and ConstantPool
- Proposed fix for #1824

UniqueName and its subtypes do not allow getting the previously registered instance.  For example, let's assume that a user is running his/her application in an OSGi container with Netty bundles and his server bundle.  Whenever the server bundle is reloaded, the server will try to create a new AttributeKey instance with the same name.  However, Netty bundles were not reloaded at all, so AttributeKey will complain that the name is taken already (by the previously loaded bundle.)

To fix this problem:

- Replaced UniqueName with Constant, AbstractConstant, and ConstantPool.  Better name and better design.

- Sctp/Udt/RxtxChannelOption is not a ChannelOption anymore.  They are just constant providers and ChannelOption is final now.  It's because caching anything that's from outside of netty-transport will lead to ClassCastException on reload, because ChannelOption's constant pool will keep all option objects for reuse.

- Signal implements Constant because we can't ensure its uniqueness anymore by relying on the exception raised by UniqueName's constructor.
2013-10-25 19:21:53 +09:00

73 lines
1.7 KiB
Java

/*
* 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;
/**
* Base implementation of {@link Constant}.
*/
public abstract class AbstractConstant<T extends AbstractConstant<T>> implements Constant<T> {
private final int id;
private final String name;
/**
* Creates a new instance.
*/
protected AbstractConstant(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public final String name() {
return name;
}
@Override
public final int id() {
return id;
}
@Override
public final int hashCode() {
return super.hashCode();
}
@Override
public final boolean equals(Object o) {
return super.equals(o);
}
@Override
public final int compareTo(T other) {
if (this == other) {
return 0;
}
int returnCode = name.compareTo(other.name());
if (returnCode != 0) {
return returnCode;
}
return ((Integer) id).compareTo(other.id());
}
@Override
public final String toString() {
return name();
}
}