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.common.util.HashCode;
030    import org.jboss.dna.connector.federation.contribution.Contribution;
031    
032    /**
033     * @author Randall Hauch
034     */
035    @ThreadSafe
036    public class FiveContributionMergePlan extends MergePlan {
037    
038        private static final long serialVersionUID = 1L;
039        private final Contribution contribution1;
040        private final Contribution contribution2;
041        private final Contribution contribution3;
042        private final Contribution contribution4;
043        private final Contribution contribution5;
044    
045        /**
046         * @param contribution1 the first contribution for this merge plan
047         * @param contribution2 the second contribution for this merge plan
048         * @param contribution3 the third contribution for this merge plan
049         * @param contribution4 the fourth contribution for this merge plan
050         * @param contribution5 the fifth contribution for this merge plan
051         */
052        /*package*/FiveContributionMergePlan( Contribution contribution1,
053                                               Contribution contribution2,
054                                               Contribution contribution3,
055                                               Contribution contribution4,
056                                               Contribution contribution5 ) {
057            assert contribution1 != null;
058            assert contribution2 != null;
059            assert contribution3 != null;
060            assert contribution4 != null;
061            assert contribution5 != null;
062            this.contribution1 = contribution1;
063            this.contribution2 = contribution2;
064            this.contribution3 = contribution3;
065            this.contribution4 = contribution4;
066            this.contribution5 = contribution5;
067            assert checkEachContributionIsFromDistinctSource();
068        }
069    
070        /**
071         * {@inheritDoc}
072         * 
073         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionCount()
074         */
075        @Override
076        public int getContributionCount() {
077            return 5;
078        }
079    
080        /**
081         * {@inheritDoc}
082         * 
083         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionFrom(java.lang.String)
084         */
085        @Override
086        public Contribution getContributionFrom( String sourceName ) {
087            if (contribution1.getSourceName().equals(sourceName)) return contribution1;
088            if (contribution2.getSourceName().equals(sourceName)) return contribution2;
089            if (contribution3.getSourceName().equals(sourceName)) return contribution3;
090            if (contribution4.getSourceName().equals(sourceName)) return contribution4;
091            if (contribution5.getSourceName().equals(sourceName)) return contribution5;
092            return null;
093        }
094    
095        /**
096         * {@inheritDoc}
097         * 
098         * @see java.lang.Iterable#iterator()
099         */
100        public Iterator<Contribution> iterator() {
101            return new Iterator<Contribution>() {
102                private int next = 5;
103    
104                public boolean hasNext() {
105                    return next > 0;
106                }
107    
108                @SuppressWarnings( "synthetic-access" )
109                public Contribution next() {
110                    if (next == 5) {
111                        next = 4;
112                        return contribution1;
113                    }
114                    if (next == 4) {
115                        next = 3;
116                        return contribution2;
117                    }
118                    if (next == 3) {
119                        next = 2;
120                        return contribution3;
121                    }
122                    if (next == 2) {
123                        next = 1;
124                        return contribution4;
125                    }
126                    if (next == 1) {
127                        next = 0;
128                        return contribution5;
129                    }
130                    throw new NoSuchElementException();
131                }
132    
133                public void remove() {
134                    throw new UnsupportedOperationException();
135                }
136            };
137        }
138    
139        /**
140         * {@inheritDoc}
141         * 
142         * @see org.jboss.dna.connector.federation.merge.MergePlan#isSource(java.lang.String)
143         */
144        @Override
145        public boolean isSource( String sourceName ) {
146            if (contribution1.getSourceName().equals(sourceName)) return true;
147            if (contribution2.getSourceName().equals(sourceName)) return true;
148            if (contribution3.getSourceName().equals(sourceName)) return true;
149            if (contribution4.getSourceName().equals(sourceName)) return true;
150            if (contribution5.getSourceName().equals(sourceName)) return true;
151            return false;
152        }
153    
154        /**
155         * {@inheritDoc}
156         * 
157         * @see java.lang.Object#hashCode()
158         */
159        @Override
160        public int hashCode() {
161            return HashCode.compute(contribution1, contribution2, contribution3, contribution4, contribution5);
162        }
163    
164    }