
fr.vergne.translation.util.impl.SmartStringSwitcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of translation-core Show documentation
Show all versions of translation-core Show documentation
Basic features to support a translation project.
The newest version!
package fr.vergne.translation.util.impl;
import fr.vergne.translation.util.Switcher;
/**
* A {@link SmartStringSwitcher} is a {@link Switcher} dedicated to switching
* between a {@link String} and another representation. It uses a simple
* switching strategy by relying on a usual implementation for the given class.
*
* @author Matthieu VERGNE
*
* @param
*/
public class SmartStringSwitcher implements Switcher {
private final Switcher internalConvertor;
private final String nullString;
/**
*
* @param nonStringClass
* the type of the second representation
* @param nullString
* the {@link String} to map to the null
value of
* the second representation
*/
public SmartStringSwitcher(Class nonStringClass, String nullString) {
this.internalConvertor = chooseConvertor(nonStringClass);
this.nullString = nullString;
}
/**
* Equivalent to {@link #SmartStringSwitcher(Class, String)} with
* null
for the null-string argument.
*/
public SmartStringSwitcher(Class nonStringClass) {
this(nonStringClass, null);
}
@Override
public T switchForth(String value) {
if (value == nullString || value != null && value.equals(nullString)) {
return null;
} else {
return internalConvertor.switchForth(value);
}
}
@Override
public String switchBack(T value) {
if (value == null) {
return nullString;
} else {
return internalConvertor.switchBack(value);
}
}
@SuppressWarnings("unchecked")
private Switcher chooseConvertor(Class nonStringClass) {
if (Integer.class.isAssignableFrom(nonStringClass)) {
return (Switcher) new Switcher() {
@Override
public Integer switchForth(String value) {
return Integer.parseInt(value);
}
@Override
public String switchBack(Integer value) {
return value.toString();
}
};
} else if (Double.class.isAssignableFrom(nonStringClass)) {
return (Switcher) new Switcher() {
@Override
public Double switchForth(String value) {
return Double.parseDouble(value);
}
@Override
public String switchBack(Double value) {
return value.toString();
}
};
} else if (Boolean.class.isAssignableFrom(nonStringClass)) {
return (Switcher) new Switcher() {
@Override
public Boolean switchForth(String value) {
return Boolean.parseBoolean(value);
}
@Override
public String switchBack(Boolean value) {
return value.toString();
}
};
} else {
throw new IllegalArgumentException(
"Impossible to setup the convertor for " + nonStringClass);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy