+ @Override
+ public Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> parent, final CopyType type,
+ final QNameModule targetModule) {
+ checkEffectiveModelCompleted(this);
+
+ final StatementSupport<A, D, E> support = definition.support();
+ final CopyPolicy policy = support.applyCopyPolicy(this, parent, type, targetModule);
+ switch (policy) {
+ case CONTEXT_INDEPENDENT:
+ if (hasEmptySubstatements()) {
+ // This statement is context-independent and has no substatements -- hence it can be freely shared.
+ return Optional.of(this);
+ }
+ // FIXME: YANGTOOLS-694: filter out all context-independent substatements, eliminate fall-through
+ // fall through
+ case DECLARED_COPY:
+ // FIXME: YANGTOOLS-694: this is still to eager, we really want to copy as a lazily-instantiated
+ // context, so that we can support building an effective statement without copying
+ // anything -- we will typically end up not being inferred against. In that case,
+ // this slim context should end up dealing with differences at buildContext()
+ // time. This is a YANGTOOLS-1067 prerequisite (which will deal with what can and
+ // cannot be shared across instances).
+ return Optional.of(parent.childCopyOf(this, type, targetModule));
+ case IGNORE:
+ return Optional.empty();
+ case REJECT:
+ throw new IllegalStateException("Statement " + support.getPublicView() + " should never be copied");
+ default:
+ throw new IllegalStateException("Unhandled policy " + policy);
+ }
+ }
+