001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.graph.commands.basic;
023    
024    import java.util.HashMap;
025    import java.util.LinkedList;
026    import java.util.List;
027    import java.util.Map;
028    import net.jcip.annotations.NotThreadSafe;
029    import org.jboss.dna.common.util.StringUtil;
030    import org.jboss.dna.graph.cache.CachePolicy;
031    import org.jboss.dna.graph.commands.GetChildrenCommand;
032    import org.jboss.dna.graph.properties.DateTime;
033    import org.jboss.dna.graph.properties.Path;
034    import org.jboss.dna.graph.properties.Property;
035    import org.jboss.dna.graph.properties.Path.Segment;
036    
037    /**
038     * @author Randall Hauch
039     */
040    @NotThreadSafe
041    public class BasicGetChildrenCommand extends BasicGraphCommand implements GetChildrenCommand {
042    
043        /**
044         */
045        private static final long serialVersionUID = -8515194602506918337L;
046        private final Map<Segment, Property[]> childProperties = new HashMap<Segment, Property[]>();
047        private final List<Segment> children = new LinkedList<Segment>();
048        private final Path path;
049        private CachePolicy cachePolicy;
050        private DateTime timeLoaded;
051    
052        /**
053         * @param path the path to the node; may not be null
054         */
055        public BasicGetChildrenCommand( Path path ) {
056            super();
057            assert path != null;
058            this.path = path;
059        }
060    
061        /**
062         * {@inheritDoc}
063         */
064        public void setNoChildren() {
065            this.children.clear();
066        }
067    
068        /**
069         * {@inheritDoc}
070         * 
071         * @see org.jboss.dna.graph.commands.GetChildrenCommand#addChild(org.jboss.dna.graph.properties.Path.Segment,
072         *      org.jboss.dna.graph.properties.Property[])
073         */
074        public void addChild( Segment nameOfChild,
075                              Property... identityProperties ) {
076            if (nameOfChild == null) return;
077            this.children.add(nameOfChild);
078            if (identityProperties != null) {
079                if (identityProperties.length == 0) identityProperties = null;
080                this.childProperties.put(nameOfChild, identityProperties);
081            }
082        }
083    
084        /**
085         * Get the identity properties for the supplied child.
086         * 
087         * @param child the name of the child
088         * @return the array of identity properties for the child, or null if there are none
089         */
090        public Property[] getChildIdentityProperties( Segment child ) {
091            return this.childProperties.get(child);
092        }
093    
094        /**
095         * @return children
096         */
097        public List<Segment> getChildren() {
098            return this.children;
099        }
100    
101        /**
102         * {@inheritDoc}
103         */
104        public Path getPath() {
105            return path;
106        }
107    
108        /**
109         * {@inheritDoc}
110         */
111        public CachePolicy getCachePolicy() {
112            return cachePolicy;
113        }
114    
115        /**
116         * {@inheritDoc}
117         */
118        public DateTime getTimeLoaded() {
119            return timeLoaded;
120        }
121    
122        /**
123         * @param timeLoaded Sets timeLoaded to the specified value.
124         */
125        public void setTimeLoaded( DateTime timeLoaded ) {
126            this.timeLoaded = timeLoaded;
127        }
128    
129        /**
130         * {@inheritDoc}
131         */
132        public void setCachePolicy( CachePolicy cachePolicy ) {
133            this.cachePolicy = cachePolicy;
134        }
135    
136        /**
137         * {@inheritDoc}
138         * 
139         * @see java.lang.Object#toString()
140         */
141        @Override
142        public String toString() {
143            StringBuilder sb = new StringBuilder();
144            sb.append(this.getClass().getSimpleName());
145            sb.append(" at ");
146            sb.append(this.getPath());
147            List<Path.Segment> children = this.getChildren();
148            if (children != null && children.size() > 0) {
149                sb.append(" with ").append(children.size()).append(" children: ");
150                sb.append(StringUtil.readableString(children));
151            }
152            return sb.toString();
153        }
154    }