Android Open Source - appsensor C S V Compressor






From Project

Back to project page appsensor.

License

The source code is released under:

GNU General Public License

If you think the Android project appsensor listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.dfki.appsensor.utils;
//from   w  w w .  j a v a 2 s  . c  om
/**
 * <p>
 * This class can be used to remove redundant information from from a table when
 * writing it into a csv file. Basically, it blanks those fields that do not
 * change from one row to the following one.
 * </p>
 * 
 * @author Matthias Boehmer, matthias.boehmer@dfki.de
 */
public class CSVCompressor {
  
  /** buffers the string of the csv file */
  private StringBuffer sb;

  /** data of the last row put to the table */
  private String[] lastRow;

  /**
   * Add a new row of data to the table.
   * 
   * @param add
   */
  public void add(String[] add) {
    if (sb == null) {

      sb = new StringBuffer();

      // if we do not have a first last row, we cannot compress anything
      // redundant
      int size = add.length;
      for (int i = 0; i < (size - 1); i++) {
        sb.append((add[i] == null) ? "nil" : add[i]);
        sb.append(',');
      }
      sb.append((add[size - 1] == null) ? "nil" : add[size - 1]);

    } else {
      sb.append('\n');

      // compress: do not add values that are contained in the last row
      int size = add.length;
      for (int i = 0; i < (size - 1); i++) {
        if(lastRow[i] == null) {
          if(add[i] != null) sb.append(add[i]);
        }
        else if (!lastRow[i].equals(add[i])) {
          sb.append((add[i] == null) ? "nil" : add[i]);
          }

        sb.append(',');
      }
      
      if(lastRow[size - 1] == null) {
        if(add[size - 1] != null) sb.append(add[size - 1]);
      } else if (!lastRow[size - 1].equals(add[size - 1])) {
        sb.append((add[size - 1] == null) ? "nil" : add[size - 1]);
      }
    }

    lastRow = add;
  }

  /**
   * Returns the data as a "compressed" csv string with redundant values
   * removed.
   * 
   * @return
   */
  public String getCSV() {
    return sb.toString();
  }

}




Java Source Code List

de.dfki.appsensor.backup.InstallationBackupAgent.java
de.dfki.appsensor.backup.WrapperBackupAgent.java
de.dfki.appsensor.data.AppUsageProvider.java
de.dfki.appsensor.data.db.AppUsageEventDAO.java
de.dfki.appsensor.data.db.GeneralDAO.java
de.dfki.appsensor.data.entities.AppUsageEvent.java
de.dfki.appsensor.logging.AppUsageLogger.java
de.dfki.appsensor.logging.BackgroundService.java
de.dfki.appsensor.logging.DeviceObserver.java
de.dfki.appsensor.logging.HardwareObserver.java
de.dfki.appsensor.logging.LocationObserver.java
de.dfki.appsensor.logging.ServiceStarter.java
de.dfki.appsensor.sync.AppUsageSyncAdapter.java
de.dfki.appsensor.sync.AppUsageSyncService.java
de.dfki.appsensor.sync.AuthenticationService.java
de.dfki.appsensor.sync.Authenticator.java
de.dfki.appsensor.sync.SyncThread.java
de.dfki.appsensor.ui.HomeActivity.java
de.dfki.appsensor.ui.SettingsActivity.java
de.dfki.appsensor.utils.App.java
de.dfki.appsensor.utils.CSVCompressor.java
de.dfki.appsensor.utils.MyDBHelper.java
de.dfki.appsensor.utils.NetUtils.java
de.dfki.appsensor.utils.UIUtils.java
de.dfki.appsensor.utils.Utils.java