Remove duplicate code in ConstantPool class
Motivation: Currently, valueOf() and newInstance() use almost same code to create new constant. For maintainability, it's better to share duplicate code among them. Motification: Added new private functions. - checkNotNullAndNotEmpty() is for checking whether the name of a constant is null and empty. - newConstant0() is for creating a new constant. Result: - Compact source code - Improvement of maintainability
This commit is contained in:
parent
e3980a6487
commit
5a9da7c5ba
@ -55,31 +55,24 @@ public abstract class ConstantPool<T extends Constant<T>> {
|
||||
* @param name the name of the {@link Constant}
|
||||
*/
|
||||
public T valueOf(String name) {
|
||||
if (name == null) {
|
||||
throw new NullPointerException("name");
|
||||
}
|
||||
|
||||
if (name.isEmpty()) {
|
||||
throw new IllegalArgumentException("empty name");
|
||||
}
|
||||
T c;
|
||||
|
||||
synchronized (constants) {
|
||||
T c = constants.get(name);
|
||||
if (c == null) {
|
||||
c = newConstant(nextId, name);
|
||||
constants.put(name, c);
|
||||
nextId ++;
|
||||
if (exists(name)) {
|
||||
c = constants.get(name);
|
||||
} else {
|
||||
c = newInstance0(name);
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if a {@link AttributeKey} exists for the given {@code name}.
|
||||
*/
|
||||
public boolean exists(String name) {
|
||||
ObjectUtil.checkNotNull(name, "name");
|
||||
checkNotNullAndNotEmpty(name);
|
||||
synchronized (constants) {
|
||||
return constants.containsKey(name);
|
||||
}
|
||||
@ -91,25 +84,33 @@ public abstract class ConstantPool<T extends Constant<T>> {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public T newInstance(String name) {
|
||||
if (name == null) {
|
||||
throw new NullPointerException("name");
|
||||
if (exists(name)) {
|
||||
throw new IllegalArgumentException(String.format("'%s' is already in use", name));
|
||||
}
|
||||
|
||||
T c = newInstance0(name);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
// Be careful that this dose not check whether the argument is null or empty.
|
||||
private T newInstance0(String name) {
|
||||
synchronized (constants) {
|
||||
T c = newConstant(nextId, name);
|
||||
constants.put(name, c);
|
||||
nextId++;
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
private String checkNotNullAndNotEmpty(String name) {
|
||||
ObjectUtil.checkNotNull(name, "name");
|
||||
|
||||
if (name.isEmpty()) {
|
||||
throw new IllegalArgumentException("empty name");
|
||||
}
|
||||
|
||||
synchronized (constants) {
|
||||
T c = constants.get(name);
|
||||
if (c == null) {
|
||||
c = newConstant(nextId, name);
|
||||
constants.put(name, c);
|
||||
nextId ++;
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format("'%s' is already in use", name));
|
||||
}
|
||||
return c;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
protected abstract T newConstant(int id, String name);
|
||||
|
Loading…
Reference in New Issue
Block a user