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.document.MutableArray; 020import org.modeshape.schematic.internal.document.MutableDocument; 021 022/** 023 * An atomic array add operation for SchematicValueDelta. 024 * 025 * @author Randall Hauch <rhauch@redhat.com> (C) 2011 Red Hat Inc. 026 */ 027public class AddValueIfAbsentOperation extends AddValueOperation { 028 029 protected transient boolean added; 030 031 public AddValueIfAbsentOperation( Path path, 032 Object value ) { 033 super(path, value); 034 } 035 036 public boolean isAdded() { 037 return added; 038 } 039 040 @Override 041 public void rollback( MutableDocument delegate ) { 042 if (added) { 043 MutableArray array = mutableParent(delegate); 044 array.remove(this.index); 045 } 046 } 047 048 @Override 049 public void replay( MutableDocument delegate ) { 050 MutableArray array = mutableParent(delegate); 051 added = array.addValueIfAbsent(value); 052 } 053 054 @Override 055 public String toString() { 056 return super.toString() + " if absent"; 057 } 058 059 @Override 060 public int hashCode() { 061 return super.hashCode(); 062 } 063 064 @Override 065 public boolean equals( Object obj ) { 066 if (obj instanceof AddValueIfAbsentOperation) { 067 AddValueIfAbsentOperation other = (AddValueIfAbsentOperation)obj; 068 return equalsIfNotNull(value, other.value) && index == other.index 069 && equalsIfNotNull(getParentPath(), other.getParentPath()); 070 071 } 072 return false; 073 } 074}