1 package com4j; 2 3 import java.util.Iterator ; 4 import java.util.NoSuchElementException ; 5 6 11 final class ComCollection<T> implements Iterator <T> { 12 13 private final IEnumVARIANT e; 14 15 private Variant next; 16 17 18 21 private final Class <T> type; 22 23 ComCollection(Class <T> type, IEnumVARIANT e) { 24 this.e = e; 25 this.type = type; 26 fetch(); 27 } 28 29 public boolean hasNext() { 30 return next!=null; 31 } 32 33 public T next() { 34 if(next==null) 35 throw new NoSuchElementException (); 36 Variant v = next; 37 next = null; 38 fetch(); 39 40 Object r; 41 try { 42 if(Com4jObject.class.isAssignableFrom(type)) { 45 r = v.object((Class <? extends Com4jObject>)type); 46 } else 47 throw new UnsupportedOperationException ("I don't know how to handle "+type); 48 } finally { 49 v.clear(); 50 } 51 return (T)r; 52 } 53 54 public void remove() { 55 throw new UnsupportedOperationException (); 56 } 57 58 private void fetch() { 59 next = new Variant(); 60 int r = e.next(1,next); 61 if(r==0) { 62 next = null; 63 e.dispose(); 64 } 65 } 66 } 67 | Popular Tags |