jdk/src/share/classes/com/sun/media/sound/EventDispatcher.java
changeset 18215 b2afd66ce6db
parent 5506 202f599c92aa
child 24548 9c007a986347
equal deleted inserted replaced
18214:e9eff0f1f1df 18215:b2afd66ce6db
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.media.sound;
    26 package com.sun.media.sound;
    27 
    27 
    28 import java.util.EventObject;
       
    29 import java.util.ArrayList;
    28 import java.util.ArrayList;
    30 import java.util.List;
    29 import java.util.List;
    31 
    30 
    32 import javax.sound.sampled.Clip;
    31 import javax.sound.midi.ControllerEventListener;
    33 import javax.sound.sampled.Line;
    32 import javax.sound.midi.MetaEventListener;
       
    33 import javax.sound.midi.MetaMessage;
       
    34 import javax.sound.midi.ShortMessage;
    34 import javax.sound.sampled.LineEvent;
    35 import javax.sound.sampled.LineEvent;
    35 import javax.sound.sampled.LineListener;
    36 import javax.sound.sampled.LineListener;
    36 
       
    37 import javax.sound.midi.MetaMessage;
       
    38 import javax.sound.midi.ShortMessage;
       
    39 import javax.sound.midi.MetaEventListener;
       
    40 import javax.sound.midi.ControllerEventListener;
       
    41 
    37 
    42 
    38 
    43 
    39 
    44 /**
    40 /**
    45  * EventDispatcher.  Used by various classes in the Java Sound implementation
    41  * EventDispatcher.  Used by various classes in the Java Sound implementation
    47  *
    43  *
    48  * @author David Rivas
    44  * @author David Rivas
    49  * @author Kara Kytle
    45  * @author Kara Kytle
    50  * @author Florian Bomers
    46  * @author Florian Bomers
    51  */
    47  */
    52 class EventDispatcher implements Runnable {
    48 final class EventDispatcher implements Runnable {
    53 
    49 
    54     /**
    50     /**
    55      * time of inactivity until the auto closing clips
    51      * time of inactivity until the auto closing clips
    56      * are closed
    52      * are closed
    57      */
    53      */
    59 
    55 
    60 
    56 
    61     /**
    57     /**
    62      * List of events
    58      * List of events
    63      */
    59      */
    64     private ArrayList eventQueue = new ArrayList();
    60     private final ArrayList eventQueue = new ArrayList();
    65 
    61 
    66 
    62 
    67     /**
    63     /**
    68      * Thread object for this EventDispatcher instance
    64      * Thread object for this EventDispatcher instance
    69      */
    65      */
    71 
    67 
    72 
    68 
    73     /*
    69     /*
    74      * support for auto-closing Clips
    70      * support for auto-closing Clips
    75      */
    71      */
    76     private ArrayList<ClipInfo> autoClosingClips = new ArrayList<ClipInfo>();
    72     private final ArrayList<ClipInfo> autoClosingClips = new ArrayList<ClipInfo>();
    77 
    73 
    78     /*
    74     /*
    79      * support for monitoring data lines
    75      * support for monitoring data lines
    80      */
    76      */
    81     private ArrayList<LineMonitor> lineMonitors = new ArrayList<LineMonitor>();
    77     private final ArrayList<LineMonitor> lineMonitors = new ArrayList<LineMonitor>();
    82 
    78 
    83     /**
    79     /**
    84      * Approximate interval between calls to LineMonitor.checkLine
    80      * Approximate interval between calls to LineMonitor.checkLine
    85      */
    81      */
    86     static final int LINE_MONITOR_TIME = 400;
    82     static final int LINE_MONITOR_TIME = 400;
   103 
    99 
   104     /**
   100     /**
   105      * Invoked when there is at least one event in the queue.
   101      * Invoked when there is at least one event in the queue.
   106      * Implement this as a callback to process one event.
   102      * Implement this as a callback to process one event.
   107      */
   103      */
   108     protected void processEvent(EventInfo eventInfo) {
   104     void processEvent(EventInfo eventInfo) {
   109         int count = eventInfo.getListenerCount();
   105         int count = eventInfo.getListenerCount();
   110 
   106 
   111         // process an LineEvent
   107         // process an LineEvent
   112         if (eventInfo.getEvent() instanceof LineEvent) {
   108         if (eventInfo.getEvent() instanceof LineEvent) {
   113             LineEvent event = (LineEvent) eventInfo.getEvent();
   109             LineEvent event = (LineEvent) eventInfo.getEvent();
   164      * need to be synchronized since this includes taking the event out
   160      * need to be synchronized since this includes taking the event out
   165      * from the queue and processing the event. We only need to provide
   161      * from the queue and processing the event. We only need to provide
   166      * exclusive access over the code where an event is removed from the
   162      * exclusive access over the code where an event is removed from the
   167      *queue.
   163      *queue.
   168      */
   164      */
   169     protected void dispatchEvents() {
   165     void dispatchEvents() {
   170 
   166 
   171         EventInfo eventInfo = null;
   167         EventInfo eventInfo = null;
   172 
   168 
   173         synchronized (this) {
   169         synchronized (this) {
   174 
   170 
   386     /**
   382     /**
   387      * Container for an event and a set of listeners to deliver it to.
   383      * Container for an event and a set of listeners to deliver it to.
   388      */
   384      */
   389     private class EventInfo {
   385     private class EventInfo {
   390 
   386 
   391         private Object event;
   387         private final Object event;
   392         private Object[] listeners;
   388         private final Object[] listeners;
   393 
   389 
   394         /**
   390         /**
   395          * Create a new instance of this event Info class
   391          * Create a new instance of this event Info class
   396          * @param event the event to be dispatched
   392          * @param event the event to be dispatched
   397          * @param listeners listener list; will be copied
   393          * @param listeners listener list; will be copied
   419     /**
   415     /**
   420      * Container for a clip with its expiration time
   416      * Container for a clip with its expiration time
   421      */
   417      */
   422     private class ClipInfo {
   418     private class ClipInfo {
   423 
   419 
   424         private AutoClosingClip clip;
   420         private final AutoClosingClip clip;
   425         private long expiration;
   421         private final long expiration;
   426 
   422 
   427         /**
   423         /**
   428          * Create a new instance of this clip Info class
   424          * Create a new instance of this clip Info class
   429          */
   425          */
   430         ClipInfo(AutoClosingClip clip) {
   426         ClipInfo(AutoClosingClip clip) {