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