Abstract class and an inheritor: is it possible to factorize .parent() here?
Posted
by
fge
on Stack Overflow
See other posts from Stack Overflow
or by fge
Published on 2013-06-29T16:17:06Z
Indexed on
2013/06/29
16:21 UTC
Read the original article
Hit count: 135
java
|refactoring
Here are what I think are the relevant parts of the code of these two classes. First, TreePointer
(original source here):
public abstract class TreePointer<T extends TreeNode>
implements Iterable<TokenResolver<T>>
{
//...
/**
* What this tree can see as a missing node (may be {@code null})
*/
private final T missing;
/**
* The list of token resolvers
*/
protected final List<TokenResolver<T>> tokenResolvers;
/**
* Main protected constructor
*
* <p>This constructor makes an immutable copy of the list it receives as
* an argument.</p>
*
* @param missing the representation of a missing node (may be null)
* @param tokenResolvers the list of reference token resolvers
*/
protected TreePointer(final T missing,
final List<TokenResolver<T>> tokenResolvers)
{
this.missing = missing;
this.tokenResolvers = ImmutableList.copyOf(tokenResolvers);
}
/**
* Alternate constructor
*
* <p>This is the same as calling {@link #TreePointer(TreeNode, List)} with
* {@code null} as the missing node.</p>
*
* @param tokenResolvers the list of token resolvers
*/
protected TreePointer(final List<TokenResolver<T>> tokenResolvers)
{
this(null, tokenResolvers);
}
//...
/**
* Tell whether this pointer is empty
*
* @return true if the reference token list is empty
*/
public final boolean isEmpty()
{
return tokenResolvers.isEmpty();
}
@Override
public final Iterator<TokenResolver<T>> iterator()
{
return tokenResolvers.iterator();
}
// .equals(), .hashCode(), .toString() follow
}
Then, JsonPointer
, which contains this .parent()
method which I'd like to factorize here (original source here:
public final class JsonPointer
extends TreePointer<JsonNode>
{
/**
* The empty JSON Pointer
*/
private static final JsonPointer EMPTY
= new JsonPointer(ImmutableList.<TokenResolver<JsonNode>>of());
/**
* Return an empty JSON Pointer
*
* @return an empty, statically allocated JSON Pointer
*/
public static JsonPointer empty()
{
return EMPTY;
}
//...
/**
* Return the immediate parent of this JSON Pointer
*
* <p>The parent of the empty pointer is itself.</p>
*
* @return a new JSON Pointer representing the parent of the current one
*/
public JsonPointer parent()
{
final int size = tokenResolvers.size();
return size <= 1 ? EMPTY
: new JsonPointer(tokenResolvers.subList(0, size - 1));
}
// ...
}
As mentioned in the subject, the problem I have here is with JsonPointer
's .parent()
method. In fact, the logic behind this method applies to TreeNode
all the same, and therefore to its future implementations. Except that I have to use a constructor, and of course such a constructor is implementation dependent :/
Is there a way to make that .parent()
method available to each and every implementation of TreeNode
or is it just a pipe dream?
© Stack Overflow or respective owner