1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.util; 18 19 import java.time.temporal.ChronoUnit; 20 import java.util.concurrent.TimeUnit; 21 22 /** 23 * A {@code DataUnit} represents data sizes at a given unit of granularity and 24 * provides utility methods to convert across units. 25 * <p> 26 * Note that both SI units (powers of 10) and IEC units (powers of 2) are 27 * supported, and you'll need to pick the correct one for your use-case. For 28 * example, Wikipedia defines a "kilobyte" as an SI unit of 1000 bytes, and a 29 * "kibibyte" as an IEC unit of 1024 bytes. 30 * <p> 31 * This design is mirrored after {@link TimeUnit} and {@link ChronoUnit}. 32 * 33 * @hide 34 */ 35 public enum DataUnit { toBytes(long v)36 KILOBYTES { @Override public long toBytes(long v) { return v * 1_000; } }, toBytes(long v)37 MEGABYTES { @Override public long toBytes(long v) { return v * 1_000_000; } }, toBytes(long v)38 GIGABYTES { @Override public long toBytes(long v) { return v * 1_000_000_000; } }, toBytes(long v)39 KIBIBYTES { @Override public long toBytes(long v) { return v * 1_024; } }, toBytes(long v)40 MEBIBYTES { @Override public long toBytes(long v) { return v * 1_048_576; } }, toBytes(long v)41 GIBIBYTES { @Override public long toBytes(long v) { return v * 1_073_741_824; } }; 42 toBytes(long v)43 public long toBytes(long v) { 44 throw new AbstractMethodError(); 45 } 46 } 47