JavaのFor-eachが使えるようにDocumentCollectionを拡張する

標準のDocumentCollectionクラスはIterableインタフェースが実装されておらず、for-each構文が使えないため、Collection内の全文書に対して処理を実施したい場合、while(doc != null)とgetNextDocument(doc)を組み合わせる必要がある。

Iteratableインターフェースを実装したクラスを定義しfor-eachが使えるようにしておけば便利である。

実装はViewインスタンスもDocumentCollectionのように扱えるWrapperを用意しそのクラスを使ってIterableなコレクションクラスを実現している。

以下ソースコード

DocumentCollectionWrapperクラス
package hogehoge;

import lotus.domino.*;

public final class DocumentCollectionWrapper {
    private DocumentCollection _m_dc;
    private View _m_vw;
    private Document _m_currentDoc;
    private Document _m_prevDoc;
    private boolean _m_isView;
   
    public DocumentCollectionWrapper(DocumentCollection dc) {
        // TODO Auto-generated constructor stub
        _m_dc = dc;
        _m_isView = false;
    }
    public DocumentCollectionWrapper(View vw){
        _m_vw = vw;
        _m_isView = true;
    }
   
    public Document getFirstDoc() throws Exception{
        _m_currentDoc = (_m_isView)?
            _m_vw.getFirstDocument(): _m_dc.getFirstDocument();
       
        prevDocRecycle();
       
        return _m_currentDoc;
    }
    public Document getLastDoc() throws Exception{
        _m_currentDoc = (_m_isView)?
            _m_vw.getLastDocument(): _m_dc.getLastDocument();
       
        prevDocRecycle();
        return _m_currentDoc;
    }
   
    public Document getNextDoc() throws Exception{
        _m_currentDoc = (_m_isView)?
            _m_vw.getNextDocument(_m_currentDoc): _m_dc.getNextDocument();

        prevDocRecycle();
        return _m_currentDoc;
    }
    public Document getPrevDoc() throws Exception{
        _m_currentDoc = (_m_isView)?
            _m_vw.getPrevDocument(_m_currentDoc): _m_dc.getPrevDocument();

        prevDocRecycle();
        return _m_currentDoc;
    }
    public Document getNthDoc(int n) throws Exception{
        _m_currentDoc = (_m_isView)?
            _m_vw.getNthDocument(n): _m_dc.getNthDocument(n);
           
        prevDocRecycle();
        return _m_currentDoc;
    }
    public void destruction() throws Exception{
        prevDocRecycle();
        if(_m_isView){
            _m_vw.recycle();
        }
        else{
            _m_dc.recycle();
        }
    }
    protected void finalize()throws Throwable{
        destruction();
    }   
    private void prevDocRecycle() throws Exception{
        if(_m_prevDoc != null){
            _m_prevDoc.recycle();
        }
        _m_prevDoc = _m_currentDoc;
    }
   
}

IterableNotesDocCollectionクラス
package hogehoge;

import lotus.domino.*;


public final class IterableNotesDocCollection
implements implements Iterable<documen>, java.util.Iterator<document> {
    private DocumentCollectionWrapper _m_dcw;
    private lotus.domino.Document _m_nextDoc;
   
   
    public IterableNotesDocCollection(lotus.domino.DocumentCollection dc)
    throws Throwable{
        _m_dcw = new DocumentCollectionWrapper(dc);
    }
    public IterableNotesDocCollection(lotus.domino.View vw)
    throws Throwable{
        _m_dcw = new DocumentCollectionWrapper(vw);
    }
   
    public java.util.Iterator<document> iterator(){
        return this;
    }
    public boolean hasNext(){
        try{
            _m_nextDoc = (_m_nextDoc == null)?
                _m_dcw.getFirstDoc(): _m_dcw.getNextDoc();

            return _m_nextDoc != null;
        }
        catch(Throwable e){
            return false;
        }
    }
    public lotus.domino.Document next(){
        if(_m_nextDoc == null){
            throw new java.util.NoSuchElementException();   
        }
        return _m_nextDoc;
    }
    public void remove(){
        throw new UnsupportedOperationException();
    }
   
    public void destruction() throws Exception{
        _m_dcw.destruction();
    }
   
    protected void finalize()throws Throwable{
        destruction();
    }
}

使用例:
//vw: lotus.domino.Viewインスタンス

IterableNotesDocCollection dc = new IterableNotesDocCollection(vw);

Integer i = 0;
for(Document doc: dc){
  //ビューの全文書についてUNIDを表示する。
  System.out.println( ++i + ": Document UNID: " + doc.getUniversalID());
}