1 /**
2 * Copyright (C) 2016 Gary Gregory. All rights reserved.
3 *
4 * See the NOTICE.txt file distributed with this work for additional
5 * information regarding copyright ownership.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 package com.garygregory.jcommander.converters.nio;
21
22 import java.nio.ByteOrder;
23
24 import com.garygregory.jcommander.converters.AbstractBaseConverter;
25
26 /**
27 * Converts a String to a {@link ByteOrder}. Valid values are {@code "BIG_ENDIAN"} and {@code "LITTLE_ENDIAN"}.
28 *
29 * <p>
30 * Example:
31 * </p>
32 *
33 * <pre class="prettyprint">
34 * <code class="language-java">@Parameter(names = { "--byteOrder" }, converter = ByteOrderConverter.class)
35 * private ByteOrder byteOrder;</code>
36 * </pre>
37 * <p>
38 *
39 * @see ByteOrder
40 * @see ByteOrder#BIG_ENDIAN
41 * @see ByteOrder#LITTLE_ENDIAN
42 * @since 1.0.0
43 * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
44 */
45 public final class ByteOrderConverter extends AbstractBaseConverter<ByteOrder> {
46
47 /**
48 * Constructs a converter.
49 *
50 * @param optionName
51 * The option name, may be null.
52 */
53 public ByteOrderConverter(final String optionName) {
54 super(optionName, ByteOrder.class);
55 }
56
57 @Override
58 protected ByteOrder convertImpl(final String value) {
59 if (value.equalsIgnoreCase(ByteOrder.BIG_ENDIAN.toString())) {
60 return ByteOrder.BIG_ENDIAN;
61 }
62 if (value.equalsIgnoreCase(ByteOrder.LITTLE_ENDIAN.toString())) {
63 return ByteOrder.LITTLE_ENDIAN;
64 }
65 throw new IllegalArgumentException(value);
66 }
67 }