- final void copyTo(final StatementContextBase<?, ?, ?> target, final CopyType typeOfCopy,
- @Nullable final QNameModule targetModule) {
- final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(substatements.size() + effective.size());
-
- for (final Mutable<?, ?, ?> stmtContext : substatements.values()) {
- if (stmtContext.isSupportedByFeatures()) {
- copySubstatement(stmtContext, target, typeOfCopy, targetModule, buffer);
- }
- }
-
- for (final Mutable<?, ?, ?> stmtContext : effective) {
- copySubstatement(stmtContext, target, typeOfCopy, targetModule, buffer);
- }
-
- target.addEffectiveSubstatements(buffer);
- }
-
- private void copySubstatement(final Mutable<?, ?, ?> stmtContext, final Mutable<?, ?, ?> target,
- final CopyType typeOfCopy, final QNameModule newQNameModule, final Collection<Mutable<?, ?, ?>> buffer) {
- if (needToCopyByUses(stmtContext)) {
- final Mutable<?, ?, ?> copy = target.childCopyOf(stmtContext, typeOfCopy, newQNameModule);
- LOG.debug("Copying substatement {} for {} as {}", stmtContext, this, copy);
- buffer.add(copy);
- } else if (isReusedByUses(stmtContext)) {
- LOG.debug("Reusing substatement {} for {}", stmtContext, this);
- buffer.add(stmtContext);
- } else {
- LOG.debug("Skipping statement {}", stmtContext);
- }
- }
-
- // FIXME: revise this, as it seems to be wrong
- private static final ImmutableSet<YangStmtMapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
- YangStmtMapping.DESCRIPTION,
- YangStmtMapping.REFERENCE,
- YangStmtMapping.STATUS);
- private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
- YangStmtMapping.TYPE,
- YangStmtMapping.TYPEDEF,
- YangStmtMapping.USES);
-
- private static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
- final StatementDefinition def = stmtContext.getPublicDefinition();
- if (REUSED_DEF_SET.contains(def)) {
- LOG.debug("Will reuse {} statement {}", def, stmtContext);
- return false;
- }
- if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
- return !YangStmtMapping.GROUPING.equals(stmtContext.coerceParentContext().getPublicDefinition());
- }
-
- LOG.debug("Will copy {} statement {}", def, stmtContext);
- return true;
- }
-
- private static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
- return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
- }
-
- @Override
- public final String toString() {
- return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
- }