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

📄 deliveryorder.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// Copyright 2002 Nokia Corporation. 
// 
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER, 
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS 
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE 
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE 
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO 
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR 
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE 
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT 
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED 
// BY THIRD PARTIES 
// 
// Furthermore, information provided in this source code is preliminary, 
// and may be changed substantially prior to final release. Nokia Corporation 
// retains the right to make changes to this source code at 
// any time, without notice. This source code is provided for informational 
// purposes only. 
// 
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
// 
// A non-exclusive, non-transferable, worldwide, limited license is hereby 
// granted to the Licensee to download, print, reproduce and modify the 
// source code. The licensee has the right to market, sell, distribute and 
// make available the source code in original or modified form only when 
// incorporated into the programs developed by the Licensee. No other 
// license, express or implied, by estoppel or otherwise, to any other 
// intellectual property rights is granted herein.


package example.delivery;

import java.util.*;
import javax.microedition.rms.*;

// The DeliveryOrder class is a singleton which wraps the 'DeliverOrder'
// record store. There are six records in that record store.
// After the records are first created in a new 'DeliveryOrder'
// record store, no new records are ever added. So the recordId's
// always have known fixed values between 1 and 6, and never 7 .. MAXINT.
// See the recordId index values defined below.


public class DeliveryOrder
{
    // Allowed status values: OPENED ... SENDERDOESNOTEXIST.
    public  static final String OPENED = "Opened"; // server-side status value
    public  static final String NOJOB = "No job";
    public  static final String ASSIGNED = "Assigned";
    public  static final String PICKEDUP = "Picked up";
    public  static final String COURIERCANCELLED = "Courier cancelled";
    public  static final String RETURNEDTOSENDER = "Returned to sender";
    public  static final String COMPLETED = "Delivery completed";
    public  static final String SENDERDOESNOTEXIST = "Sender does not exist";

    // recordId's: ID ... NOTE
    private static final int ID = 1;
    private static final int STATUS = 2;
    private static final int DESCRIPTION = 3;
    private static final int SENDERADDRESS = 4;
    private static final int RECIPIENTADDRESS = 5;
    private static final int NOTE = 6;
    private static final byte[] empty = new byte[0];
    public  static final String RSNAME = "DeliveryOrder";
    private static RecordStore rs = null;
    private static DeliveryOrder instance = null;


    // The unique singleton instance of this class.

    static DeliveryOrder getInstance()
        throws RecordStoreException
    {
        if (null == instance)
        {
           instance = new DeliveryOrder();
        }
        return instance;
    }


    private DeliveryOrder()
        throws RecordStoreException
    {
        if (rs == null)
        {
            openRecordStore();
            if (rs.getNumRecords() == 0)
            {
                init();
            }
        }
    }


    static void openRecordStore()
        throws RecordStoreException
    {
        // At most, one rs should ever be open.
        if (rs == null)
        {
            rs = RecordStore.openRecordStore(RSNAME, true);
        }
    }


    static void closeRecordStore()
        throws RecordStoreException
    {
        if (rs != null)
        {
            rs.closeRecordStore();
            rs = null;
        }
    }


    // Method newRecordStore can also be used to delete all records.

    private void newRecordStore()
        throws RecordStoreException
    {
        // Note: To perform a deleteRecordStore, rs should first be closed as
        //       many times as it was opened. In this class, that's at most once.

        closeRecordStore();
        try
        {
            RecordStore.deleteRecordStore(RSNAME);
        }
        catch(RecordStoreNotFoundException e)
        {
            // It may not exist the first time newRecordStore is called.
        }
        openRecordStore();
    }


    void init()
        throws RecordStoreException
    {
        init("", NOJOB, "", "", "", "");
    }


    void init(String id,
              String status,
              String description,
              String senderAddress,
              String recipientAddress,
              String note)
        throws RecordStoreException
    {
        newRecordStore();
        addRecord(id);               // recordId ID (1)
        addRecord(status);           // recordId STATUS (2)
        addRecord(description);      // recordId DESCRIPTION (3)
        addRecord(senderAddress);    // recordId SENDERADDRESS (4)
        addRecord(recipientAddress); // recordId RECIPIENTADDRESS (5)
        addRecord(note);             // recordId NOTE (6)
    }


    boolean isStatusCourierModifyable()
        throws RecordStoreException
    {
        String status = getStatus();
        return (status.equals(DeliveryOrder.ASSIGNED) ||
                status.equals(DeliveryOrder.PICKEDUP));
    }


    String getId()
        throws RecordStoreException
    {
        return getRecord(ID);
    }


    void setId(String str)
        throws RecordStoreException
    {
        setRecord(ID, str);
    }


    String getStatus()
        throws RecordStoreException
    {
        byte[] barray = rs.getRecord(STATUS);
        if (barray == null || barray.length == 0)
        {
            return NOJOB;
        }
        else
        {
            return new String(barray);
        }
    }


    void setStatus(String str)
        throws SetStatusException, RecordStoreException
    {
        if (isValidStatus(str))
        {
            setRecord(STATUS, str);
        }
        else
        {
            SetStatusException excp =
                new SetStatusException("invalid status: '" + str + "'");
            throw excp;
        }
    }


    private boolean isValidStatus(String status)
    {
        boolean isValid = (status != null) &&
                          (status.equals(NOJOB) ||
                           status.equals(OPENED) ||
                           status.equals(ASSIGNED) ||
                           status.equals(PICKEDUP) ||
                           status.equals(COURIERCANCELLED) ||
                           status.equals(RETURNEDTOSENDER) ||
                           status.equals(SENDERDOESNOTEXIST) ||
                           status.equals(COMPLETED));
        return isValid;
    }


    String getDescription()
        throws RecordStoreException
    {
        return getRecord(DESCRIPTION);
    }


    void setDescription(String str)
        throws RecordStoreException
    {
        setRecord(DESCRIPTION, str);
    }


    String getRecipientAddress()
        throws RecordStoreException
    {
        return getRecord(RECIPIENTADDRESS);
    }


    void setRecipientAddress(String str)
        throws RecordStoreException
    {
        setRecord(RECIPIENTADDRESS, str);
    }


    String getSenderAddress()
        throws RecordStoreException
    {
        return getRecord(SENDERADDRESS);
    }


    void setSenderAddress(String str)
        throws RecordStoreException
    {
        setRecord(SENDERADDRESS, str);
    }


    String getNote()
        throws RecordStoreException
    {
        return getRecord(NOTE);
    }


    void setNote(String str)
        throws RecordStoreException
    {
        setRecord(NOTE, str);
    }


    private void addRecord(String str)
        throws RecordStoreException
    {
        if (str == null || str.length() == 0)
        {
            rs.addRecord(null, 0, 0);
        }
        else
        {
            rs.addRecord(str.getBytes(), 0, str.length());
        }
    }


    String getRecord(int ix)
        throws RecordStoreException
    {
        String str = null;
        byte[] barray = rs.getRecord(ix);
        if (barray == null || barray.length == 0)
        {
            str = "";
        }
        else
        {
            str = new String(barray);
        }
        return str;
    }


    private void setRecord(int ix, String str)
        throws RecordStoreException
    {
        if (str == null || str.length() == 0)
        {
            rs.setRecord(ix, empty, 0, 0);
        }
        else
        {
            rs.setRecord(ix, str.getBytes(), 0, str.length());
        }
    }
}

⌨️ 快捷键说明

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