jtiterator.java

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

JAVA
137
字号


package Jt;
import java.util.*;


/**
 * Iterator over the elements of a Jt collection.
 */


public class JtIterator extends JtObject {

    public static final String JtCLASS_NAME = JtIterator.class.getName(); 
    private static final long serialVersionUID = 1L;
    private transient Iterator iterator;

    // Messages

    public static final String JtNEXT = "JtNEXT";

    public JtIterator() {
    }



    /**
     * Void operation.
     */

    public void setIterator (Iterator iterator) {
        this.iterator = iterator; 
    }


    /**
     * Returns the Java Iterator.
     */

    public Iterator getIterator () {
        return (iterator);
    }


    // next: next element

    private Object next () {
        if (iterator == null) // check
            return (null);
        if (iterator.hasNext()) {
            return (iterator.next ());
        }

        return (null);
    }

    /**
     * Process object messages.
     * <ul>
     * <li>JtNEXT - returns the next element in the iteration
     * </ul> 
     */

    public Object processMessage (Object event) {

        String msgid = null;
        JtMessage e = (JtMessage) event;


        if (e == null)
            return null;

        msgid = (String) e.getMsgId ();

        if (msgid == null)
            return null;

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

        if (msgid.equals (JtIterator.JtNEXT)) {
            return (next ());
        }     

        return (super.processMessage (event));          


    }


    /**
     * Demonstrate the messages processed by JtIterator.
     */

    public static void main(String[] args) {

        JtObject factory = new JtFactory ();
        JtMessage msg;
        JtIterator it;
        Object obj;

        // Create a JtColletion

        factory.createObject (JtCollection.JtCLASS_NAME, "collection");

        msg = new JtMessage (JtCollection.JtADD);  

        // Add objects to the collection

        msg.setMsgContent (new Integer(1));
        factory.sendMessage ("collection", msg);

        msg.setMsgContent (new Integer(2));       
        factory.sendMessage ("collection", msg);

        // Retrieve the iterator associated with the collection

        it = (JtIterator) factory.getValue ("collection", "iterator");


        msg = new JtMessage (JtIterator.JtNEXT);

        // Traverse the collection using the iterator (JtNEXT message)

        while ((obj = factory.sendMessage (it, msg)) != null)
            System.out.println ("Object=" + obj);

        factory.removeObject ("collection");

    }

}


⌨️ 快捷键说明

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