001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * Unless otherwise indicated, all code in JBoss DNA is licensed
010 * to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024 package org.jboss.dna.jcr.cache;
025
026 import java.util.List;
027 import java.util.ListIterator;
028 import java.util.UUID;
029 import net.jcip.annotations.NotThreadSafe;
030 import org.jboss.dna.graph.property.Name;
031 import org.jboss.dna.graph.property.PathFactory;
032
033 /**
034 * A {@link NotThreadSafe non-thread safe} implementation of {@link Children} that can be modified in place. This is typically
035 * used to capture changes made within a session.
036 */
037 @NotThreadSafe
038 public class ChangedChildren extends ImmutableChildren {
039
040 public ChangedChildren( Children original ) {
041 super(original);
042 }
043
044 /**
045 * Creates an empty instance.
046 *
047 * @param parentUuid the UUID of the parent node
048 */
049 protected ChangedChildren( UUID parentUuid ) {
050 super(parentUuid);
051 }
052
053 /**
054 * {@inheritDoc}
055 *
056 * @see org.jboss.dna.jcr.cache.ImmutableChildren#with(org.jboss.dna.graph.property.Name, java.util.UUID,
057 * org.jboss.dna.graph.property.PathFactory)
058 */
059 @Override
060 public ChangedChildren with( Name newChildName,
061 UUID newChildUuid,
062 PathFactory pathFactory ) {
063 // Simply add the node to this object ...
064 super.add(newChildName, newChildUuid, pathFactory);
065 return this;
066 }
067
068 /**
069 * {@inheritDoc}
070 *
071 * @see org.jboss.dna.jcr.cache.ImmutableChildren#without(java.util.UUID, org.jboss.dna.graph.property.PathFactory)
072 */
073 @Override
074 public ChangedChildren without( UUID childUuid,
075 PathFactory pathFactory ) {
076 // Remove the object that has the same UUID (regardless of the current SNS index) ...
077 ChildNode toBeRemoved = childrenByUuid.get(childUuid);
078 if (toBeRemoved == null) {
079 return this;
080 }
081 // Remove the child from this object, then adjust the remaining child node instances that follow it ...
082 Name childName = toBeRemoved.getName();
083 List<ChildNode> childrenWithSameName = childrenByName.get(childName);
084 int snsIndex = toBeRemoved.getSnsIndex();
085 if (snsIndex > childrenWithSameName.size()) {
086 // The child node (with that SNS index) is no longer here) ...
087 return this;
088 }
089 ListIterator<ChildNode> iter = childrenWithSameName.listIterator(--snsIndex);
090 assert iter.hasNext();
091 ChildNode willBeRemoved = iter.next();
092 assert willBeRemoved == toBeRemoved;
093 childrenByUuid.remove(toBeRemoved.getUuid());
094 iter.remove(); // removes the item that was last returned from 'next()'
095 while (iter.hasNext()) {
096 ChildNode next = iter.next();
097 ChildNode newNext = next.with(pathFactory.createSegment(childName, ++snsIndex));
098 childrenByUuid.put(newNext.getUuid(), newNext);
099 iter.set(newNext);
100 }
101 return this;
102 }
103
104 }