if (resolver instanceof RootModificationApplyOperation) {
return ((RootModificationApplyOperation) resolver).snapshot();
}
- return new NotUpgradable(resolver);
- }
-
- /**
- * Implementation of Upgradable {@link RootModificationApplyOperation}
- *
- * This implementation is associated with {@link LatestOperationHolder}
- * which holds latest available implementation, which may be used for
- * upgrade.
- *
- * Upgrading {@link LatestOperationHolder} will not affect any instance,
- * unless client invoked {@link #upgradeIfPossible()} which will result in
- * changing delegate to the latest one.
- *
- */
- private static final class Upgradable extends RootModificationApplyOperation {
-
- private final LatestOperationHolder holder;
- private ModificationApplyOperation delegate;
-
- public Upgradable(final LatestOperationHolder holder, final ModificationApplyOperation delegate) {
- this.holder = holder;
- this.delegate = delegate;
-
- }
-
- @Override
- public void upgradeIfPossible() {
- ModificationApplyOperation holderCurrent = holder.getCurrent();
- if (holderCurrent != delegate) {
- // FIXME: Allow update only if there is addition of models, not
- // removals.
- delegate = holderCurrent;
- }
-
- }
-
- @Override
- ModificationApplyOperation getDelegate() {
- return delegate;
- }
-
- @Override
- public RootModificationApplyOperation snapshot() {
- return new Upgradable(holder, getDelegate());
- }
-
- }
-
- private static final class NotUpgradable extends RootModificationApplyOperation {
-
- private final ModificationApplyOperation delegate;
-
- public NotUpgradable(final ModificationApplyOperation delegate) {
- this.delegate = delegate;
- }
-
- @Override
- public ModificationApplyOperation getDelegate() {
- return delegate;
- }
-
- @Override
- public void upgradeIfPossible() {
- // Intentional noop
- }
-
- @Override
- public RootModificationApplyOperation snapshot() {
- return this;
- }
- }
-
- /**
- * Holder and factory for upgradable root modifications
- *
- * This class is factory for upgradable root modifications and provides an
- * access to set latest backing implementation.
- *
- */
- static class LatestOperationHolder {
-
- private ModificationApplyOperation current = new AlwaysFailOperation();
-
- /**
- * Return latest backing implemenation
- *
- * @return
- */
- public ModificationApplyOperation getCurrent() {
- return current;
- }
-
- /**
- * Sets latest backing implementation of associated
- * {@link RootModificationApplyOperation}.
- * <p>
- * Note: This does not result in upgrading implementation of already
- * existing {@link RootModificationApplyOperation}. Users, which
- * obtained instances using {@link #newSnapshot()}, deriving
- * {@link RootModificationApplyOperation} from this modification must
- * explicitly invoke
- * {@link RootModificationApplyOperation#upgradeIfPossible()} on their
- * instance to be updated to latest backing implementation.
- *
- * @param newApplyOper
- * New backing implementation
- */
- public void setCurrent(final ModificationApplyOperation newApplyOper) {
- current = newApplyOper;
- }
-
- /**
- *
- * Creates new upgradable {@link RootModificationApplyOperation}
- * associated with holder.
- *
- * @return New upgradable {@link RootModificationApplyOperation} with
- * {@link #getCurrent()} used as backing implementation.
- */
- public RootModificationApplyOperation newSnapshot() {
- return new Upgradable(this, current);
- }
-
+ return new NotUpgradableModificationApplyOperation(resolver);
}
}