001    /* 
002     * Copyright (c) Holger Pfaff - http://pfaff.ws
003     *
004     * This software maybe used for any purpose provided the
005     * above copyright notice is retained. It is supplied as is.
006     * No warranty expressed or implied - Use at your own risk.
007     */
008    
009    import java.util.Vector;
010    
011    /**
012      * (#)Fifo.java
013      * @author   Holger Pfaff
014      * @version  3.2 19-Mar-2004<br><br>
015      *
016      * Implements a fifo (first in - first out queue) with
017      * an option to limit the number of entries, If the fifo
018      * already contains <code>maxentries</code>, the oldest entry will be
019      * discarded on addition of any new entry. If <code>unique</code> is
020      * specified to be <code>true</code> entries already contained will not
021      * be added to this fifo.
022      */
023    
024    public class Fifo extends Vector {
025    
026      /**
027       * Maximal number of entries. 0 for unlimited
028       */
029      protected int maxentries = 0;
030    
031      /**
032       * Force entries to be unique ?
033       */
034            protected boolean unique = false;
035            
036      /**
037       * Constructs an unlimited <code>Fifo</code> with duplicate
038       * entries allowed.
039       */
040            public Fifo() {
041            }
042            
043      /**
044       * Constructs a <code>Fifo</code> limited to <code>maxentries</code>.
045       * Duplicate entries allowed.
046       *
047       * @param   maxentries   maximal number if entries.
048       */
049            public Fifo(int maxentries) {
050              super(maxentries);
051              this.maxentries = maxentries;
052            }
053            
054      /**
055       * Constructs an unlimited <code>Fifo</code> with duplicate
056       * entries allowed or not allowed depending on <code>unique</code>.
057       *
058       * @param   unique   force unique entries ?
059       */
060            public Fifo(boolean unique) {
061              this.unique = unique;
062            }
063            
064      /**
065       * Constructs a <code>Fifo</code>.
066       *
067       * @param   maxentries   maximal number if entries.
068       * @param   unique   force unique entries ?
069       */
070            public Fifo(int maxentries, boolean unique) {
071              super(maxentries);
072              this.maxentries = maxentries;
073              this.unique = unique;
074            }
075            
076      /**
077       * add an object to this fifo
078       *
079       * @param   obj   object to add.
080       */
081      public void in(Object obj) {
082              if(maxentries == size() && maxentries > 0) {
083                      removeElementAt(0);
084                    }
085                    if(unique == false || contains(obj) == false) {
086                      super.addElement(obj);
087                    }
088            }
089      
090      /**
091       * remove an object from this fifo
092       */
093      public Object out() {
094              if(size() > 0) {
095          Object obj = elementAt(0);
096                      removeElementAt(0);
097          return obj;
098                    } else {
099          return null;
100        }
101            }
102    }