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.charset; 021 022import java.nio.charset.Charset; 023 024import com.garygregory.jcommander.converters.AbstractBaseConverter; 025 026/** 027 * Converts a {@link String} into a {@link Charset}. 028 * <p> 029 * For a description of the format, see {@link Charset#forName(String)}. 030 * </p> 031 * 032 * <p> 033 * Example: 034 * </p> 035 * 036 * <pre class="prettyprint"> 037 * <code class="language-java">@Parameter(names = { "--charset" }, converter = CharsetConverter.class) 038 * private Charset charset;</code> 039 * </pre> 040 * <p> 041 * 042 * @see Charset 043 * @see Charset#forName(String) 044 * 045 * @since 1.0.0 046 * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a> 047 */ 048public class CharsetConverter extends AbstractBaseConverter<Charset> { 049 050 /** 051 * Constructs a converter. 052 * 053 * @param optionName 054 * The option name, may be null. 055 */ 056 public CharsetConverter(final String optionName) { 057 super(optionName, Charset.class); 058 } 059 060 @Override 061 protected Charset convertImpl(final String value) { 062 return Charset.forName(value); 063 } 064 065}