objectenumeration.java

来自「SyncML的java实现类库 funambol公司发布的」· Java 代码 · 共 117 行

JAVA
117
字号
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.funambol.storage;

import com.funambol.util.Log;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * Useful class to enumerate sorted and filtered contact records
 */
public class ObjectEnumeration implements Enumeration {
    int index = 0;
    byte[] filter = null;
    RecordStore rs = null;
    Serializable s = null;
    RecordEnumeration re = null;
    int size = 0;
    
    public ObjectEnumeration() {
    }
    
    /**
     * Constructor
     */
    public ObjectEnumeration(RecordStore refrs, ObjectFilter rf,
            ObjectComparator rc, Serializable obj) {
        //int recordIndex = 0;
        this.rs = refrs;
        this.s = obj;
        try {
            this.re = rs.enumerateRecords((RecordFilter)rf,
                    (RecordComparator) rc, false);
            this.size = re.numRecords();
        } catch (RecordStoreNotOpenException ex) {
            Log.debug(this,"objectEnumeration constructor: " +
                    "RecordStoreBotFoundException ");
            ex.printStackTrace();
        }
    }
    
    /**
     * Enumeration interface implemented method
     * @return true if enumeration has more elements
     */
    public boolean hasMoreElements() {
        return this.re.hasNextElement();
    }
    
    /**
     * Enumeration interface implemented method
     * @return Object to fill enumeration
     */
    public Object nextElement() {
        ByteArrayInputStream dataStream = null;
        DataInputStream in = null;
        try {
            int recordIndex = re.nextRecordId();
            
            dataStream = new ByteArrayInputStream(rs.getRecord(recordIndex));
            Serializable so;
            so = (Serializable) s.getClass().newInstance();
            
            return new Serialized(new DataInputStream(dataStream), so, recordIndex);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            Log.error(this, "nextElement() RecordStoreException ");
        } catch (IOException ex) {
            Log.error(this, "nextElement() IOException ");
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            Log.error(this, "nextElement() InstantittionException ");
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            Log.error(this, "nextElement() IllegalAccessException ");
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * return the number of objects within the enumeration
     * @return size is the number of objects into this enumeration
     */
    public int getSize() {
        return this.size;
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?