以下是我认为这两个类的代码的相关部分。首先,TreePointer
(原始来源在这里):
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();
}
// .iterator(), .equals(), .hashCode(), .toString() follow
}
然后,JsonPointer
其中包含.parent()
我想在此处分解的此方法(此处为原始来源:
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));
}
// ...
}
如主题中所述,我在这里遇到的问题是JsonPointer
'.parent()
方法。事实上,这种方法背后的逻辑适用于所有TreeNode
实现。除了我必须使用构造函数,当然这样的构造函数是依赖于实现的:/
有没有办法实现.parent()
,以便每个实现都TreeNode
接收一个自身的实例而不是TreeNode
,或者它只是一个白日梦?