content: package net.csdn.common.unit;
  
  /**
   * BlogInfo: william
   * Date: 11-9-1
   * Time: 下午4:16
   */
  public enum ByteSizeUnit {
      BYTES {
          @Override
          public long toBytes(long size) {
              return size;
          }
  
          @Override
          public long toKB(long size) {
              return size / (C1 / C0);
          }
  
          @Override
          public long toMB(long size) {
              return size / (C2 / C0);
          }
  
          @Override
          public long toGB(long size) {
              return size / (C3 / C0);
          }
      },
      KB {
          @Override
          public long toBytes(long size) {
              return x(size, C1 / C0, MAX / (C1 / C0));
          }
  
          @Override
          public long toKB(long size) {
              return size;
          }
  
          @Override
          public long toMB(long size) {
              return size / (C2 / C1);
          }
  
          @Override
          public long toGB(long size) {
              return size / (C3 / C1);
          }
      },
      MB {
          @Override
          public long toBytes(long size) {
              return x(size, C2 / C0, MAX / (C2 / C0));
          }
  
          @Override
          public long toKB(long size) {
              return x(size, C2 / C1, MAX / (C2 / C1));
          }
  
          @Override
          public long toMB(long size) {
              return size;
          }
  
          @Override
          public long toGB(long size) {
              return size / (C3 / C2);
          }
      },
      GB {
          @Override
          public long toBytes(long size) {
              return x(size, C3 / C0, MAX / (C3 / C0));
          }
  
          @Override
          public long toKB(long size) {
              return x(size, C3 / C1, MAX / (C3 / C1));
          }
  
          @Override
          public long toMB(long size) {
              return x(size, C3 / C2, MAX / (C3 / C2));
          }
  
          @Override
          public long toGB(long size) {
              return size;
          }
      };
  
      static final long C0 = 1L;
      static final long C1 = C0 * 1024L;
      static final long C2 = C1 * 1024L;
      static final long C3 = C2 * 1024L;
  
      static final long MAX = Long.MAX_VALUE;
  
      /**
       * Scale d by m, checking for overflow.
       * This has a short name to make above code more readable.
       */
      static long x(long d, long m, long over) {
          if (d > over) return Long.MAX_VALUE;
          if (d < -over) return Long.MIN_VALUE;
          return d * m;
      }
  
  
      public long toBytes(long size) {
          throw new AbstractMethodError();
      }
  
      public long toKB(long size) {
          throw new AbstractMethodError();
      }
  
      public long toMB(long size) {
          throw new AbstractMethodError();
      }
  
      public long toGB(long size) {
          throw new AbstractMethodError();
      }
  }
  