View Javadoc

1   package org.perfidix.meter;
2   
3   import java.io.File;
4   import java.math.BigDecimal;
5   import java.math.MathContext;
6   
7   /**
8    * File meter for measuring the size of a file or directory registered beforehand.
9    * 
10   * @author Sebastian Graf, University of Konstanz
11   * 
12   */
13  public class FileMeter extends AbstractMeter {
14  
15      /** Static name of meter. */
16      private static final String NAME = "FileMeter";
17  
18      /** Reference to file to be benched. */
19      private final File mFile;
20  
21      /** Scale of memory. */
22      private transient final Memory mScale;
23  
24      /**
25       * Constructor.
26       * 
27       * @param pFile
28       *            to be evaluated, is going to be traversed recursively
29       * @param pScale
30       *            for returning the data
31       */
32      public FileMeter(final File pFile, final Memory pScale) {
33          super();
34          mFile = pFile;
35          mScale = pScale;
36      }
37  
38      /**
39       * {@inheritDoc}
40       */
41      @Override
42      public double getValue() {
43          long size = iterateRecursive(mFile);
44          return new BigDecimal(size, MathContext.DECIMAL128).divide(new BigDecimal(mScale.getNumberOfBytes()),
45              MathContext.DECIMAL128).doubleValue();
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      public String getUnit() {
53          return mScale.getUnit();
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public String getUnitDescription() {
61          return mScale.getUnitDescription();
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public String getName() {
69          return NAME;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public int hashCode() {
77          final int prime = 31;
78          int result = 1;
79          result = prime * result + ((mFile == null) ? 0 : mFile.hashCode());
80          return result;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public boolean equals(Object obj) {
88          if (this == obj)
89              return true;
90          if (obj == null)
91              return false;
92          if (getClass() != obj.getClass())
93              return false;
94          FileMeter other = (FileMeter)obj;
95          if (mFile == null) {
96              if (other.mFile != null)
97                  return false;
98          } else if (!mFile.equals(other.mFile))
99              return false;
100         return true;
101     }
102 
103     private static long iterateRecursive(final File pFile) {
104         long size = 0;
105         if (pFile.isDirectory()) {
106             for (final File child : pFile.listFiles()) {
107                 size = size + iterateRecursive(child);
108             }
109         }
110         if (pFile.isFile()) {
111             size = size + pFile.length();
112         }
113         return size;
114     }
115 
116 }