001    package edu.harvard.deas.hyperenc.vsat;
002    
003    import java.io.Serializable;
004    import java.util.Arrays;
005    
006    /**
007     * A page of data. Contains an array of bytes (the data), and an integer counter
008     * that can be used to count the number of times a page has been accessed. The
009     * data contained in a VSatPage is immutable, and the counter may only be
010     * incremented.
011     */
012    public class VSatPage implements Serializable
013    {
014      private static final long serialVersionUID = 1L;
015            
016      /** The random data in this VSatPage */
017      private final byte[] data;
018      
019      /** Counter */
020      private int accessCount;
021    
022      /**
023       * Creates a new VSatPage with the specified data. The <code>data</code> array
024       * is copied into the constructed object; subsequent mutations of the passed
025       * array do not affect this object.
026       * 
027       * @param data
028       *        Data for this page.
029       */
030      public VSatPage(byte[] data)
031      {
032        this.accessCount = 0;
033        this.data = Arrays.copyOf(data, data.length);
034      }
035    
036      /**
037       * This page's data, as a byte array. The returned array is a copy;
038       * modifications to the returned array do not affect this VSatPage.
039       * 
040       * @return a copy of this page's data
041       */
042      public byte[] getData() {
043        return Arrays.copyOf(data, data.length);
044      }
045      
046      /**
047       * Adds 1 to the counter.
048       */
049      public synchronized void increment() {
050        accessCount++;
051      }
052      
053      /**
054       * Returns the current value of the counter.
055       * @return the counter
056       */
057      public synchronized int getCount() {
058        return accessCount;
059      }
060    }