jtlist.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 291 行

JAVA
291
字号


package Jt;
import java.util.*;


/**
 * Handles a list of objects.
 */


public class JtList extends JtObject {

    private static final long serialVersionUID = 1L;
    public static final String JtCLASS_NAME = JtList.class.getName(); 
    private LinkedList col = null;
    public static final String JtADD = "JtADD";
    public static final String JtCLEAR = "JtCLEAR";
    public static final String JtFIRST = "JtFIRST";
    public static final String JtLAST = "JtLAST";
    public static final String JtREMOVE_FIRST = "JtREMOVE_FIRST";
    public static final String JtREMOVE_OBJECT = "JtREMOVE_OBJECT";    

    public JtList() {
    }


    /**
     * Returns the LinkedList used to implement this class. 
     */
    public LinkedList getLinkedList () {
        return (col);
    }



    /**
     * Sets the LinkedList used to implement this class. 
     */

    public void setLinkedList (LinkedList col) {
        this.col = col;
    }

    /**
     * Returns a JtIterator.
     */

    public Object getIterator () {
        JtIterator jit;
        //Collection values;

        jit = new JtIterator ();

        if (col == null)
            return (null);
        /*
     values = col.values ();

     if (values == null)
       return (null);
         */
        jit.setIterator(col.iterator ());

        return (jit);
    }
    
    /**
     * void operation. 
     */
    public void setIterator (Object iterator) {

    }

    /**
     * Void operation.
     */

    public void setSize (int size) {
        // this.size = this.size; // void operation
    }

    /**
     * Returns the number of elements in this list.
     */ 

    public int getSize () {
        return (col != null ? col.size (): 0);
    }


    /**
     * Process object messages.
     * <ul>
     * <li> JtADD - Appends the object specified by msgContent to the end of
     * this list
     * <li> JtCLEAR - Removes all the objects from this list
     * <li> JtFIRST - Returns the first element in the list
     * <li> JtLAST - Returns the last element in the list
     * <li> JtREMOVE_FIRST - Removes and returns the first element in the list
     * <li> JtREMOVE_OBJECT - Removes the object specified by msgContent.
     * </ul>
     * @param message Jt Message
     */

    public Object processMessage (Object message) {

        String msgid = null;
        JtMessage e = (JtMessage) message;
        Object content;
        //Object data;


        if (e == null)
            return null;

        msgid = (String) e.getMsgId ();

        if (msgid == null)
            return null;

        content = e.getMsgContent();
        //data = e.getMsgData ();

        // Remove this object
        if (msgid.equals (JtObject.JtREMOVE)) {
            return (null);     
        }

        if (msgid.equals (JtList.JtADD)) {
            // Add object to the list

            if (content == null) {
                handleWarning 
                ("JtList.processMessage(JtADD):invalid content (null)");
                return (this);

            }
            if (col == null)
                col = new LinkedList ();

            col.add (content);        
            return (this);
        }     


        if (msgid.equals (JtList.JtCLEAR)) {

            if (col != null) {
                col.clear ();
            }

            return (this);
        }

        if (msgid.equals (JtList.JtFIRST)) {

            if (col == null)
                return (null);

            if (col.size () < 1) {
                return (null);
            }

            return (col.getFirst ());
        }


        if (msgid.equals (JtList.JtLAST)) {

            if (col == null)
                return (null);

            if (col.size () < 1) {
                return (null);
            }

            return (col.getLast ());
        }


        if (msgid.equals (JtList.JtREMOVE_FIRST)) {

            if (col == null)
                return (null);

            if (col.size () < 1) {
                return (null);
            }

            return (col.removeFirst ());
        }

        if (msgid.equals (JtList.JtREMOVE_OBJECT)) {
            // Remove an object from the collection

            if (content == null) {
                handleWarning 
                ("JtList.processMessage(JtREMOVE_OBJECT):invalid message content (null)");
                return (null);

            }
            if (!col.contains (content)) {
                handleError ("element not found " + content);
                return (null);
            }    
            
            col.remove (content);        
            return (null);
        } 
        
        return (super.processMessage(message));


    }


    /**
     * Demonstrates the messages processed by JtList.
     */ 

    public static void main(String[] args) {

        JtObject main = new JtObject ();
        JtMessage msg;
        JtIterator it;
        Object obj;
        Integer I;

        //main.setObjTrace (1);
        //main.setLogFile ("log.txt");


        // Create a JtList

        main.createObject (JtList.JtCLASS_NAME, "list");

        msg = (JtMessage) main.createObject (JtMessage.JtCLASS_NAME, "message");
        main.setValue (msg, "msgId", JtList.JtADD);
        main.setValue (msg, "msgContent", new Integer (1));

        // Add an object to the list

        main.sendMessage ("list", msg);


        main.setValue (msg, "msgId", JtList.JtADD);
        main.setValue (msg, "msgContent", I = new Integer (2));


        // Add object to the list

        main.sendMessage ("list", msg);

        System.out.println ("Size=" + main.getValue ("list", "size"));


        obj = (Object) main.sendMessage ("list", new JtMessage (JtList.JtFIRST));

        System.out.println ("First=" + obj);

        obj = (Object) main.sendMessage ("list", new JtMessage (JtList.JtLAST));

        System.out.println ("Last=" + obj);

        it = (JtIterator) main.getValue ("list", "iterator");

        for (;;) {
            obj = (Object) main.sendMessage (it, new JtMessage (JtIterator.JtNEXT));
            if (obj == null)
                break;
            System.out.println (obj);
        }

        main.setValue (msg, "msgId", JtList.JtREMOVE_OBJECT);
        main.setValue (msg, "msgContent", I);
        main.sendMessage ("list", msg);
        
        System.out.println ("Size=" + main.getValue ("list", "size"));        
        
        // Clear the list

        main.setValue ("message", "msgId", JtList.JtCLEAR);
        main.sendMessage ("list", "message");
        main.removeObject ("list");
    }

}


⌨️ 快捷键说明

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