001    /*
002     * cs101 producer/consumer (single integer) buffer
003     * $Id: IntBuffer.java,v 1.9 2003/09/23 15:37:30 gus Exp $
004     *
005     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
006     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
007     * CS101 homepage</a> or email <las@ai.mit.edu>.
008     *
009     * Please do not redistribute without obtaining permission.
010     */
011    
012    package cs101.util.semaphore;
013    
014    /**
015     * Implements a producer/consumer synchronized buffer. <br>
016     * Interface is IntBuffer.putButton( int ), IntBuffer.getButton(). This class
017     * does guarantee that no value written to it will be overwritten before it
018     * has been read and also guarantees that once a value has been read it will
019     * not be re-read on subsequent attempts. <p>
020     *
021     * <em>This class only behaves well when a single thread is reading the buffer
022     * and a single thread is writing to the buffer, or the order in which events
023     * are processed is unimportant. This class doesn't guarantee
024     * that competing threads will get to write in a sensible order, and does
025     * not guarantee that competing threads will get to read from the buffer
026     * in a sensible order</em>
027     *
028     * @see    cs101.util.semaphore.BS
029     *
030     * @author  Lynn Andrea Stein, las@ai.mit.edu
031     * @author  Patrick G. Heck, gus.heck@olin.edu
032     * @version $Id: IntBuffer.java,v 1.9 2003/09/23 15:37:30 gus Exp $
033     *
034     */
035    public final class IntBuffer {
036        private int button;
037        private BS buttonRead = new BS(true);
038        private BS buttonWrite = new BS(false);
039        
040        //putButton( int )
041        /**
042         * An alternate (calculator oriented) name for {@link #putInt(int)}.
043         *
044         * @param    newButton    the button to be inserted.
045         *
046         * @see #getButton
047         * @see cs101.util.semaphore.BS
048         */
049        public void putButton( int newButton ) {
050            this.putInt(newButton);
051        }
052        
053        // getButton()
054        /**
055         * An alternate (caluculator oriented) name for {@link #getInt}.
056         *
057         * @return    the next button.
058         *
059         * @see #putButton
060         * @see cs101.util.semaphore.BS
061         */
062        public int getButton() {
063            return this.getInt();
064        }
065        
066        //putButton( int )
067        /**
068         * (Safely) Puts an int into the IntBuffer.
069         *
070         * @param    newButton    the value to be inserted.
071         *
072         * @see #getButton
073         * @see cs101.util.semaphore.BS
074         */
075        public void putInt( int newButton ) {
076            this.buttonWrite.request();
077            this.button = newButton;
078            this.buttonRead.release();
079        }
080        
081        // getButton()
082        /**
083         * (Safely) Consumes the int held in the IntBuffer.
084         *
085         * @return    the integer value.
086         *
087         * @see #putButton
088         * @see cs101.util.semaphore.BS
089         */
090        public int getInt() {
091            this.buttonRead.request();
092            int b = this.button;
093            this.buttonWrite.release();
094            return b;
095        }
096    }
097    
098    /* Comments:
099     *
100     * History:
101     *     $Log: IntBuffer.java,v $
102     *     Revision 1.9  2003/09/23 15:37:30  gus
103     *     javadoc fix
104     *
105     *     Revision 1.8  2003/09/23 14:42:16  gus
106     *     javadoc change
107     *
108     *     Revision 1.7  2003/02/27 15:53:39  gus
109     *     make me an author
110     *
111     *     Revision 1.6  2003/02/27 15:49:55  gus
112     *     provided a more generic interface while maintianing the button oriented interface
113     *     used by the calculator problem set
114     *
115     *     Revision 1.5  2003/02/27 15:38:31  gus
116     *     Javadoc tweaks
117     *
118     *     Revision 1.4  2003/02/26 19:37:15  gus
119     *     expanded class doc comment.
120     *
121     *     Revision 1.3  2002/11/25 15:36:22  gus
122     *     fix typo in last vix.
123     *
124     *     Revision 1.2  2002/11/25 15:24:20  gus
125     *     fix javadoc errors
126     *
127     *     Revision 1.1.1.1  2002/06/05 21:56:32  root
128     *     CS101 comes to Olin finally.
129     *
130     *     Revision 1.1  2000/04/24 22:17:22  nathanw
131     *     Bulk reorganization
132     *
133     *     Revision 1.2  1998/07/24 17:19:28  tparnell
134     *     Placate new javadoc behavior
135     *
136     *     Revision 1.1  1998/03/13 22:18:15  tparnell
137     *     Import from server crash.  I think the src and class files match up.
138     *
139     *     Revision 1.3  1996/08/01 18:26:28  reuben
140     *     More javadoc tweaking (hopefully the final pass)
141     *
142     *     Revision 1.2  1996/07/30 17:26:00  reuben
143     *     Added/corrected javadoc comments.
144     *
145     *     Revision 1.1.1.1  1996/07/18 17:38:24  sit
146     *     Import from /mit/6.096/share/classes after 6.80s session
147     *
148     *     Revision 1.1  1996/06/25 22:23:36  las
149     *     Initial revision
150     *
151     *     6-25-96  Created by las@ai.mit.edu from PNCBuffer.java
152     *
153     *     Revision 1.4  1996/06/19 23:03:02  las
154     *     Oops.  Renamed BS's methods to correspond with reality.
155     *
156     *     6-19-96  Documentation cleaned up by las@ai.mit.edu
157     *     6-18-96  Created by las@ai.mit.edu
158     *
159     */