All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nerd4j.csv.field.converter.StringToBoolean Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * #%L
 * Nerd4j CSV
 * %%
 * Copyright (C) 2013 Nerd4j
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package org.nerd4j.csv.field.converter;

import org.nerd4j.csv.field.CSVFieldConverter;


/**
 * Implementation of the {@link CSVFieldConverter} interface
 * that converts {@link String}s into {@link Boolean}s.
 * 
 * 

* This converter accepts the following representations * of a boolean value (the parsing ignores the character case): *

    *
  • 1, 0
  • *
  • y, n; Y, N;
  • *
  • t, f; T, F
  • *
  • Yes, No; YES, NO; etc.
  • *
  • true, false; True, False; TRUE, FALSE; etc.
  • *
*

* * @author Nerd4j Team */ public final class StringToBoolean extends AbstractCSVFieldConverter { /** * Default constructor. * */ public StringToBoolean() { super( "Unable to convert {1} into a Boolean value" ); } /* ***************** */ /* EXTENSION HOOKS */ /* ***************** */ /** * {@inheritDoc} */ @Override protected Boolean performConversion( final String source ) throws Exception { switch( source.length() ) { case 1: return parseCharacter( source.charAt(0) ); case 2: return "no".equalsIgnoreCase(source) ? Boolean.FALSE : null; case 3: return "yes".equalsIgnoreCase(source) ? Boolean.TRUE : null; case 4: return "true".equalsIgnoreCase(source) ? Boolean.TRUE : null; case 5: return "false".equalsIgnoreCase(source) ? Boolean.FALSE : null; default: return null; } } /* ***************** */ /* PRIVATE METHODS */ /* ***************** */ /** * If the source value is made of a single character * returns the boolean value related to such character. * * @param source the value to parse. * @return the corresponding boolean or null. */ private Boolean parseCharacter( final char source ) { switch( source ) { case '1': return Boolean.TRUE; case 't': return Boolean.TRUE; case 'T': return Boolean.TRUE; case 'y': return Boolean.TRUE; case 'Y': return Boolean.TRUE; case '0': return Boolean.FALSE; case 'f': return Boolean.FALSE; case 'F': return Boolean.FALSE; case 'n': return Boolean.FALSE; case 'N': return Boolean.FALSE; default: return null; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy