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

📄 clocked.java

📁 用java写的jt-jpeg。jt表示java time package
💻 JAVA
字号:
/*
 |
 | Clocked.java
 |
 | Clocked 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.clocked;
import jcp.*;
import java.util.Vector;

/**
 *  Clocked objects are components that react only to a
 *  prespecified "clock" input. The go() method is not executed
 *  until the clock input is received. An signal is emitted on the last
 *  output when the go() method completes.
 *  The last input is the clock input.

 * @author James Shin Young
 */

public abstract class Clocked extends SynchComponent
{

    // Default size of internal event queue.
    private static int DEFAULT_QUEUE_SIZE = 4;

    // Maximum size of internal event queue.
    private static int MAX_QUEUE_SIZE = 64;

    // The number of slots by which to increase the event queue size when
    // needed.
    private static int QUEUE_INCREMENT = 2;

    // The clock inport
    private Port clockPort;

    // Event queue
    private EventQueue queue;

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

    public Clocked() {
        queue = new EventQueue(DEFAULT_QUEUE_SIZE);
        clockPort = super.addPort(true,"clk");
    }

    public final void go(Port port) {

        // Activate only on the clock inport
        if (port == clockPort) {
            // clock == true -> compute
            if (port.signal() == Signal.TRUE) {
                run();
            } else if (port.signal() == Signal.FALSE) {
            // clock == false -> update output
                updateOutputs();
            }
        }
    }

    /**
     *  Executed every time a clock signal is received by the component.
     *  Should be overridden to do something interesting.
     */
    public abstract void run();

    /**
     *  Emits a signal to a port.
     *  @param sig  The signal to be sent.
     *  @param port The destination port.
     *  @return true if signal successfully emitted.
     */
    public boolean emit(Object sig, Port port) {
        // Instead of emitting the signal immediately to the port,
        // add a event to the queue

        if (queue.full()) {
            if ((queue.capacity() - QUEUE_INCREMENT) < MAX_QUEUE_SIZE) {
                JTSystem.println("Increased queue size on clocked component "+
                                  this.toString());
                queue.increaseSize(QUEUE_INCREMENT);
            } else {
                JTSystem.error("Event queue on synchronous component: "+
                                this.toString()+
                                " reached maximum.");
            }
        }

        return queue.add(new Event(sig,port));

    }

    private void updateOutputs() {
        // Process the events stored in the queue.
        while(!queue.empty()) {
            Event e = queue.get();
            super.emit(e.signal(),e.port());
        }
    }
}

⌨️ 快捷键说明

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