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