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
31a6ab9b1d
commit
c4d69e982b
@ -55,31 +55,24 @@ public abstract class ConstantPool<T extends Constant<T>> {
|
|||||||
* @param name the name of the {@link Constant}
|
* @param name the name of the {@link Constant}
|
||||||
*/
|
*/
|
||||||
public T valueOf(String name) {
|
public T valueOf(String name) {
|
||||||
if (name == null) {
|
T c;
|
||||||
throw new NullPointerException("name");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name.isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("empty name");
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized (constants) {
|
synchronized (constants) {
|
||||||
T c = constants.get(name);
|
if (exists(name)) {
|
||||||
if (c == null) {
|
c = constants.get(name);
|
||||||
c = newConstant(nextId, name);
|
} else {
|
||||||
constants.put(name, c);
|
c = newInstance0(name);
|
||||||
nextId ++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns {@code true} if a {@link AttributeKey} exists for the given {@code name}.
|
* Returns {@code true} if a {@link AttributeKey} exists for the given {@code name}.
|
||||||
*/
|
*/
|
||||||
public boolean exists(String name) {
|
public boolean exists(String name) {
|
||||||
ObjectUtil.checkNotNull(name, "name");
|
checkNotNullAndNotEmpty(name);
|
||||||
synchronized (constants) {
|
synchronized (constants) {
|
||||||
return constants.containsKey(name);
|
return constants.containsKey(name);
|
||||||
}
|
}
|
||||||
@ -91,25 +84,33 @@ public abstract class ConstantPool<T extends Constant<T>> {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T newInstance(String name) {
|
public T newInstance(String name) {
|
||||||
if (name == null) {
|
if (exists(name)) {
|
||||||
throw new NullPointerException("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()) {
|
if (name.isEmpty()) {
|
||||||
throw new IllegalArgumentException("empty name");
|
throw new IllegalArgumentException("empty name");
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (constants) {
|
return name;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract T newConstant(int id, String name);
|
protected abstract T newConstant(int id, String name);
|
||||||
|
Loading…
Reference in New Issue
Block a user