001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.connector.federation.merge;
023
024 import java.util.Iterator;
025 import java.util.NoSuchElementException;
026 import net.jcip.annotations.ThreadSafe;
027 import org.jboss.dna.common.util.HashCode;
028 import org.jboss.dna.connector.federation.contribution.Contribution;
029
030 /**
031 * @author Randall Hauch
032 */
033 @ThreadSafe
034 public class FiveContributionMergePlan extends MergePlan {
035
036 private static final long serialVersionUID = 1L;
037 private final Contribution contribution1;
038 private final Contribution contribution2;
039 private final Contribution contribution3;
040 private final Contribution contribution4;
041 private final Contribution contribution5;
042
043 /**
044 * @param contribution1 the first contribution for this merge plan
045 * @param contribution2 the second contribution for this merge plan
046 * @param contribution3 the third contribution for this merge plan
047 * @param contribution4 the fourth contribution for this merge plan
048 * @param contribution5 the fifth contribution for this merge plan
049 */
050 /*package*/FiveContributionMergePlan( Contribution contribution1,
051 Contribution contribution2,
052 Contribution contribution3,
053 Contribution contribution4,
054 Contribution contribution5 ) {
055 assert contribution1 != null;
056 assert contribution2 != null;
057 assert contribution3 != null;
058 assert contribution4 != null;
059 assert contribution5 != null;
060 this.contribution1 = contribution1;
061 this.contribution2 = contribution2;
062 this.contribution3 = contribution3;
063 this.contribution4 = contribution4;
064 this.contribution5 = contribution5;
065 assert checkEachContributionIsFromDistinctSource();
066 }
067
068 /**
069 * {@inheritDoc}
070 *
071 * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionCount()
072 */
073 @Override
074 public int getContributionCount() {
075 return 5;
076 }
077
078 /**
079 * {@inheritDoc}
080 *
081 * @see org.jboss.dna.connector.federation.merge.MergePlan#getContributionFrom(java.lang.String)
082 */
083 @Override
084 public Contribution getContributionFrom( String sourceName ) {
085 if (contribution1.getSourceName().equals(sourceName)) return contribution1;
086 if (contribution2.getSourceName().equals(sourceName)) return contribution2;
087 if (contribution3.getSourceName().equals(sourceName)) return contribution3;
088 if (contribution4.getSourceName().equals(sourceName)) return contribution4;
089 if (contribution5.getSourceName().equals(sourceName)) return contribution5;
090 return null;
091 }
092
093 /**
094 * {@inheritDoc}
095 *
096 * @see java.lang.Iterable#iterator()
097 */
098 public Iterator<Contribution> iterator() {
099 return new Iterator<Contribution>() {
100 private int next = 5;
101
102 public boolean hasNext() {
103 return next > 0;
104 }
105
106 @SuppressWarnings( "synthetic-access" )
107 public Contribution next() {
108 if (next == 5) {
109 next = 4;
110 return contribution1;
111 }
112 if (next == 4) {
113 next = 3;
114 return contribution2;
115 }
116 if (next == 3) {
117 next = 2;
118 return contribution3;
119 }
120 if (next == 2) {
121 next = 1;
122 return contribution4;
123 }
124 if (next == 1) {
125 next = 0;
126 return contribution5;
127 }
128 throw new NoSuchElementException();
129 }
130
131 public void remove() {
132 throw new UnsupportedOperationException();
133 }
134 };
135 }
136
137 /**
138 * {@inheritDoc}
139 *
140 * @see org.jboss.dna.connector.federation.merge.MergePlan#isSource(java.lang.String)
141 */
142 @Override
143 public boolean isSource( String sourceName ) {
144 if (contribution1.getSourceName().equals(sourceName)) return true;
145 if (contribution2.getSourceName().equals(sourceName)) return true;
146 if (contribution3.getSourceName().equals(sourceName)) return true;
147 if (contribution4.getSourceName().equals(sourceName)) return true;
148 if (contribution5.getSourceName().equals(sourceName)) return true;
149 return false;
150 }
151
152 /**
153 * {@inheritDoc}
154 *
155 * @see java.lang.Object#hashCode()
156 */
157 @Override
158 public int hashCode() {
159 return HashCode.compute(contribution1, contribution2, contribution3, contribution4, contribution5);
160 }
161
162 }