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.Path;
032    import org.jboss.dna.graph.property.PathFactory;
033    
034    /**
035     * A {@link NotThreadSafe non-thread safe} implementation of {@link Children} that can be modified in place. This is typically
036     * used to capture changes made within a session.
037     */
038    @NotThreadSafe
039    public class ChangedChildren extends ImmutableChildren {
040    
041        public ChangedChildren( Children original ) {
042            super(original);
043        }
044    
045        /**
046         * Creates an empty instance.
047         * 
048         * @param parentUuid the UUID of the parent node
049         */
050        protected ChangedChildren( UUID parentUuid ) {
051            super(parentUuid);
052        }
053    
054        protected ChangedChildren( ImmutableChildren original,
055                                     Name additionalChildName,
056                                     Path.Segment beforeChild,
057                                     UUID childUuid,
058                                     PathFactory pathFactory ) {
059            super(original, additionalChildName, beforeChild, childUuid, pathFactory);
060        }
061        /**
062         * {@inheritDoc}
063         * 
064         * @see org.jboss.dna.jcr.cache.ImmutableChildren#with(org.jboss.dna.graph.property.Name, java.util.UUID,
065         *      org.jboss.dna.graph.property.PathFactory)
066         */
067        @Override
068        public ChangedChildren with( Name newChildName,
069                                     UUID newChildUuid,
070                                     PathFactory pathFactory ) {
071            // Simply add the node to this object ...
072            super.add(newChildName, newChildUuid, pathFactory);
073            return this;
074        }
075    
076        /**
077         * Create another Children object that is equivalent to this node but with the supplied child added before the named node.
078         * 
079         * @param newChildName the name of the new child; may not be null
080         * @param beforeChild the path segment of the child before which this node should be added; may not be null
081         * @param newChildUuid the UUID of the new child; may not be null
082         * @param pathFactory the factory that can be used to create Path and/or Path.Segment instances.
083         * @return the new Children object; never null
084         */
085        public ChangedChildren with( Name newChildName,
086                                     Path.Segment beforeChild,
087                                     UUID newChildUuid,
088                                     PathFactory pathFactory ) {
089            return new ChangedChildren(this, newChildName, beforeChild, newChildUuid, pathFactory);
090        }
091    
092        /**
093         * {@inheritDoc}
094         * 
095         * @see org.jboss.dna.jcr.cache.ImmutableChildren#without(java.util.UUID, org.jboss.dna.graph.property.PathFactory)
096         */
097        @Override
098        public ChangedChildren without( UUID childUuid,
099                                        PathFactory pathFactory ) {
100            // Remove the object that has the same UUID (regardless of the current SNS index) ...
101            ChildNode toBeRemoved = childrenByUuid.get(childUuid);
102            if (toBeRemoved == null) {
103                return this;
104            }
105            // Remove the child from this object, then adjust the remaining child node instances that follow it ...
106            Name childName = toBeRemoved.getName();
107            List<ChildNode> childrenWithSameName = childrenByName.get(childName);
108            int snsIndex = toBeRemoved.getSnsIndex();
109            if (snsIndex > childrenWithSameName.size()) {
110                // The child node (with that SNS index) is no longer here) ...
111                return this;
112            }
113            ListIterator<ChildNode> iter = childrenWithSameName.listIterator(--snsIndex);
114            assert iter.hasNext();
115            ChildNode willBeRemoved = iter.next();
116            assert willBeRemoved == toBeRemoved;
117            childrenByUuid.remove(toBeRemoved.getUuid());
118            iter.remove(); // removes the item that was last returned from 'next()'
119            while (iter.hasNext()) {
120                ChildNode next = iter.next();
121                ChildNode newNext = next.with(pathFactory.createSegment(childName, ++snsIndex));
122                childrenByUuid.put(newNext.getUuid(), newNext);
123                iter.set(newNext);
124            }
125            return this;
126        }
127    
128    }