⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.java

📁 j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件
💻 JAVA
字号:
package com.jmobilecore.dstore;

import javax.microedition.rms.*;

/**
 * An abstract base class for record stores
 *
 * @author 	Greg Gridin
 */
abstract public class Table {

    /**
     * The invalid record id
     **/
    static public final int INVALID_RECORD_ID = -1;

    /**
     * The <code>RecordStructure</code> for the current <code>Record</code>
     **/
    public RecordStructure structure;

    /**
     * Indicates whether the RMStorage is open
     **/
    protected boolean isOpen = false;

    /**
     * The  <code>RecordStore</code> for storing table data
     **/
    public RecordStore recordStore;

    /**
     *  The <code>RecordFilter</code> for data loading
     **/
    public RecordFilter loadFilter;

    /**
     *  The <code>RecordComparator</code> for data loading
     **/
    public RecordComparator loadComparator;

    /**
     *  The <code>keepUpdated</code> flag for data loading
     **/
    public boolean loadKeepUpdated;

    /**
     *  Creates new instance of <code>Table</code> class and opens data store
     * @param  name the MIDlet suite unique name for the table, not to exceed 32 characters
     * @param  create if true, the record store will be created if necessary.
     **/
    public Table(String name, boolean create) {
        open(name, create);
    }

    /**
     * Opens data store
     * @param  name the MIDlet suite unique name for the table, not to exceed 32 characters
     * @param  create if true, the record store will be created if necessary.
     * @return <code>true</code> if operation was successful, <code>false</code> otherwise
     **/
    public boolean open(String name, boolean create) {

        isOpen = false;
        try {
          recordStore = RecordStore.openRecordStore(name, create);
          isOpen = true;
        }
        catch (Exception ignored) {
        }
        return isOpen;
    }

    /**
     * Closes the data store. This method does not destroy the <code>Table</code> class.
     * Record structure and other table related information continue to be valid.
     * You can reopen data storage by calling method <code>open</code>
     * @return <code>true</code> if operation was successful, <code>false</code> otherwise
     * @see #open(String, boolean)
     **/
    public boolean close() {

        isOpen = false;
        try {
          recordStore.closeRecordStore();
        }
        catch (Exception ignored) {
            return false;
        }
        return true;
    }

    /**
     * Returns the number of records currently in the record store.
     * The record store should be open. If record store is not open returns <code>-1</code>.
     * @see #isOpen
     * @see #open(java.lang.String, boolean)
     */
    public int getNumRecords() {
        try {
            return recordStore.getNumRecords();
        } catch (Exception ignored) {
            return INVALID_RECORD_ID;
        }
    }

    /**
     * Adds new record to data store
     * The record store should be open. If record store is not open returns <code>INVALID_RECORD_ID</code>.
     * @param rec The <code>Record</code> to be added
     * @return the record id of added record. If record cannot be added returns <code>INVALID_RECORD_ID</code>.
     *
     * @see #INVALID_RECORD_ID
     * @see #open(java.lang.String, boolean)
     * @see Record
     */
    public int addRecord(Record rec) {
        try {
            return recordStore.addRecord(rec.data, 0, rec.length);
        } catch (Exception ignored) {
            return INVALID_RECORD_ID;
        }
    }

    /**
     * Sets data to the specified record
     * The record store should be open. If record store is not open returns <code>false</code>.
     * @param id The id of the record to use in this operation.
     * @param rec The <code>Record</code> to be set
     * @return <code>true</code> if update was successful, <code>false</code> otherwise
     *
     * @see #addRecord(Record)
     * @see Record
     */
    public boolean setRecord(int id, Record rec) {
        try {
            recordStore.setRecord(id, rec.data, 0, rec.length);
            return true;
        } catch (Exception ignored) {
            return false;
        }
    }

    /**
     * Deletes data from the table
     * The record store should be open. If record store is not open returns <code>false</code>.
     * @param id The id of the record to use in this operation.
     * @return <code>true</code> if deletion was successful, <code>false</code> otherwise
     *
     * @see Record
     */
    public boolean deleteRecord(int id) {
        try {
            recordStore.deleteRecord(id);
            return true;
        } catch (Exception ignored) {
            return false;
        }
    }

    /**
     * Abstract method for processing record of current <code>Table</code>
     * @param data The binary data representing record used in this operation
     * @param recId The id of the record to use in this operation.
     * @return <code>true</code> if record processing should continue,
     * <code>false</code> if record processing should be stopped
     */
    abstract public boolean processRecord(byte[] data, int recId);

    /**
     * Iterates through records in the record store
     * @return <code>true</code> if operation was successful, <code>false</code> otherwise
     */
    public boolean load() {

        try {
            RecordEnumeration recEnum = recordStore.enumerateRecords(loadFilter, loadComparator, loadKeepUpdated);
            while (recEnum.hasNextElement()) {
                int recId = recEnum.nextRecordId();
                if (!processRecord(recordStore.getRecord(recId), recId))
                    break;
            }
            recEnum.destroy();
        } catch (Exception ignored) {
            return false;
        }
        return true;
    }

    /**
     * Deletes the named record store.
     * @param name Name the MIDlet suite unique name for the table, not to exceed 32 characters
     * @return <code>true</code> if operation was successful, <code>false</code> otherwise
     */
    static public boolean delete(String name) {

        try {
          RecordStore.deleteRecordStore(name);
        }
        catch(Exception ignored) {
            return false;
        }
        return true;
    }
}

⌨️ 快捷键说明

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