PlayerQueueImpl.java
上传用户:liming6160
上传日期:2022-06-07
资源大小:785k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  *    Copyright (C) 2001 - 2007 Mobicom-Kavkaz, Inc
  3.  *    MFRadio - stream radio client for Java 2 Micro Edition
  4.  *    
  5.  *    Visit the project page at: http://mfradio.sourceforge.net
  6.  *
  7.  *    This program is free software; you can redistribute it and/or modify
  8.  *    it under the terms of the GNU General Public License as published by
  9.  *    the Free Software Foundation; either version 2 of the License, or
  10.  *    (at your option) any later version.
  11.  *
  12.  *    This program is distributed in the hope that it will be useful,
  13.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *    GNU General Public License for more details.
  16.  *
  17.  *    You should have received a copy of the GNU General Public License
  18.  *    along with this program; if not, write to the Free Software
  19.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *    Java (TM) and all Java (TM)-based marks are a trademark or 
  22.  *    registered trademark of Sun Microsystems, Inc, in the United States 
  23.  *    and other countries.
  24.  */
  25. package ru.mobicomk.mfradio.util;
  26. import java.util.Vector;
  27. import javax.microedition.media.Player;
  28. import ru.mobicomk.mfradio.Constants;
  29. import ru.mobicomk.mfradio.controller.UIController;
  30. import ru.mobicomk.mfradio.iface.PlayerQueue;
  31. /**
  32.  * Queue for {@link Player} objects.
  33.  *
  34.  * @author  Roman Bondarenko
  35.  */
  36. public class PlayerQueueImpl 
  37.     implements PlayerQueue {
  38.     
  39.     private Vector players_;
  40.     private UIController controller_;
  41.     
  42.     /**
  43.      * Creates a new instance of queue.
  44.      * @param controller Application controller object.
  45.      */
  46.     public PlayerQueueImpl(UIController controller) {
  47.         controller_ = controller;
  48.         players_ = new Vector(3, 3);
  49.     }
  50.     
  51.     // PlayerQueue implementation //////////////////////////////////////////////
  52.     
  53.     /**
  54.      * Implements method {@link ru.mobicomk.mfradio.iface.PlayerQueue#clear}
  55.      * <p>Remove all Players from the queue.</p>
  56.      *
  57.      * @see PlayerQueue
  58.      * @see PlayerQueue#clear
  59.      */
  60.     public void clear() {
  61.         Player p = null;
  62.         synchronized (players_) {
  63.             //controller_.log("clear >> remove all players (size: "+players_.size()+")");
  64.             while (!players_.isEmpty()) {
  65.                 p = (Player)players_.firstElement();
  66.                 players_.removeElement(p);
  67.                 p.close();
  68.             }
  69.             players_.notifyAll();
  70.             //controller_.log("clear >> notify!");              
  71.         }
  72.     }
  73.     /**
  74.      * Implements method {@link ru.mobicomk.mfradio.iface.PlayerQueue#pushTail}
  75.      * <p>Append Player object to tail of the queue.</p>
  76.      *
  77.      * @param player Player object to place in the queue.
  78.      * @throws ru.mobicomk.mfradio.util.PlayerQueueException if the given object 
  79.      * already in queue.
  80.      * @see PlayerQueue
  81.      * @see PlayerQueue#pushTail
  82.      */
  83.     public void pushTail(Player player) throws PlayerQueueException {
  84.         synchronized (players_) {
  85.             if (players_.contains(player)) {
  86.                 throw new PlayerQueueException("Element already in queue.");
  87.             }
  88.             
  89.             players_.addElement(player);
  90.             //controller_.log("pushTail >> add element (size: "+players_.size()+")");
  91.             
  92.             if (players_.size() == 2) {
  93.                 //controller_.log("pushTail >> notify!");
  94.                 players_.notifyAll();
  95.                 controller_.queueIsNotEmpty();
  96.             }
  97.         }
  98.     }
  99.     
  100.     /**
  101.      * Implements method {@link ru.mobicomk.mfradio.iface.PlayerQueue#popHead}
  102.      * <p>Remove Player object from head of the queue and return it to caller.</p>
  103.      *
  104.      * @throws ru.mobicomk.mfradio.util.PlayerQueueException if queue is empty 
  105.      * after 1 minute of waiting.
  106.      * @return Player object from head of the queue.
  107.      * @see PlayerQueue
  108.      * @see PlayerQueue#popHead
  109.      */
  110.     public Player popHead() throws PlayerQueueException {
  111.         synchronized (players_) {
  112.             if (players_.isEmpty()) {
  113.                 //controller_.log("popHead >> queue is empty. waiting...");
  114.                 controller_.queueIsEmpty();
  115.                 try {
  116.                     players_.wait(60000); // 60 sec
  117.                 } catch (InterruptedException ex) {
  118.                     //controller_.log("popHead >> InterruptedException >> " + ex.getMessage());
  119.                     throw new PlayerQueueException(ex.getMessage());
  120.                 } 
  121.                 if (players_.isEmpty()) { // timeout
  122.                     //controller_.log("popHead >> queue is empty again. throw exception!");
  123.                     throw new PlayerQueueException(controller_.getLocale().getString(Constants.STR_Queue_is_empty));
  124.                 }
  125.             }
  126.             
  127.             Object o = players_.firstElement();
  128.             players_.removeElement(o);
  129.             
  130.             //controller_.log("popHead >> pop element (size: "+players_.size()+")");
  131.             return (Player)o;
  132.         }
  133.     }
  134.     
  135. }