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.List;
028    import java.util.concurrent.CopyOnWriteArrayList;
029    import net.jcip.annotations.ThreadSafe;
030    import org.jboss.dna.connector.federation.contribution.Contribution;
031    
032    /**
033     * @author Randall Hauch
034     */
035    @ThreadSafe
036    public class MultipleContributionMergePlan extends MergePlan {
037    
038        private static final long serialVersionUID = 1L;
039        private final List<Contribution> contributions = new CopyOnWriteArrayList<Contribution>();
040    
041        /**
042         * @param contributions the contributions for this merge plan
043         */
044        /*package*/MultipleContributionMergePlan( Contribution... contributions ) {
045            assert contributions != null;
046            for (int i = 0; i != contributions.length; ++i) {
047                assert contributions[i] != null;
048                this.contributions.add(contributions[i]);
049            }
050            assert checkEachContributionIsFromDistinctSource();
051        }
052    
053        /**
054         * @param contributions the contributions for this merge plan
055         */
056        /*package*/MultipleContributionMergePlan( Iterable<Contribution> contributions ) {
057            assert contributions != null;
058            for (Contribution contribution : contributions) {
059                assert contribution != null;
060                this.contributions.add(contribution);
061            }
062            assert checkEachContributionIsFromDistinctSource();
063        }
064    
065        /**
066         * @param contributions the contributions for this merge plan
067         */
068        /*package*/MultipleContributionMergePlan( Iterator<Contribution> contributions ) {
069            assert contributions != null;
070            while (contributions.hasNext()) {
071                Contribution contribution = contributions.next();
072                assert contribution != null;
073                this.contributions.add(contribution);
074            }
075            assert checkEachContributionIsFromDistinctSource();
076        }
077    
078        /**
079         * {@inheritDoc}
080         * 
081         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionCount()
082         */
083        @Override
084        public int getContributionCount() {
085            return contributions.size();
086        }
087    
088        /**
089         * {@inheritDoc}
090         * 
091         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionFrom(java.lang.String)
092         */
093        @Override
094        public Contribution getContributionFrom( String sourceName ) {
095            for (Contribution contribution : contributions) {
096                if (contribution.getSourceName().equals(sourceName)) return contribution;
097            }
098            return null;
099        }
100    
101        /**
102         * {@inheritDoc}
103         * 
104         * @see java.lang.Iterable#iterator()
105         */
106        public Iterator<Contribution> iterator() {
107            final Iterator<Contribution> iterator = this.contributions.iterator();
108            return new Iterator<Contribution>() {
109                public boolean hasNext() {
110                    return iterator.hasNext();
111                }
112    
113                public Contribution next() {
114                    return iterator.next();
115                }
116    
117                public void remove() {
118                    throw new UnsupportedOperationException();
119                }
120            };
121        }
122    
123        /**
124         * {@inheritDoc}
125         * 
126         * @see org.jboss.dna.connector.federation.merge.MergePlan#isSource(java.lang.String)
127         */
128        @Override
129        public boolean isSource( String sourceName ) {
130            for (Contribution contribution : contributions) {
131                if (contribution.getSourceName().equals(sourceName)) return true;
132            }
133            return false;
134        }
135    
136        /**
137         * @param contribution
138         */
139        public void addContribution( Contribution contribution ) {
140            this.contributions.add(contribution);
141        }
142    
143        /**
144         * {@inheritDoc}
145         * 
146         * @see java.lang.Object#hashCode()
147         */
148        @Override
149        public int hashCode() {
150            return contributions.hashCode();
151        }
152    
153    }