Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.buffer » [javadoc | source]
    1   /*
    2   Copyright 1999 CERN - European Organization for Nuclear Research.
    3   Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
    4   is hereby granted without fee, provided that the above copyright notice appear in all copies and 
    5   that both that copyright notice and this permission notice appear in supporting documentation. 
    6   CERN makes no representations about the suitability of this software for any purpose. 
    7   It is provided "as is" without expressed or implied warranty.
    8   */
    9   package org.apache.mahout.math.buffer;
   10   
   11   import org.apache.mahout.math.PersistentObject;
   12   import org.apache.mahout.math.list.ObjectArrayList;
   13   
   14   /** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
   15   @Deprecated
   16   public class ObjectBuffer extends PersistentObject implements ObjectBufferConsumer {
   17   
   18     private final ObjectBufferConsumer target;
   19     private final Object[] elements;
   20   
   21     // vars cached for speed
   22     private final ObjectArrayList<Object> list;
   23     private final int capacity;
   24     private int size;
   25   
   26     /**
   27      * Constructs and returns a new buffer with the given target.
   28      *
   29      * @param target   the target to flush to.
   30      * @param capacity the number of points the buffer shall be capable of holding before overflowing and flushing to the
   31      *                 target.
   32      */
   33     public ObjectBuffer(ObjectBufferConsumer target, int capacity) {
   34       this.target = target;
   35       this.capacity = capacity;
   36       this.elements = new Object[capacity];
   37       this.list = new ObjectArrayList<Object>(elements);
   38       this.size = 0;
   39     }
   40   
   41     /**
   42      * Adds the specified element to the receiver.
   43      *
   44      * @param element the element to add.
   45      */
   46     public void add(Object element) {
   47       if (this.size == this.capacity) {
   48         flush();
   49       }
   50       this.elements[size++] = element;
   51     }
   52   
   53     /**
   54      * Adds all elements of the specified list to the receiver.
   55      *
   56      * @param list the list of which all elements shall be added.
   57      */
   58     @Override
   59     public void addAllOf(ObjectArrayList<Object> list) {
   60       int listSize = list.size();
   61       if (this.size + listSize >= this.capacity) {
   62         flush();
   63       }
   64       this.target.addAllOf(list);
   65     }
   66   
   67     /** Sets the receiver's size to zero. In other words, forgets about any internally buffered elements. */
   68     public void clear() {
   69       this.size = 0;
   70     }
   71   
   72     /** Adds all internally buffered elements to the receiver's target, then resets the current buffer size to zero. */
   73     public void flush() {
   74       if (this.size > 0) {
   75         list.setSize(this.size);
   76         this.target.addAllOf(list);
   77         this.size = 0;
   78       }
   79     }
   80   }

Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.buffer » [javadoc | source]