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 org.modeshape.schematic.annotation.Immutable;
020import org.modeshape.schematic.document.Array;
021import org.modeshape.schematic.document.Document;
022import org.modeshape.schematic.document.Path;
023import org.modeshape.schematic.internal.document.ArrayEditor;
024import org.modeshape.schematic.internal.document.MutableArray;
025import org.modeshape.schematic.internal.document.MutableDocument;
026
027/**
028 * An atomic operation on an {@link Array} for SchematicValueDelta.
029 * <p/>
030 * 
031 * @author (various)
032 */
033@Immutable
034public abstract class ArrayOperation extends Operation {
035
036    protected ArrayOperation( Path path,
037                              int hashCode ) {
038        super(path, hashCode);
039    }
040
041    @Override
042    protected MutableArray mutableParent( MutableDocument delegate ) {
043        Document parent = delegate;
044        Path parentPath = getParentPath();
045        for (String fieldName : parentPath) {
046            assert parent != null : "Unexpected to find path " + parentPath + " in " + delegate + ". Unable to apply operation "
047                                    + this;
048            parent = parent.getDocument(fieldName);
049        }
050        if (parent instanceof ArrayEditor) {
051            ArrayEditor parentEditor = (ArrayEditor)parent;
052            parent = parentEditor.unwrap();
053        }
054        return (MutableArray)parent;
055    }
056
057    protected Collection<?> cloneValues( Collection<?> values ) {
058        return (Collection<?>)cloneValue(values);
059    }
060
061}