Modularize command patterns

This commit is contained in:
Arne Keller 2020-02-19 08:55:36 +01:00
parent ba6a939a92
commit be6bc87b58
3 changed files with 17 additions and 11 deletions

View File

@ -16,7 +16,7 @@ none s T3 Emma 1 false true
103-118 103-118
none e 103 118 3 true true none e 103 118 3 true true
none s T3 Emma 1 false true none s T3 Emma 1 false true
Error, could not delete rolling stock Error, invalid delete rolling stock argument
OK OK
1 1
2 2

View File

@ -9,7 +9,7 @@
9 9
T3-Emma T3-Emma
103-118 103-118
Error, could not delete rolling stock Error, invalid delete rolling stock argument
OK OK
1 1
passenger coach W1 added to train 1 passenger coach W1 added to train 1

View File

@ -15,41 +15,47 @@ import java.util.regex.Pattern;
* @version 1.0 * @version 1.0
*/ */
public final class CommandFactory { public final class CommandFactory {
private static final String NUMBER = "[+-]?\\d+";
private static final String VECTOR = NUMBER + "," + NUMBER;
private static final String ALPHANUMERIC_WORD = "[\\p{L}\\p{N}]+";
private static final String ROLLING_STOCK_IDENTIFIER
= "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|W" + NUMBER;
private static final String ADD_TRACK = "add track"; private static final String ADD_TRACK = "add track";
private static final Pattern ADD_TRACK_ARGUMENTS private static final Pattern ADD_TRACK_ARGUMENTS
= Pattern.compile(" \\(([+-]?\\d+,[+-]?\\d+)\\) -> \\(([+-]?\\d+,[+-]?\\d+)\\)"); = Pattern.compile(" \\((" + VECTOR + ")\\) -> \\((" + VECTOR + ")\\)");
private static final String ADD_SWITCH = "add switch"; private static final String ADD_SWITCH = "add switch";
private static final Pattern ADD_SWITCH_ARGUMENTS private static final Pattern ADD_SWITCH_ARGUMENTS
= Pattern.compile(" \\(([+-]?\\d+,[+-]?\\d+)\\) -> \\(([+-]?\\d+,[+-]?\\d+)\\),\\(([+-]?\\d+,[+-]?\\d+)\\)"); = Pattern.compile(" \\((" + VECTOR + ")\\) -> \\((" + VECTOR + ")\\),\\((" + VECTOR + ")\\)");
private static final String DELETE_TRACK = "delete track"; private static final String DELETE_TRACK = "delete track";
private static final String LIST_TRACKS = "list tracks"; private static final String LIST_TRACKS = "list tracks";
private static final String SET_SWITCH = "set switch"; private static final String SET_SWITCH = "set switch";
private static final Pattern SET_SWITCH_ARGUMENTS private static final Pattern SET_SWITCH_ARGUMENTS
= Pattern.compile(" \\+?(\\d+) position \\(([+-]?\\d+,[+-]?\\d+)\\)"); = Pattern.compile(" (" + NUMBER + ") position \\((" + VECTOR + ")\\)");
private static final String CREATE_ENGINE = "create engine"; private static final String CREATE_ENGINE = "create engine";
private static final Pattern CREATE_ENGINE_ARGUMENTS private static final Pattern CREATE_ENGINE_ARGUMENTS
= Pattern.compile(" (electrical|diesel|steam) ([\\p{L}\\p{N}]+) ([\\p{L}\\p{N}]+) \\+?(\\d+) (true|false) (true|false)"); = Pattern.compile(" (electrical|diesel|steam) (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" + NUMBER + ") (true|false) (true|false)");
private static final String LIST_ENGINES = "list engines"; private static final String LIST_ENGINES = "list engines";
private static final String CREATE_COACH = "create coach"; private static final String CREATE_COACH = "create coach";
private static final Pattern CREATE_COACH_ARGUMENTS private static final Pattern CREATE_COACH_ARGUMENTS
= Pattern.compile(" (passenger|freight|special) \\+?(\\d+) (true|false) (true|false)"); = Pattern.compile(" (passenger|freight|special) (" + NUMBER + ") (true|false) (true|false)");
private static final String LIST_COACHES = "list coaches"; private static final String LIST_COACHES = "list coaches";
private static final String CREATE_TRAIN_SET = "create train-set"; private static final String CREATE_TRAIN_SET = "create train-set";
private static final Pattern CREATE_TRAIN_SET_ARGUMENTS private static final Pattern CREATE_TRAIN_SET_ARGUMENTS
= Pattern.compile(" ([\\p{L}\\p{N}]+) ([\\p{L}\\p{N}]+) \\+?(\\d+) (true|false) (true|false)"); = Pattern.compile(" (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" + NUMBER + ") (true|false) (true|false)");
private static final String LIST_TRAIN_SETS = "list train-sets"; private static final String LIST_TRAIN_SETS = "list train-sets";
private static final String DELETE_ROLLING_STOCK = "delete rolling stock"; private static final String DELETE_ROLLING_STOCK = "delete rolling stock";
private static final Pattern DELETE_ROLLING_STOCK_ARGUMENT private static final Pattern DELETE_ROLLING_STOCK_ARGUMENT
= Pattern.compile(" ([\\p{L}\\p{N}]++(-[\\p{L}\\p{N}]+)?)"); = Pattern.compile(" (" + ROLLING_STOCK_IDENTIFIER + ")");
private static final String ADD_TRAIN = "add train"; private static final String ADD_TRAIN = "add train";
private static final Pattern ADD_TRAIN_ARGUMENTS private static final Pattern ADD_TRAIN_ARGUMENTS
= Pattern.compile(" \\+?(\\d+) (([\\p{L}\\p{N}]+-[\\p{L}\\p{N}]+)|(W\\+?\\d+))"); = Pattern.compile(" (" + NUMBER + ") (" + ROLLING_STOCK_IDENTIFIER + ")");
private static final String DELETE_TRAIN = "delete train"; private static final String DELETE_TRAIN = "delete train";
private static final String LIST_TRAINS = "list trains"; private static final String LIST_TRAINS = "list trains";
private static final String SHOW_TRAIN = "show train"; private static final String SHOW_TRAIN = "show train";
private static final String PUT_TRAIN = "put train"; private static final String PUT_TRAIN = "put train";
private static final Pattern PUT_TRAIN_ARGUMENTS private static final Pattern PUT_TRAIN_ARGUMENTS
= Pattern.compile(" \\+?(\\d+) at \\(([+-]?\\d+,[+-]?\\d+)\\) in direction ([+-]?\\d+,[+-]?\\d+)"); = Pattern.compile(" (" + NUMBER + ") at \\((" + VECTOR + ")\\) in direction (" + VECTOR + ")");
private static final String STEP = "step"; private static final String STEP = "step";
private static final String POSITIVE_NUMBER = " \\+?\\d+"; private static final String POSITIVE_NUMBER = " \\+?\\d+";