001    /*
002     * cs101 producer/consumer (single word) buffer
003     * $Id: PNCBuffer.java,v 1.4 2003/09/23 14:41:25 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     * Copyright (C) 1996 Massachusetts Institute of Technology.
010     * Please do not redistribute without obtaining permission.
011     */
012    
013    package cs101.util.semaphore;
014    
015    /**
016     * cs101.util.PNCBuffer implements a producer/consumer synchronized buffer.
017     * Interface is pncBuffer.putWord(String), pncBuffer.getNextWord().
018     * <br>
019     * Copyright 1996 Massachusetts Institute of Technology
020     *
021     * @see    cs101.util.semaphore.BS
022     *
023     * @author  Lynn Andrea Stein, las@ai.mit.edu
024     * @version $Id: PNCBuffer.java,v 1.4 2003/09/23 14:41:25 gus Exp $
025     *
026     */
027    public final class PNCBuffer {
028        private String word = null;
029        private BS wordRead = new BS(true);
030        private BS wordWrite = new BS(false);
031    
032       //putWord( String )
033       /**
034        * (Safely) Puts a word into the PNCBuffer.
035        *
036        * @param    newWord    the word to be inserted.
037        *
038        * @see #getNextWord
039        * @see cs101.util.semaphore.BS
040        */
041        public void putWord( String newWord ) {
042            this.wordWrite.request();
043            this.word = newWord;
044            this.wordRead.release();
045        }
046    
047       //getNextWord()
048       /**
049        * (Safely) Consumes the word held in the PNCBuffer.
050        *
051        * @return    the word.
052        *
053        * @see #putWord
054        * @see cs101.util.semaphore.BS
055        */
056        public String getNextWord() {
057            this.wordRead.request();
058            String nw = this.word;
059            this.wordWrite.release();
060            return nw;
061        }
062    }
063    
064    /* Comments:
065     *
066     * History:
067     *     $Log: PNCBuffer.java,v $
068     *     Revision 1.4  2003/09/23 14:41:25  gus
069     *     javadoc fix
070     *
071     *     Revision 1.3  2002/11/25 15:37:53  gus
072     *     fix typo in last vix
073     *
074     *     Revision 1.2  2002/11/25 15:25:23  gus
075     *     fix javadoc errors.
076     *
077     *     Revision 1.1.1.1  2002/06/05 21:56:32  root
078     *     CS101 comes to Olin finally.
079     *
080     *     Revision 1.1  2000/04/24 22:17:22  nathanw
081     *     Bulk reorganization
082     *
083     *     Revision 1.2  1998/07/24 17:19:31  tparnell
084     *     Placate new javadoc behavior
085     *
086     *     Revision 1.1  1998/03/13 22:18:18  tparnell
087     *     Import from server crash.  I think the src and class files match up.
088     *
089     *     Revision 1.3  1996/08/01 18:26:30  reuben
090     *     More javadoc tweaking (hopefully the final pass)
091     *
092     *     Revision 1.2  1996/07/30 17:26:00  reuben
093     *     Added/corrected javadoc comments.
094     *
095     *     Revision 1.1.1.1  1996/07/18 17:38:24  sit
096     *     Import from /mit/6.096/share/classes after 6.80s session
097     *
098     *     Revision 1.4  1996/06/19 23:03:02  las
099     *     Oops.  Renamed BS's methods to correspond with reality.
100     *
101     *     6-19-96  Documentation cleaned up by las@ai.mit.edu 
102     *     6-18-96  Created by las@ai.mit.edu 
103     *
104     */