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

📄 component.java

📁 用java写的jt-jpeg。jt表示java time package
💻 JAVA
字号:
/*
 |
 | Component.java
 |
 | Component class
 | James Shin Young
 |
 | Created:  December 12, 1997
 |
 | Copyright (c) 1997 by James Shin Young and the Regents
 | of the University of California.  All rights reserved.
 |
 | Permission to use, copy, modify, and distribute this software
 | and its documentation for NON-COMMERCIAL purposes and without
 | fee is hereby granted provided that this copyright notice
 | appears in all copies.
 |
 */

package jcp;
import java.awt.Menu;
import java.util.Vector;
import java.util.Enumeration;

/**
 *  This is the basic building block of JavaTime systems, which are
 *  collections of components that interact through ports.
 *
 *  The go(Port) method, whose implementation is to be defined by its
 *  subclasses is invoked whenever a new value is assigned to one
 *  of the component's active ports. It is not executed if the new
 *  signal represents the same value as the previous signal, as
 *  determined by the equals() method of the signal.
 *
 *  @author James Shin Young
 *  @see AsynchComponent
 *  @see SynchComponent
 *  @see Signal
 *  @see Port
 */
public class Component
{

///////////////////////////////////////////////////////////////////////////////
//* Private variables

    // Vector of the component's ports
    private Vector ports;

    // Initial size for ports vector
    private final static int defaultPorts = 2;

    // This component's name
    private String name;

    // The network this component is contained within, if any.
    private Network container;

    // Is the component enabled?
    private boolean enabled;

///////////////////////////////////////////////////////////////////////////////
//* Constructors

    /**
     *  Creates a component with the specified name.
     *  @param name The name of the component
     */
    public Component(String name) {
        setContainer(null); //michaels
        ports = new Vector(defaultPorts);

        if (name != null) {
            this.name = name;
        } else {
            name = super.toString();
        }

        enabled = true;
    }

    /**
     *  Creates a no-name component.
     */
    public Component() {
        this("");
    }

///////////////////////////////////////////////////////////////////////////////
//* Public methods

    /**
     *  Sends a signal to the specified port.
     *  @param sig      The signal, which can be a handle to any Java object.
     *  @param port     The target port.
     *  @return true if the signal was successfully received by all the
     *  port's receivers.
     */
    public boolean emit(Object sig, Port port) {
        // Make sure the port is actually owned by this component.
        if (port.owner() == this) {
            return port.emit(sig);
        } else {
            JTSystem.error("Port "+port.toString()+" is not owned by component "+
                           this.toString());
            return false;
        }
    }

    /**
     *  Sends a signal to the component's port at a given index.
     *  @param index    The index of the port the signal is to be
     *                  sent to.
     */
    public boolean emit(Object sig, int index) {
        try {
            return emit(sig,((Port)ports.elementAt(index)));
        } catch (ArrayIndexOutOfBoundsException e) {
            JTSystem.error("Port "+index+" does not exist in component "+
                           this.toString());
        }

        return false;
    }

    /**
     *  Sends a signal to the component's port with the specified name.
     *  If two ports have the same name, the signal will be sent to the
     *  port with the lower index.
     *  @param name     The name of the target port.
     */
    public boolean emit(Object sig, String name) {
        Port p = this.port(name);
        if (p != null) {
            return emit(sig,p);
        } else {
            JTSystem.error("Port named "+name+" does not exist in component "+
                           this.toString());
            return false;
        }
    }

    public static void connect(Port source, Port receiver) {
        Port.connect(source,receiver);
    }

    public static void disconnect(Port source, Port receiver) {
        Port.disconnect(source,receiver);
    }

    /**
     *  Returns the number of ports the component has.
     */
    public int ports() {
        if (ports != null) {
            return ports.size();
        } else {
            return 0;
        }
    }

    /**
     *  Returns the component's ports as an enumeration.
     */
    public Enumeration portEnum() {
        return ports.elements();
    }

    /**
     *  Returns the index number of a port.
     *  @return The index of the specified input port. -1 if not found.
     */
    public int portIndex(Port port) {
        for (int i=0; i < ports.size(); i++) {
            if (ports.elementAt(i) == port) {
                return i;
            }
        }
        return -1;
    }

    /**
     *  Returns the port with the given name.
     */
    public Port port(String name) {
        for (int i=0; i < ports.size(); i++) {
            Port p = port(i);
            if (p.name().equals(name)) {
                return p;
            }
        }
        return null;
    }

    /**
     *  Returns the port at the specified index.
     *  @return The port at the given index. Null if index is out
     *  of range.
     */
    public Port port(int index) {
        try {
            return (Port)ports.elementAt(index);
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
    }

    //---- Methods used for controlling the "simulation".. ----//

    /**
     *  Enable the component.
     */
    public void enable() {
        enabled = true;
    }

    /**
     *  Disable the component.
     */
    public void disable() {
        enabled = false;
    }

    /**
     *  Reset the component.
     */
    public void reset() {
        if (ports != null) {
            for (int i=0; i < ports.size(); i++) {
                ((Port)ports.elementAt(i)).reset();
            }
        }
    }

    /**
     *  Returns the component's name.
     */
    public String name() { return name; }

    /**
     *  Returns the Network that the component is contained within, if any.
     *  Otherwise, returns null.
     */
    public Network container() { return container; }

    /**
     *  Sets the network containing this component.
     */
    public void setContainer(Network container) {
        this.container = container;
    }

    /**
     *  This method is called whenever a new value is written to one of the
     *  component's input ports. Should be overridden so that the component
     *  does something interesting.
     *  @param port The port whose value change activated the component.
     */
    public void go(Port port) { return; }

///////////////////////////////////////////////////////////////////////////////
//* Protected methods

    /**
     *  Adds a new port to the component.
     *  @param active   true to add an active port, false to add an inactive port.
     */
    protected Port addPort(boolean active, String name) {
        Port port = new Port(this,name,null,active);
        ports.addElement(port);
        return port;
    }

    /**
     *  Adds an anonymous port to the component.
     */
    protected Port addPort(boolean active) {
        return addPort(active,"");
    }

    /**
     *  Removes the specified port. The port is disconnected from all
     *  its sources and receivers.
     */
    protected void removePort(Port p) {
        p.disconnectAll();
        ports.removeElement(p);
    }

    /**
     *  Sets the component's name.
     */
    protected void setName(String n) { name = n; }

    /**
     *  This method is executed whenever a new signal is received on an
     *  active port.
     */
    protected synchronized boolean activate(Port port, Object signal) {

        if (enabled) {
            // Execute the go method only on signal *changes*.
            if (!port.signal().equals(signal)) {
                port.updateSignal(signal);
                this.go(port);
            }
            return true;
        } else {
            return false;
        }
    }
}

⌨️ 快捷键说明

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