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