001/**
002 *  Copyright (C) 2016 Gary Gregory. All rights reserved.
003 *
004 *  See the NOTICE.txt file distributed with this work for additional
005 *  information regarding copyright ownership.
006 *  
007 *  Licensed under the Apache License, Version 2.0 (the "License");
008 *  you may not use this file except in compliance with the License.
009 *  You may obtain a copy of the License at
010 *  
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *  
013 *  Unless required by applicable law or agreed to in writing, software
014 *  distributed under the License is distributed on an "AS IS" BASIS,
015 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 *  See the License for the specific language governing permissions and
017 *  limitations under the License.
018 */
019
020package com.garygregory.jcommander.converters.nio;
021
022import java.nio.ByteOrder;
023
024import com.garygregory.jcommander.converters.AbstractBaseConverter;
025
026/**
027 * Converts a String to a {@link ByteOrder}. Valid values are {@code "BIG_ENDIAN"} and {@code "LITTLE_ENDIAN"}.
028 * 
029 * <p>
030 * Example:
031 * </p>
032 * 
033 * <pre class="prettyprint">
034 * <code class="language-java">&#64;Parameter(names = { "--byteOrder" }, converter = ByteOrderConverter.class)
035 * private ByteOrder byteOrder;</code>
036 * </pre>
037 * <p>
038 * 
039 * @see ByteOrder
040 * @see ByteOrder#BIG_ENDIAN
041 * @see ByteOrder#LITTLE_ENDIAN
042 * @since 1.0.0
043 * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
044 */
045public final class ByteOrderConverter extends AbstractBaseConverter<ByteOrder> {
046
047    /**
048     * Constructs a converter.
049     * 
050     * @param optionName
051     *            The option name, may be null.
052     */
053    public ByteOrderConverter(final String optionName) {
054        super(optionName, ByteOrder.class);
055    }
056
057    @Override
058    protected ByteOrder convertImpl(final String value) {
059        if (value.equalsIgnoreCase(ByteOrder.BIG_ENDIAN.toString())) {
060            return ByteOrder.BIG_ENDIAN;
061        }
062        if (value.equalsIgnoreCase(ByteOrder.LITTLE_ENDIAN.toString())) {
063            return ByteOrder.LITTLE_ENDIAN;
064        }
065        throw new IllegalArgumentException(value);
066    }
067}