View Javadoc
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.net;
21  
22  import java.net.NetworkInterface;
23  import java.net.SocketException;
24  
25  import com.garygregory.jcommander.converters.AbstractBaseConverter;
26  
27  /**
28   * Converts a {@link String} into a {@link NetworkInterface}.
29   * <p>
30   * For a description of the format, see {@link NetworkInterface#getByName(String)}.
31   * </p>
32   * 
33   * <p>
34   * Example:
35   * </p>
36   * 
37   * <pre class="prettyprint">
38   * <code class="language-java">&#64;Parameter(names = { "--networkInterface" }, converter = NetworkInterfaceConverter.class)
39   * private NetworkInterface networkInterface;</code>
40   * </pre>
41   * <p>
42   * 
43   * @see NetworkInterface
44   * @see NetworkInterface#getByName(String)
45   * 
46   * @since 1.0.0
47   * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
48   */
49  public class NetworkInterfaceNameConverter extends AbstractBaseConverter<NetworkInterface> {
50  
51      /**
52       * Constructs a converter.
53       * 
54       * @param optionName
55       *            The option name, may be null.
56       */
57      public NetworkInterfaceNameConverter(final String optionName) {
58          super(optionName, NetworkInterface.class);
59      }
60  
61      @Override
62      protected NetworkInterface convertImpl(final String value) throws SocketException {
63          return NetworkInterface.getByName(value);
64      }
65  
66  }