blob: cf045b8aec0652a4b18ddb26cffb85efd0f60cd8 [file] [log] [blame]
Jeff Sharkey9f2dc052018-01-07 16:47:31 -07001/*
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
17package android.util;
18
19import java.time.temporal.ChronoUnit;
20import java.util.concurrent.TimeUnit;
21
22/**
Jeff Sharkey399ea832018-01-19 09:56:51 +090023 * 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 Sharkey9f2dc052018-01-07 16:47:31 -070030 * <p>
31 * This design is mirrored after {@link TimeUnit} and {@link ChronoUnit}.
Jeff Sharkeyeb738c12018-03-26 13:35:50 -060032 *
33 * @hide
Jeff Sharkey9f2dc052018-01-07 16:47:31 -070034 */
35public 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}