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     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed 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.connector.federation.merge;
025    
026    import java.util.Iterator;
027    import java.util.NoSuchElementException;
028    import net.jcip.annotations.ThreadSafe;
029    import org.jboss.dna.connector.federation.contribution.Contribution;
030    
031    /**
032     * @author Randall Hauch
033     */
034    @ThreadSafe
035    public class OneContributionMergePlan extends MergePlan {
036    
037        private static final long serialVersionUID = 1L;
038        private final Contribution contribution;
039    
040        /**
041         * @param contribution the contribution for this merge plan
042         */
043        /*package*/OneContributionMergePlan( Contribution contribution ) {
044            assert contribution != null;
045            this.contribution = contribution;
046        }
047    
048        /**
049         * {@inheritDoc}
050         * 
051         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionCount()
052         */
053        @Override
054        public int getContributionCount() {
055            return 1;
056        }
057    
058        /**
059         * {@inheritDoc}
060         * 
061         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionFrom(java.lang.String)
062         */
063        @Override
064        public Contribution getContributionFrom( String sourceName ) {
065            return isSource(sourceName) ? contribution : null;
066        }
067    
068        /**
069         * {@inheritDoc}
070         * 
071         * @see java.lang.Iterable#iterator()
072         */
073        public Iterator<Contribution> iterator() {
074            return new Iterator<Contribution>() {
075                private boolean next = true;
076    
077                public boolean hasNext() {
078                    return next;
079                }
080    
081                @SuppressWarnings( "synthetic-access" )
082                public Contribution next() {
083                    if (next) {
084                        next = false;
085                        return contribution;
086                    }
087                    throw new NoSuchElementException();
088                }
089    
090                public void remove() {
091                    throw new UnsupportedOperationException();
092                }
093            };
094        }
095    
096        /**
097         * {@inheritDoc}
098         * 
099         * @see org.jboss.dna.connector.federation.merge.MergePlan#isSource(java.lang.String)
100         */
101        @Override
102        public boolean isSource( String sourceName ) {
103            return contribution.getSourceName().equals(sourceName);
104        }
105    
106        /**
107         * {@inheritDoc}
108         * 
109         * @see java.lang.Object#hashCode()
110         */
111        @Override
112        public int hashCode() {
113            return contribution.hashCode();
114        }
115    
116    }