BooleanConverter.java revision 58a4af720c30d8dba072a96e3713577c8d30aeae
158a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust/**
258a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * Copyright (C) 2010 the original author or authors.
358a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * See the notice.md file distributed with this work for additional
458a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * information regarding copyright ownership.
558a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust *
658a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * Licensed under the Apache License, Version 2.0 (the "License");
758a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * you may not use this file except in compliance with the License.
858a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * You may obtain a copy of the License at
958a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust *
1058a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust *     http://www.apache.org/licenses/LICENSE-2.0
1158a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust *
1258a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * Unless required by applicable law or agreed to in writing, software
1358a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * distributed under the License is distributed on an "AS IS" BASIS,
1458a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1558a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * See the License for the specific language governing permissions and
1658a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * limitations under the License.
1758a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust */
1858a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust
195a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beustpackage com.beust.jcommander.converters;
205a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust
2176c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beustimport com.beust.jcommander.ParameterException;
225a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust
2376c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beustpublic class BooleanConverter extends BaseConverter<Boolean> {
2476c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust
2576c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust  public BooleanConverter(String optionName) {
2676c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust    super(optionName);
2776c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust  }
285a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust
295a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust  @Override
305a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust  public Boolean convert(String value) {
3176c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust    if ("false".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value)) {
3276c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust      return Boolean.parseBoolean(value);
3376c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust    } else {
3476c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust      throw new ParameterException(getErrorString(value, "a boolean"));
3576c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust    }
365a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust  }
375a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust
385a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust}
39