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.document.Path;
019import org.modeshape.schematic.internal.HashCode;
020import org.modeshape.schematic.internal.document.MutableArray;
021import org.modeshape.schematic.internal.document.MutableDocument;
022
023/**
024 * An atomic array add operation for SchematicValueDelta.
025 * 
026 * @author Randall Hauch <rhauch@redhat.com> (C) 2011 Red Hat Inc.
027 */
028public class SetValueOperation extends ArrayOperation {
029
030    protected final Object value;
031    protected final int index;
032
033    protected transient Object oldValue;
034
035    public SetValueOperation( Path parentPath,
036                              Object value,
037                              int index ) {
038        super(parentPath, HashCode.compute(parentPath, value, index));
039        this.value = value;
040        this.index = index;
041    }
042
043    @Override
044    public SetValueOperation clone() {
045        return new SetValueOperation(getParentPath(), cloneValue(value), index);
046    }
047
048    @Override
049    public void rollback( MutableDocument delegate ) {
050        MutableArray array = mutableParent(delegate);
051        if (oldValue != null) {
052            array.set(index, oldValue);
053        } else {
054            array.remove(index);
055        }
056    }
057
058    public int getIndex() {
059        return index;
060    }
061
062    public Object getValue() {
063        return value;
064    }
065
066    @Override
067    public void replay( MutableDocument delegate ) {
068        MutableArray array = mutableParent(delegate);
069        oldValue = array.setValue(index, value);
070    }
071
072    @Override
073    public String toString() {
074        return "Set at '" + parentPath + "' the value '" + value + "' (at index " + index + ")";
075    }
076
077    @Override
078    public int hashCode() {
079        return super.hashCode();
080    }
081
082    @Override
083    public boolean equals( Object obj ) {
084        if (obj instanceof SetValueOperation) {
085            SetValueOperation other = (SetValueOperation)obj;
086            return equalsIfNotNull(value, other.value) && index == other.index
087                   && equalsIfNotNull(getParentPath(), other.getParentPath());
088
089        }
090        return false;
091    }
092}