    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.io;
    019
    020import java.nio.ByteOrder;
    021
    022/**
    023 * Converts Strings to {@link ByteOrder} instances.
    024 *
    025 * @since 2.6
    026 */
    027public final class ByteOrderParser {
    028
    029    /**
    030     * ByteOrderUtils is a static utility class, so prevent construction with a private constructor.
    031     */
    032    private ByteOrderParser() {
    033    }
    034
    035    /**
    036     * Parses the String argument as a {@link ByteOrder}.
    037     * <p>
    038     * Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "LITTLE_ENDIAN"}.
    039     * </p>
    040     * <p>
    041     * Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "BIG_ENDIAN"}.
    042     * </p>
    043     * Examples:
    044     * <ul>
    045     * <li>{@code ByteOrderParser.parseByteOrder("LITTLE_ENDIAN")} returns {@code ByteOrder.LITTLE_ENDIAN}</li>
    046     * <li>{@code ByteOrderParser.parseByteOrder("BIG_ENDIAN")} returns {@code ByteOrder.BIG_ENDIAN}</li>
    047     * </ul>
    048     *
    049     * @param value
    050     *            the {@code String} containing the ByteOrder representation to be parsed
    051     * @return the ByteOrder represented by the string argument
    052     * @throws IllegalArgumentException
    053     *             if the {@code String} containing the ByteOrder representation to be parsed is unknown.
    054     */
    055    public static ByteOrder parseByteOrder(final String value) {
    056        if (ByteOrder.BIG_ENDIAN.toString().equals(value)) {
    057            return ByteOrder.BIG_ENDIAN;
    058        }
    059        if (ByteOrder.LITTLE_ENDIAN.toString().equals(value)) {
    060            return ByteOrder.LITTLE_ENDIAN;
    061        }
    062        throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expeced one of " + ByteOrder.LITTLE_ENDIAN +
    063                 ", " + ByteOrder.BIG_ENDIAN);
    064    }
    065
    066}