001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.util.unit;
018
019/**
020 * A standard set of {@link DataSize} units.
021 *
022 * <p>The unit prefixes used in this class are
023 * <a href="https://en.wikipedia.org/wiki/Binary_prefix">binary prefixes</a>
024 * indicating multiplication by powers of 2. The following table displays the
025 * enum constants defined in this class and corresponding values.
026 *
027 * <p>
028 * <table border="1">
029 * <tr><th>Constant</th><th>Data Size</th><th>Power&nbsp;of&nbsp;2</th><th>Size in Bytes</th></tr>
030 * <tr><td>{@link #BYTES}</td><td>1B</td><td>2^0</td><td>1</td></tr>
031 * <tr><td>{@link #KILOBYTES}</td><td>1KB</td><td>2^10</td><td>1,024</td></tr>
032 * <tr><td>{@link #MEGABYTES}</td><td>1MB</td><td>2^20</td><td>1,048,576</td></tr>
033 * <tr><td>{@link #GIGABYTES}</td><td>1GB</td><td>2^30</td><td>1,073,741,824</td></tr>
034 * <tr><td>{@link #TERABYTES}</td><td>1TB</td><td>2^40</td><td>1,099,511,627,776</td></tr>
035 * </table>
036 *
037 * @author Stephane Nicoll
038 * @author Sam Brannen
039 * @since 5.1
040 * @see DataSize
041 */
042public enum DataUnit {
043
044        /**
045         * Bytes, represented by suffix {@code B}.
046         */
047        BYTES("B", DataSize.ofBytes(1)),
048
049        /**
050         * Kilobytes, represented by suffix {@code KB}.
051         */
052        KILOBYTES("KB", DataSize.ofKilobytes(1)),
053
054        /**
055         * Megabytes, represented by suffix {@code MB}.
056         */
057        MEGABYTES("MB", DataSize.ofMegabytes(1)),
058
059        /**
060         * Gigabytes, represented by suffix {@code GB}.
061         */
062        GIGABYTES("GB", DataSize.ofGigabytes(1)),
063
064        /**
065         * Terabytes, represented by suffix {@code TB}.
066         */
067        TERABYTES("TB", DataSize.ofTerabytes(1));
068
069
070        private final String suffix;
071
072        private final DataSize size;
073
074
075        DataUnit(String suffix, DataSize size) {
076                this.suffix = suffix;
077                this.size = size;
078        }
079
080        DataSize size() {
081                return this.size;
082        }
083
084        /**
085         * Return the {@link DataUnit} matching the specified {@code suffix}.
086         * @param suffix one of the standard suffixes
087         * @return the {@link DataUnit} matching the specified {@code suffix}
088         * @throws IllegalArgumentException if the suffix does not match the suffix
089         * of any of this enum's constants
090         */
091        public static DataUnit fromSuffix(String suffix) {
092                for (DataUnit candidate : values()) {
093                        if (candidate.suffix.equals(suffix)) {
094                                return candidate;
095                        }
096                }
097                throw new IllegalArgumentException("Unknown data unit suffix '" + suffix + "'");
098        }
099
100}