1 18 19 package com.hp.hpl.jena.util.iterator; 22 23 24 import java.util.*; 27 28 36 public class ConcatenatedIterator 37 implements Iterator 38 { 39 42 43 46 47 50 51 private Iterator m_iter0 = null; 52 53 54 private Iterator m_iter1 = null; 55 56 57 protected Object m_defaultValue = null; 58 59 60 protected boolean m_defaultValueSeen = false; 61 62 63 64 67 77 public ConcatenatedIterator( Iterator iter0, Iterator iter1 ) { 78 m_iter0 = iter0; 79 m_iter1 = iter1; 80 } 81 82 83 86 92 public boolean hasNext() { 93 return m_iter0.hasNext() || m_iter1.hasNext() || (hasDefaultValue() && !m_defaultValueSeen); 94 } 95 96 97 104 public Object next() { 105 boolean next0 = m_iter0.hasNext(); 106 boolean next1 = m_iter1.hasNext(); 107 108 if (next0 || next1) { 110 Object next = (next0) ? m_iter0.next() : m_iter1.next(); 111 112 if (hasDefaultValue() && m_defaultValue.equals( next )) { 114 m_defaultValueSeen = true; 115 } 116 117 return next; 118 } 119 else if (hasDefaultValue() && !m_defaultValueSeen) { 120 m_defaultValueSeen = true; 122 return m_defaultValue; 123 } 124 else { 125 throw new NoSuchElementException( "Tried to access next() element from empty concatenated iterator" ); 127 } 128 } 129 130 131 142 public void remove() { 143 throw new UnsupportedOperationException ( "Cannot remove elements from concatenated iterator" ); 144 } 145 146 147 158 public void setDefaultValue( Object defaultValue ) { 159 m_defaultValue = defaultValue; 160 } 161 162 163 168 public boolean hasDefaultValue() { 169 return m_defaultValue != null; 170 } 171 172 173 174 177 178 182 183 } 184 185 186 215 216 | Popular Tags |