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 remove operation for SchematicValueDelta. 025 * 026 * @author Randall Hauch <rhauch@redhat.com> (C) 2011 Red Hat Inc. 027 */ 028public class RemoveValueOperation extends ArrayOperation { 029 030 protected final Object value; 031 protected transient int actualIndex = -1; 032 033 public RemoveValueOperation( Path parentPath, 034 Object value ) { 035 super(parentPath, HashCode.compute(parentPath, value)); 036 this.value = value; 037 } 038 039 @Override 040 public RemoveValueOperation clone() { 041 return new RemoveValueOperation(getParentPath(), cloneValue(value)); 042 } 043 044 @Override 045 public void rollback( MutableDocument delegate ) { 046 if (actualIndex > -1) { 047 MutableArray array = mutableParent(delegate); 048 array.add(actualIndex, value); 049 } 050 } 051 052 public Object getRemovedValue() { 053 return value; 054 } 055 056 public int getActualIndex() { 057 return actualIndex; 058 } 059 060 @Override 061 public void replay( MutableDocument delegate ) { 062 MutableArray array = mutableParent(delegate); 063 actualIndex = array.indexOf(value); 064 array.remove(actualIndex); 065 } 066 067 @Override 068 public String toString() { 069 return "Remove at '" + parentPath + "' the value '" + value + "'"; 070 } 071 072 @Override 073 public int hashCode() { 074 return super.hashCode(); 075 } 076 077 @Override 078 public boolean equals( Object obj ) { 079 if (obj instanceof RemoveValueOperation) { 080 RemoveValueOperation other = (RemoveValueOperation)obj; 081 return equalsIfNotNull(value, other.value) && equalsIfNotNull(getParentPath(), other.getParentPath()); 082 083 } 084 return false; 085 } 086}