com.feilong.lib.xstream.converters.basic.BooleanConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2003, 2004 Joe Walnes.
* Copyright (C) 2006, 2007, 2014, 2018 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 26. September 2003 by Joe Walnes
*/
package com.feilong.lib.xstream.converters.basic;
/**
* Converts a boolean primitive or java.lang.Boolean wrapper to
* a String.
*
* @author Joe Walnes
* @author David Blevins
*/
public class BooleanConverter extends AbstractSingleValueConverter{
public static final BooleanConverter TRUE_FALSE = new BooleanConverter("true", "false", false);
public static final BooleanConverter YES_NO = new BooleanConverter("yes", "no", false);
public static final BooleanConverter BINARY = new BooleanConverter("1", "0", true);
private final String positive;
private final String negative;
private final boolean caseSensitive;
public BooleanConverter(final String positive, final String negative, final boolean caseSensitive){
this.positive = positive;
this.negative = negative;
this.caseSensitive = caseSensitive;
}
public BooleanConverter(){
this("true", "false", false);
}
/**
* @deprecated As of 1.4.8 use {@link #canConvert(Class)}
*/
@Deprecated
public boolean shouldConvert(final Class type,final Object value){
return true;
}
@Override
public boolean canConvert(final Class type){
return type == boolean.class || type == Boolean.class;
}
@Override
public Object fromString(final String str){
if (caseSensitive){
return positive.equals(str) ? Boolean.TRUE : Boolean.FALSE;
}else{
return positive.equalsIgnoreCase(str) ? Boolean.TRUE : Boolean.FALSE;
}
}
@Override
public String toString(final Object obj){
final Boolean value = (Boolean) obj;
return obj == null ? null : value.booleanValue() ? positive : negative;
}
}