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