All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mcs.jmeasurement.MemoryHelper Maven / Gradle / Ivy

There is a newer version: 1.1.226
Show newest version
/*
 * MCS Media Computer Software
 * Copyright (c) 2006 by MCS
 * --------------------------------------
 * Created on 20.03.2006 by w.klaas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.mcs.jmeasurement;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * This class is for converting memory values to GUI representable strings.
 * 
 * @author w.klaas
 */
public final class MemoryHelper {

  /** constant for conversing B -> kb.. */
  private static final int CONVERSION_CONSTANT = 1024;

  /** formatting nnumbers. */
  private static NumberFormat numberFormat;

  /** prevent instancing. */
  private MemoryHelper() {
  }

  /**
   * converting a memory value into a GUI representable string.
   * 
   * @param freeMemory
   *            long value for the meory
   * @return the gui representation like 1,23 KB
   */
  public static String toGUIString(final long freeMemory) {
    if (numberFormat == null) {
      numberFormat = DecimalFormat.getInstance();
      numberFormat.setMaximumFractionDigits(2);
      numberFormat.setMinimumFractionDigits(2);
    }
    long bytes = freeMemory;
    double mem = (double) freeMemory;
    String result = "";
    if (bytes > CONVERSION_CONSTANT) {
      mem = bytes / (double) CONVERSION_CONSTANT;
      result = numberFormat.format(mem) + " kB";
      bytes = bytes / CONVERSION_CONSTANT;
    }
    if (bytes > CONVERSION_CONSTANT) {
      mem = bytes / (double) CONVERSION_CONSTANT;
      result = numberFormat.format(mem) + " MB";
      bytes = bytes / CONVERSION_CONSTANT;
    }
    if (bytes > CONVERSION_CONSTANT) {
      mem = bytes / (double) CONVERSION_CONSTANT;
      result = numberFormat.format(mem) + " GB";
      bytes = bytes / CONVERSION_CONSTANT;
    }
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy