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.MutableDocument; 021 022/** 023 * An atomic put operation for SchematicValueDelta. 024 * 025 * @author (various) 026 */ 027public class PutOperation extends Operation { 028 protected final String fieldName; 029 protected final Object oldValue; 030 protected final Object newValue; 031 032 public PutOperation( Path parentPath, 033 String fieldName, 034 Object oldValue, 035 Object newValue ) { 036 super(parentPath, HashCode.compute(parentPath, fieldName, /*oldValue,*/newValue)); 037 this.fieldName = fieldName; 038 this.oldValue = oldValue; 039 this.newValue = newValue; 040 } 041 042 @Override 043 public PutOperation clone() { 044 return new PutOperation(getParentPath(), fieldName, cloneValue(oldValue), cloneValue(newValue)); 045 } 046 047 public Object getNewValue() { 048 return newValue; 049 } 050 051 public Object getOldValue() { 052 return oldValue; 053 } 054 055 public String getFieldName() { 056 return fieldName; 057 } 058 059 @Override 060 public void rollback( MutableDocument delegate ) { 061 MutableDocument parent = mutableParent(delegate); 062 assert parent != null; 063 if (oldValue == null) { 064 parent.remove(fieldName); 065 } else { 066 parent.put(fieldName, oldValue); 067 } 068 } 069 070 @Override 071 public void replay( MutableDocument delegate ) { 072 MutableDocument parent = mutableParent(delegate); 073 assert parent != null; 074 parent.put(fieldName, newValue); 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 PutOperation) { 085 PutOperation other = (PutOperation)obj; 086 return equalsIfNotNull(fieldName, other.fieldName) /*&& equalsIfNotNull(oldValue, other.oldValue)*/ 087 && equalsIfNotNull(newValue, other.newValue) && equalsIfNotNull(getParentPath(), other.getParentPath()); 088 } 089 return false; 090 } 091 092 @Override 093 public String toString() { 094 return "Put at '" + parentPath + "' the '" + fieldName + "' field value '" + newValue 095 + (oldValue != null ? "' (replaces '" + oldValue + "')" : "'"); 096 } 097}