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 AddValueOperation extends ArrayOperation { 029 030 protected static final int APPEND_INDEX = -1; 031 032 protected final Object value; 033 protected final int index; 034 protected transient int actualIndex = -1; 035 036 public AddValueOperation( Path path, 037 Object value ) { 038 super(path, HashCode.compute(path, value, APPEND_INDEX)); 039 assert value != null; 040 this.value = value; 041 this.index = APPEND_INDEX; 042 } 043 044 public AddValueOperation( Path path, 045 Object value, 046 int index ) { 047 super(path, HashCode.compute(path, value, index)); 048 assert value != null; 049 this.value = value; 050 this.index = index; 051 } 052 053 @Override 054 public AddValueOperation clone() { 055 return new AddValueOperation(getParentPath(), cloneValue(value), index); 056 } 057 058 public Object getValue() { 059 return value; 060 } 061 062 public int getIndex() { 063 return index; 064 } 065 066 public int getActualIndex() { 067 return actualIndex; 068 } 069 070 @Override 071 public void rollback( MutableDocument delegate ) { 072 if (actualIndex > -1) { 073 MutableArray array = mutableParent(delegate); 074 array.remove(actualIndex); 075 } 076 } 077 078 @Override 079 public void replay( MutableDocument delegate ) { 080 MutableArray array = mutableParent(delegate); 081 if (index == APPEND_INDEX) { 082 actualIndex = array.addValue(value); 083 } else { 084 array.addValue(index, value); 085 actualIndex = index; 086 } 087 } 088 089 @Override 090 public String toString() { 091 return "Add to '" + parentPath + "' the value '" + value + "'" + (index >= 0 ? " at index " + index : ""); 092 } 093 094 @Override 095 public int hashCode() { 096 return super.hashCode(); 097 } 098 099 @Override 100 public boolean equals( Object obj ) { 101 if (obj instanceof AddValueOperation) { 102 AddValueOperation other = (AddValueOperation)obj; 103 return equalsIfNotNull(value, other.value) && index == other.index 104 && equalsIfNotNull(getParentPath(), other.getParentPath()); 105 106 } 107 return false; 108 } 109}