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 java.util.Collection; 019import java.util.List; 020import org.modeshape.schematic.document.Array.Entry; 021import org.modeshape.schematic.document.Path; 022import org.modeshape.schematic.internal.HashCode; 023import org.modeshape.schematic.internal.document.MutableArray; 024import org.modeshape.schematic.internal.document.MutableDocument; 025 026/** 027 * An atomic array add operation for SchematicValueDelta. 028 * 029 * @author Randall Hauch <rhauch@redhat.com> (C) 2011 Red Hat Inc. 030 */ 031public class RetainAllValuesOperation extends ArrayOperation { 032 033 protected final Collection<?> values; 034 protected transient List<Entry> removedEntries; 035 036 public RetainAllValuesOperation( Path parentPath, 037 Collection<?> values ) { 038 super(parentPath, HashCode.compute(parentPath, values)); 039 this.values = values; 040 } 041 042 @Override 043 public RetainAllValuesOperation clone() { 044 return new RetainAllValuesOperation(getParentPath(), cloneValues(values)); 045 } 046 047 @Override 048 public void rollback( MutableDocument delegate ) { 049 if (removedEntries != null) { 050 // Add into the same locations ... 051 MutableArray array = mutableParent(delegate); 052 for (Entry entry : removedEntries) { 053 array.add(entry.getIndex(), entry.getValue()); 054 } 055 } 056 } 057 058 public Collection<?> getRetainedValues() { 059 return values; 060 } 061 062 public List<Entry> getRemovedEntries() { 063 return removedEntries; 064 } 065 066 @Override 067 public void replay( MutableDocument delegate ) { 068 MutableArray array = mutableParent(delegate); 069 removedEntries = array.retainAllValues(values); 070 } 071 072 @Override 073 public String toString() { 074 return "Retain at '" + parentPath + "' the values: " + values; 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 RetainAllValuesOperation) { 085 RetainAllValuesOperation other = (RetainAllValuesOperation)obj; 086 return equalsIfNotNull(values, other.values) && equalsIfNotNull(getParentPath(), other.getParentPath()); 087 088 } 089 return false; 090 } 091}