Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 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 | /** |
Jeff Sharkey | 399ea83 | 2018-01-19 09:56:51 +0900 | [diff] [blame] | 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. |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 30 | * <p> |
| 31 | * This design is mirrored after {@link TimeUnit} and {@link ChronoUnit}. |
Jeff Sharkey | eb738c1 | 2018-03-26 13:35:50 -0600 | [diff] [blame] | 32 | * |
| 33 | * @hide |
Jeff Sharkey | 9f2dc05 | 2018-01-07 16:47:31 -0700 | [diff] [blame] | 34 | */ |
| 35 | public enum DataUnit { |
| 36 | KILOBYTES { @Override public long toBytes(long v) { return v * 1_000; } }, |
| 37 | MEGABYTES { @Override public long toBytes(long v) { return v * 1_000_000; } }, |
| 38 | GIGABYTES { @Override public long toBytes(long v) { return v * 1_000_000_000; } }, |
| 39 | KIBIBYTES { @Override public long toBytes(long v) { return v * 1_024; } }, |
| 40 | MEBIBYTES { @Override public long toBytes(long v) { return v * 1_048_576; } }, |
| 41 | GIBIBYTES { @Override public long toBytes(long v) { return v * 1_073_741_824; } }; |
| 42 | |
| 43 | public long toBytes(long v) { |
| 44 | throw new AbstractMethodError(); |
| 45 | } |
| 46 | } |