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