KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > ComCollection


1 package com4j;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.NoSuchElementException JavaDoc;
5
6 /**
7  * Wraps IEnumVARIANT and implements {@link Iterator}.
8  *
9  * @author Kohsuke Kawaguchi
10  */

11 final class ComCollection<T> implements Iterator JavaDoc<T> {
12
13     private final IEnumVARIANT e;
14
15     private Variant next;
16
17
18     /**
19      * The expected item type.
20      */

21     private final Class JavaDoc<T> type;
22
23     ComCollection(Class JavaDoc<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 JavaDoc();
36         Variant v = next;
37         next = null;
38         fetch();
39
40         Object JavaDoc r;
41         try {
42 // ideally we'd like to use ChangeVariantType to do the conversion
43
// but for now let's just support interface types
44
if(Com4jObject.class.isAssignableFrom(type)) {
45                 r = v.object((Class JavaDoc<? extends Com4jObject>)type);
46             } else
47                 throw new UnsupportedOperationException JavaDoc("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 JavaDoc();
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