001/* A Token holder with a capacity of one token. 002 003 Copyright (c) 1997-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.actor; 029 030import java.util.LinkedList; 031import java.util.List; 032 033import ptolemy.data.Token; 034import ptolemy.kernel.util.IllegalActionException; 035 036/////////////////////////////////////////////////////////////////// 037//// Mailbox 038 039/** 040 A token holder with capacity one. 041 042 @author Jie Liu, Edward A. Lee, Lukito Muliadi 043 @version $Id$ 044 @since Ptolemy II 0.2 045 @Pt.ProposedRating Green (eal) 046 @Pt.AcceptedRating Green (neuendor) 047 */ 048public class Mailbox extends AbstractReceiver { 049 /** Construct an empty Mailbox with no container. 050 */ 051 public Mailbox() { 052 super(); 053 } 054 055 /** Construct an empty Mailbox with the specified container. 056 * @param container The container. 057 * @exception IllegalActionException If the container does 058 * not accept this receiver. 059 */ 060 public Mailbox(IOPort container) throws IllegalActionException { 061 super(container); 062 } 063 064 /////////////////////////////////////////////////////////////////// 065 //// public methods //// 066 067 /** Clear this receiver of any contained token. 068 * @exception IllegalActionException If a derived class throws it (not 069 * thrown in this base class). 070 */ 071 @Override 072 public void clear() throws IllegalActionException { 073 _token = null; 074 } 075 076 /** Return a list with the token currently in the receiver, or 077 * an empty list if there is no such token. 078 * @return A list of instances of Token. 079 */ 080 @Override 081 public List<Token> elementList() { 082 List<Token> result = new LinkedList<Token>(); 083 if (_token != null) { 084 result.add(_token); 085 } 086 return result; 087 } 088 089 /** Get the contained Token. If there is none, throw an exception. 090 * The token is removed. 091 * @return The token contained by this mailbox. 092 * @exception NoTokenException If this mailbox is empty. 093 */ 094 @Override 095 public Token get() throws NoTokenException { 096 if (_token == null) { 097 throw new NoTokenException(getContainer(), 098 "Attempt to get data from an empty mailbox."); 099 } 100 101 Token token = _token; 102 _token = null; 103 return token; 104 } 105 106 /** Return true if this mailbox is empty. 107 * @return True if this mailbox is empty. 108 */ 109 @Override 110 public boolean hasRoom() { 111 return _token == null; 112 } 113 114 /** Return true if the argument is 1 and the mailbox is empty, 115 * and otherwise return false. 116 * @param numberOfTokens The number of tokens to put into the mailbox. 117 * @return True if the argument is 1 and the mailbox is empty, 118 * and otherwise return false. 119 * @exception IllegalArgumentException If the argument is not positive. 120 * This is a runtime exception, so it does not need to be declared 121 * explicitly. 122 */ 123 @Override 124 public boolean hasRoom(int numberOfTokens) throws IllegalArgumentException { 125 if (numberOfTokens < 1) { 126 throw new IllegalArgumentException( 127 "hasRoom() requires a positive argument."); 128 } 129 130 if (numberOfTokens == 1) { 131 return _token == null; 132 } 133 134 return false; 135 } 136 137 /** Return true if this mailbox is not empty. 138 * @return True if this mailbox is not empty. 139 */ 140 @Override 141 public boolean hasToken() { 142 return _token != null; 143 } 144 145 /** Return true if the argument is 1 and this mailbox is not empty, 146 * and otherwise return false. 147 * @param numberOfTokens The number of tokens to get from the receiver. 148 * @return True if the argument is 1 and this mailbox is not empty. 149 * @exception IllegalArgumentException If the argument is not positive. 150 * This is a runtime exception, so it does not need to be declared 151 * explicitly. 152 */ 153 @Override 154 public boolean hasToken(int numberOfTokens) 155 throws IllegalArgumentException { 156 if (numberOfTokens < 1) { 157 throw new IllegalArgumentException( 158 "hasToken() requires a positive argument."); 159 } 160 161 if (numberOfTokens == 1) { 162 return _token != null; 163 } 164 165 return false; 166 } 167 168 /** Put a token into the mailbox. If the argument is null, then the 169 * mailbox will not contain a token after this returns. 170 * @param token The token to be put into the mailbox. 171 * @exception NoRoomException If this mailbox is not empty. 172 */ 173 @Override 174 public void put(Token token) throws NoRoomException { 175 if (_token != null) { 176 throw new NoRoomException(getContainer(), 177 "Cannot put a token in a full mailbox."); 178 } 179 180 _token = token; 181 } 182 183 /////////////////////////////////////////////////////////////////// 184 //// protected variables //// 185 186 /** The token held. */ 187 protected Token _token = null; 188}