001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.schematic.internal.delta;
017
018import org.modeshape.schematic.annotation.Immutable;
019import org.modeshape.schematic.document.Path;
020import org.modeshape.schematic.internal.HashCode;
021import org.modeshape.schematic.internal.document.MutableDocument;
022
023/**
024 * An atomic remove operation for SchematicValueDelta.
025 * 
026 * @author (various)
027 */
028@Immutable
029public class RemoveOperation extends Operation {
030    protected final String fieldName;
031    protected final Object oldValue;
032
033    private transient boolean removed;
034
035    public RemoveOperation( Path parentPath,
036                            String fieldName,
037                            Object oldValue ) {
038        super(parentPath, HashCode.compute(parentPath, fieldName /*,oldValue*/));
039        this.fieldName = fieldName;
040        this.oldValue = oldValue;
041    }
042
043    @Override
044    public RemoveOperation clone() {
045        return new RemoveOperation(getParentPath(), fieldName, cloneValue(oldValue));
046    }
047
048    public String getFieldName() {
049        return fieldName;
050    }
051
052    @Override
053    public void rollback( MutableDocument delegate ) {
054        if (oldValue != null) {
055            delegate = mutableParent(delegate);
056            delegate.put(fieldName, oldValue);
057        }
058    }
059
060    @Override
061    public void replay( MutableDocument delegate ) {
062        MutableDocument parent = mutableParent(delegate);
063        assert parent != null;
064        removed = parent.remove(fieldName) != null;
065    }
066
067    public boolean isRemoved() {
068        return removed;
069    }
070
071    @Override
072    public String toString() {
073        return "Remove from '" + parentPath + "' the '" + fieldName + "' field value '" + oldValue + "'";
074    }
075
076    @Override
077    public int hashCode() {
078        return super.hashCode();
079    }
080
081    @Override
082    public boolean equals( Object obj ) {
083        if (obj instanceof RemoveOperation) {
084            RemoveOperation other = (RemoveOperation)obj;
085            // Note we don't consider the 'oldValue', since two removes are equivalent based only upon the field name ...
086            return equalsIfNotNull(fieldName, other.fieldName) /*&& equalsIfNotNull(oldValue, other.oldValue)*/
087                   && equalsIfNotNull(getParentPath(), other.getParentPath());
088
089        }
090        return false;
091    }
092}