001    package org.hackystat.sensor.ant.vcs;
002    
003    /**
004     * Generic size counter. Note that this class is not desinged to count binary files.
005     *
006     * @author Qin ZHANG
007     * @version $Id: GenericSizeCounter.java,v 1.1.1.1 2005/10/20 23:56:56 johnson Exp $
008     */
009    public class GenericSizeCounter {
010    
011      private int totalLines = 0;
012      private int nonEmptyLines = 0;
013      
014      /**
015       * Construct this instance to count lines.
016       *
017       * @param content The file content.
018       */
019      public GenericSizeCounter(Object[] content) {
020        for (int i = 0; i < content.length; i++) {
021          Object line = content[i];
022    
023          if (line != null) {
024            this.totalLines++;
025    
026            if (line instanceof String) {
027              String str = (String) line;
028              if (!this.isLineWhiteSpace(str)) {
029                this.nonEmptyLines++;
030              }
031            }
032            else {
033              this.nonEmptyLines++;
034            }
035          }
036        }
037      }
038    
039      /**
040       * Tests whether the line contains white space only or not.
041       *
042       * @param line The line to be tested.
043       * @return True if the lines contains only white spaces or is null.
044       */
045      private boolean isLineWhiteSpace(String line) {
046        for (int i = 0; i < line.length(); i++) {
047          char ch = line.charAt(i);
048          if (!Character.isWhitespace(ch)) {
049            return false;
050          }
051        }
052        return true;
053      }
054    
055      /**
056       * Gets the total number of lines, including empty lines.
057       *
058       * @return The total number of lines.
059       */
060      public int getNumOfTotalLines() {
061        return this.totalLines;
062      }
063    
064      /**
065       * Gets the number of lines, excluding empty lines.
066       *
067       * @return The number of lines, excluding empty lines.
068       */
069      public int getNumOfNonEmptyLines() {
070        return this.nonEmptyLines;
071      }
072    }