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