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 TwoContributionMergePlan extends MergePlan {
037    
038        private static final long serialVersionUID = 1L;
039        private final Contribution contribution1;
040        private final Contribution contribution2;
041    
042        /**
043         * @param contribution1 the first contribution for this merge plan
044         * @param contribution2 the second contribution for this merge plan
045         */
046        /*package*/TwoContributionMergePlan( Contribution contribution1,
047                                              Contribution contribution2 ) {
048            assert contribution1 != null;
049            assert contribution2 != null;
050            this.contribution1 = contribution1;
051            this.contribution2 = contribution2;
052            assert checkEachContributionIsFromDistinctSource();
053        }
054    
055        /**
056         * {@inheritDoc}
057         * 
058         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionCount()
059         */
060        @Override
061        public int getContributionCount() {
062            return 2;
063        }
064    
065        /**
066         * {@inheritDoc}
067         * 
068         * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionFrom(java.lang.String)
069         */
070        @Override
071        public Contribution getContributionFrom( String sourceName ) {
072            if (contribution1.getSourceName().equals(sourceName)) return contribution1;
073            if (contribution2.getSourceName().equals(sourceName)) return contribution2;
074            return null;
075        }
076    
077        /**
078         * {@inheritDoc}
079         * 
080         * @see java.lang.Iterable#iterator()
081         */
082        public Iterator<Contribution> iterator() {
083            return new Iterator<Contribution>() {
084                private int next = 2;
085    
086                public boolean hasNext() {
087                    return next > 0;
088                }
089    
090                @SuppressWarnings( "synthetic-access" )
091                public Contribution next() {
092                    if (next == 2) {
093                        next = 1;
094                        return contribution1;
095                    }
096                    if (next == 1) {
097                        next = 0;
098                        return contribution2;
099                    }
100                    throw new NoSuchElementException();
101                }
102    
103                public void remove() {
104                    throw new UnsupportedOperationException();
105                }
106            };
107        }
108    
109        /**
110         * {@inheritDoc}
111         * 
112         * @see org.jboss.dna.connector.federation.merge.MergePlan#isSource(java.lang.String)
113         */
114        @Override
115        public boolean isSource( String sourceName ) {
116            return contribution1.getSourceName().equals(sourceName) || contribution2.getSourceName().equals(sourceName);
117        }
118    
119        /**
120         * {@inheritDoc}
121         * 
122         * @see java.lang.Object#hashCode()
123         */
124        @Override
125        public int hashCode() {
126            return HashCode.compute(contribution1, contribution2);
127        }
128    
129    }