/** * Copies a context node into a target context node. * @param contextNode A context node from any context node. * @param targetContextNode The target context node. * @param copyStrategy The strategy to determine what to copy. * @return The copied context node in the target context node. */ public static ContextNode copyContextNode(ContextNode contextNode, ContextNode targetContextNode, CopyStrategy copyStrategy) { if (contextNode == null) throw new NullPointerException(); if (targetContextNode == null) throw new NullPointerException(); if (copyStrategy == null) copyStrategy = DEFAULT_COPY_STRATEGY; List<ContextNode> copyContextNodes = copyStrategy.replaceContextNode(contextNode); if (copyContextNodes == null) copyContextNodes = Collections.singletonList(contextNode); // TODO: maybe avoid creating a list object here ContextNode targetCopiedContextNode = null; for (ContextNode copyContextNode : copyContextNodes) { XDIArc copyContextNodeXDIArc = copyContextNode.getXDIArc(); if (copyContextNodeXDIArc != null) targetCopiedContextNode = targetContextNode.setContextNode(copyContextNodeXDIArc); else targetCopiedContextNode = targetContextNode; copyContextNodeContents(copyContextNode, targetCopiedContextNode, copyStrategy); } return copyContextNodes.size() > 1 ? null : targetCopiedContextNode; }
@Override public List<Relation> replaceRelation(Relation relation) { List<Relation> compoundCopyRelations = Collections.singletonList(relation); for (CopyStrategy copyStrategy : this.copyStrategies) { List<Relation> nextCompoundCopyRelations = new ArrayList<Relation> (); for (Relation compoundCopyRelation : compoundCopyRelations) { List<Relation> copyRelations = copyStrategy.replaceRelation(compoundCopyRelation); if (copyRelations != null) nextCompoundCopyRelations.addAll(copyRelations); else nextCompoundCopyRelations.add(compoundCopyRelation); } compoundCopyRelations = nextCompoundCopyRelations; } return compoundCopyRelations; }
if (copyStrategy == null) copyStrategy = DEFAULT_COPY_STRATEGY; List<Relation> copyRelations = copyStrategy.replaceRelation(relation); if (copyRelations == null) copyRelations = Collections.singletonList(relation); // TODO: maybe avoid creating a list object here
@Override public List<ContextNode> replaceContextNode(ContextNode contextNode) { List<ContextNode> compoundCopyContextNodes = Collections.singletonList(contextNode); for (CopyStrategy copyStrategy : this.copyStrategies) { List<ContextNode> nextCompoundCopyContextNodes = new ArrayList<ContextNode> (); for (ContextNode compoundCopyContextNode : compoundCopyContextNodes) { List<ContextNode> copyContextNodes = copyStrategy.replaceContextNode(compoundCopyContextNode); if (copyContextNodes != null) nextCompoundCopyContextNodes.addAll(copyContextNodes); else nextCompoundCopyContextNodes.add(compoundCopyContextNode); } compoundCopyContextNodes = nextCompoundCopyContextNodes; } return compoundCopyContextNodes; }
/** * Copies a literal into another context node. * @param literalNode A literal from any context node. * @param targetContextNode The target context node. * @param copyStrategy The strategy to determine what to copy. * @return The copied literal in the target context node. */ public static LiteralNode copyLiteralNode(LiteralNode literalNode, ContextNode targetContextNode, CopyStrategy copyStrategy) { if (literalNode == null) throw new NullPointerException(); if (targetContextNode == null) throw new NullPointerException(); if (copyStrategy == null) copyStrategy = DEFAULT_COPY_STRATEGY; if ((literalNode = copyStrategy.replaceLiteralNode(literalNode)) == null) return null; Object literalData = literalNode.getLiteralData(); LiteralNode targetLiteral = targetContextNode.setLiteralNode(literalData); return targetLiteral; }
@Override public LiteralNode replaceLiteralNode(LiteralNode literalNode) { for (CopyStrategy copyStrategy : this.copyStrategies) literalNode = copyStrategy.replaceLiteralNode(literalNode); return literalNode; } }