added converters

This commit is contained in:
Isaac Parenteau
2018-09-13 20:05:44 -05:00
parent 0d0457452a
commit 2aa99351e9
9 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package net.locusworks.argparser.converters;
import net.locusworks.argparser.interfaces.IParameterConverter;
/**
* Converts a DataDiodeProperty value into a boolean value
* @author Isaac Parenteau
*
*/
public class BooleanConverter implements IParameterConverter<Boolean> {
@Override
public Boolean convert(String value) {
if (value == null || value.trim().isEmpty()) return false;
if (value.trim().toLowerCase().equals("yes")) return true;
boolean retVal = Boolean.parseBoolean(value.trim());
return retVal;
}
}

View File

@ -0,0 +1,14 @@
package net.locusworks.argparser.converters;
import java.nio.charset.StandardCharsets;
import net.locusworks.argparser.interfaces.IParameterConverter;
public class ByteArrayConverter implements IParameterConverter<byte[]> {
@Override
public byte[] convert(String value) {
return value.getBytes(StandardCharsets.UTF_8);
}
}

View File

@ -0,0 +1,14 @@
package net.locusworks.argparser.converters;
import java.io.File;
import net.locusworks.argparser.interfaces.IParameterConverter;
public class DirectoryConverter implements IParameterConverter<File> {
public File convert(String value) {
File file = new File(value);
return file;
}
}

View File

@ -0,0 +1,25 @@
package net.locusworks.argparser.converters;
import java.io.File;
import net.locusworks.argparser.interfaces.IParameterConverter;
/**
* Converts a command line argument to a File with verification that the file
* exists and is a file nothing else
* @author Isaac Parenteau
*
*/
public class DirectoryExistConverter implements IParameterConverter<File>{
@Override
public File convert(String value) {
File f = new File(value);
if (!f.exists()) throw new IllegalArgumentException(value + " does not exist");
if (!f.isDirectory()) throw new IllegalArgumentException(value + " is not a directory");
return new File(value);
}
}

View File

@ -0,0 +1,12 @@
package net.locusworks.argparser.converters;
import net.locusworks.argparser.interfaces.IParameterConverter;
public class DoubleConverter implements IParameterConverter<Double> {
@Override
public Double convert(String value) {
return Double.parseDouble(value);
}
}

View File

@ -0,0 +1,25 @@
package net.locusworks.argparser.converters;
import java.io.File;
import net.locusworks.argparser.interfaces.IParameterConverter;
/**
* Converts a command line argument to a File with verification that the file
* exists and is a file nothing else
* @author Isaac Parenteau
*
*/
public class FileExistConverter implements IParameterConverter<File>{
@Override
public File convert(String value) {
File f = new File(value);
if (!f.exists()) throw new IllegalArgumentException(value + " does not exist");
if (!f.isFile()) throw new IllegalArgumentException(value + " is not a file");
return new File(value);
}
}

View File

@ -0,0 +1,12 @@
package net.locusworks.argparser.converters;
import net.locusworks.argparser.interfaces.IParameterConverter;
public class FloatConverter implements IParameterConverter<Float> {
@Override
public Float convert(String value) {
return Float.parseFloat(value);
}
}

View File

@ -0,0 +1,12 @@
package net.locusworks.argparser.converters;
import net.locusworks.argparser.interfaces.IParameterConverter;
public class LongConverter implements IParameterConverter<Long> {
@Override
public Long convert(String value) {
return Long.parseLong(value);
}
}

View File

@ -0,0 +1,12 @@
package net.locusworks.argparser.converters;
import net.locusworks.argparser.interfaces.IParameterConverter;
public class ShortConverter implements IParameterConverter<Short> {
@Override
public Short convert(String value) {
return Short.parseShort(value);
}
}