Clean up code and comment
This commit is contained in:
parent
3c9344dcef
commit
32c1bb2999
7 changed files with 82 additions and 27 deletions
|
@ -1,11 +1,13 @@
|
|||
plugins {
|
||||
id 'java'
|
||||
id 'application'
|
||||
}
|
||||
|
||||
group 'de.vanitasvitae'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
mainClassName = "de.vanitasvitae.imi.codes.Main"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package de.vanitasvitae.imi.codes;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -11,15 +10,31 @@ import de.vanitasvitae.imi.codes.types.SampleTubeCode;
|
|||
import de.vanitasvitae.imi.codes.types.SampleType;
|
||||
import de.vanitasvitae.imi.codes.types.StudyNumber;
|
||||
|
||||
/**
|
||||
* Class that generates {@link SampleTubeCode SampleTubeCodes}.
|
||||
*/
|
||||
public class CodeGenerator {
|
||||
|
||||
private final FileRepository repository = new FileRepository(new File(".imicodes"));
|
||||
|
||||
public CodeGenerator() {
|
||||
// Pls do not instantiate.
|
||||
private CodeGenerator() {
|
||||
|
||||
}
|
||||
|
||||
public List<SampleTubeCode> generateCodes(StudyNumber studyNumber, SampleType sampleType, int numberOfCodes)
|
||||
/**
|
||||
* Generate {@code numberOfCodes} new codes for samples of type {@code sampleType}, which are associated to study
|
||||
* {@code studyNumber}.
|
||||
*
|
||||
* @param repository provides access to records of existing codes
|
||||
* @param studyNumber number of the study
|
||||
* @param sampleType type of the samples
|
||||
* @param numberOfCodes number of codes to be generated
|
||||
*
|
||||
* @return list of {@link SampleTubeCode SampleTubeCodes}
|
||||
*
|
||||
* @throws InvalidOptionException if too many codes would be generated.
|
||||
* @throws IOException if IO goes wrong reading or writing sample code records.
|
||||
*/
|
||||
public static List<SampleTubeCode> generateCodes(FileRepository repository, StudyNumber studyNumber, SampleType sampleType, int numberOfCodes)
|
||||
throws InvalidOptionException, IOException {
|
||||
List<SampleTubeCode> codes = new ArrayList<>();
|
||||
|
||||
|
|
|
@ -8,6 +8,12 @@ public class HtmlTableStringBuilder {
|
|||
|
||||
private StringBuilder html;
|
||||
|
||||
/**
|
||||
* Generated a HTML String which contains a table of {@link SampleTubeCode SampleTubeCodes}.
|
||||
*
|
||||
* @param title Title which gets inserted as level 2 header.
|
||||
* @param codes list of codes
|
||||
*/
|
||||
public HtmlTableStringBuilder(String title, List<SampleTubeCode> codes) {
|
||||
html = new StringBuilder("<!DOCTYPE html>\n")
|
||||
.append("<html>\n")
|
||||
|
|
|
@ -7,11 +7,13 @@ import java.io.IOException;
|
|||
import java.io.Writer;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.sql.SQLOutput;
|
||||
import java.util.List;
|
||||
|
||||
import de.vanitasvitae.imi.codes.input.Arguments;
|
||||
import de.vanitasvitae.imi.codes.input.InputValidator;
|
||||
import de.vanitasvitae.imi.codes.input.InvalidOptionException;
|
||||
import de.vanitasvitae.imi.codes.persistence.FileRepository;
|
||||
import de.vanitasvitae.imi.codes.types.SampleTubeCode;
|
||||
import de.vanitasvitae.imi.codes.types.SampleType;
|
||||
import de.vanitasvitae.imi.codes.types.StudyNumber;
|
||||
|
@ -24,9 +26,10 @@ import org.apache.commons.cli.ParseException;
|
|||
|
||||
public class Main {
|
||||
|
||||
private static final String NAME_JAR = "java -jar imicodes.jar";
|
||||
private static final String NAME_JAR = "imi.codes";
|
||||
private static final String HELP_HEADER = "Generate ID codes for sample tubes.";
|
||||
private static final String HELP_FOOTER = "\nAuthor: Paul Schaub <paul.schaub@wwu.de>";
|
||||
private static final File RECORD_DIR = new File(".imicodes");
|
||||
|
||||
/**
|
||||
* Entry point to the program.
|
||||
|
@ -39,6 +42,7 @@ public class Main {
|
|||
Options options = Arguments.getCommandLineOptions();
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
|
||||
// Parse command line arguments
|
||||
CommandLine arguments;
|
||||
try {
|
||||
arguments = parser.parse(options, args);
|
||||
|
@ -60,7 +64,7 @@ public class Main {
|
|||
File outputPath;
|
||||
boolean externalBrowser;
|
||||
|
||||
// Parse arguments
|
||||
// Validate command line arguments
|
||||
try {
|
||||
studyNumber = InputValidator.validateStudyNumber(arguments.getOptionValue(Arguments.STUDY_NUMBER));
|
||||
sampleType = InputValidator.validateSampleType(arguments.getOptionValue(Arguments.SAMPLE_TYPE));
|
||||
|
@ -76,39 +80,55 @@ public class Main {
|
|||
} catch (InvalidOptionException e) {
|
||||
// Something is wrong with the users input, so exit.
|
||||
System.out.println(e.getMessage());
|
||||
printHelp(options);
|
||||
return;
|
||||
}
|
||||
|
||||
CodeGenerator generator = new CodeGenerator();
|
||||
// Store/read number of generated codes to file
|
||||
FileRepository repository = new FileRepository(RECORD_DIR);
|
||||
|
||||
// Generate codes
|
||||
List<SampleTubeCode> codes;
|
||||
try {
|
||||
codes = generator.generateCodes(studyNumber, sampleType, numberOfCodes);
|
||||
codes = CodeGenerator.generateCodes(repository, studyNumber, sampleType, numberOfCodes);
|
||||
} catch (InvalidOptionException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Could not store records about already generated codes.");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
// Build HTML
|
||||
String html = new HtmlTableStringBuilder("IMI Sample Tube Code Generator", codes).toString();
|
||||
writeHtml(html, outputPath);
|
||||
|
||||
// Write HTML to output path
|
||||
try {
|
||||
writeHtml(html, outputPath);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Could not write HTML to file " + outputPath.getAbsolutePath());
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
// Upon users wish, open generated HTML in a browser.
|
||||
if (externalBrowser && Desktop.isDesktopSupported()) {
|
||||
try {
|
||||
Desktop.getDesktop().browse(new URI("file://" + outputPath.getAbsolutePath()));
|
||||
} catch (IOException | URISyntaxException e) {
|
||||
System.out.println("Could not open generated HTML in a browser.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeHtml(String html, File destination) {
|
||||
/*
|
||||
Write HTML to a file.
|
||||
*/
|
||||
private static void writeHtml(String html, File destination) throws IOException {
|
||||
try(Writer writer = new FileWriter(destination)) {
|
||||
writer.write(html);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,11 +5,22 @@ import java.util.stream.IntStream;
|
|||
|
||||
import de.vanitasvitae.imi.codes.input.InvalidOptionException;
|
||||
|
||||
/**
|
||||
* Abstract class, that represents a {@link CharSequence} which MUST match a given REGEX.
|
||||
*/
|
||||
public abstract class BoundedCharSequence implements CharSequence {
|
||||
|
||||
private final Pattern regex;
|
||||
private final String value;
|
||||
|
||||
/**
|
||||
* Create a {@link BoundedCharSequence}. If the {@code value} doesn't match the {@code regex},
|
||||
* a {@link InvalidOptionException} is thrown.
|
||||
*
|
||||
* @param regex regular expression
|
||||
* @param value value of the {@link BoundedCharSequence}.
|
||||
* @throws InvalidOptionException thrown when the value doesn't match the regex.
|
||||
*/
|
||||
protected BoundedCharSequence(Pattern regex, String value) throws InvalidOptionException {
|
||||
this.regex = regex;
|
||||
if (!matchPattern(value)) {
|
||||
|
|
|
@ -2,12 +2,26 @@ package de.vanitasvitae.imi.codes.types;
|
|||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* Identification Code for Sample Tubes.
|
||||
*/
|
||||
public class SampleTubeCode implements CharSequence {
|
||||
|
||||
private final StudyNumber studyNumber;
|
||||
private final SampleType sampleType;
|
||||
private final int sampleNumber;
|
||||
|
||||
/**
|
||||
* Create a new {@link SampleTubeCode} object.
|
||||
* This constructor throws an {@link IllegalArgumentException}, if one of the following cases is happening:
|
||||
* - {@code studyNumber} is null
|
||||
* - {@code sampleType} is null
|
||||
* - {@code sampleNumber is negative or greater than 9999}
|
||||
*
|
||||
* @param studyNumber code that identifies the associated study
|
||||
* @param sampleType type of the sample
|
||||
* @param sampleNumber number of the sample
|
||||
*/
|
||||
public SampleTubeCode(StudyNumber studyNumber, SampleType sampleType, int sampleNumber) {
|
||||
if (studyNumber == null) {
|
||||
throw new IllegalArgumentException("StudyNumber MUST NOT be null.");
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package de.vanitasvitae.imi.codes.types;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Study {
|
||||
private final StudyNumber number;
|
||||
private final List<SampleTubeCode> codes = new ArrayList<>();
|
||||
|
||||
public Study(StudyNumber number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue