Advertisement
Guest User

Karoch

a guest
Jan 25th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Properties;
  5.  
  6. public class Karoch {
  7.     // Data Source interface and plain simple factory, console input by default
  8.     static interface DataSource {
  9.         static final String CONFIG_KEY = "ds";
  10.         static final String TYPE_CONSOLE = "console";
  11.         static final String TYPE_STATIC = "static";
  12.  
  13.         String readLine() throws IOException;
  14.     }
  15.  
  16.     static DataSource getDataSource(Properties properties) throws Exception {
  17.         switch (properties.getProperty(DataSource.CONFIG_KEY, DataSource.TYPE_CONSOLE)) {
  18.             case DataSource.TYPE_CONSOLE:
  19.                 return () -> new BufferedReader(new InputStreamReader(System.in)).readLine();
  20.  
  21.             case DataSource.TYPE_STATIC:
  22.                 return () -> {
  23.                     System.out.println("0");
  24.                     return "0";
  25.                 };
  26.  
  27.             default:
  28.                 throw new Exception("Data Source type is unknown");
  29.         }
  30.     }
  31.  
  32.     // Data Validator with plain simple factory, only positive number are valid by default
  33.     static interface Validator {
  34.         static final String CONFIG_KEY = "validator";
  35.         static final String TYPE_POSITIVE_NUMBER = "posNumber";
  36.         boolean isValid(String s);
  37.     }
  38.  
  39.     static Validator getValidator(Properties properties) {
  40.         switch (properties.getProperty(Validator.CONFIG_KEY, Validator.TYPE_POSITIVE_NUMBER)) {
  41.             case Validator.TYPE_POSITIVE_NUMBER:
  42.                 return (String s) -> {
  43.                     try {
  44.                         return Double.parseDouble(s) >= 0;
  45.                     } catch (NumberFormatException e) {
  46.                         return false;
  47.                     }
  48.                 };
  49.  
  50.             default:
  51.                 return (String s) -> true;
  52.         }
  53.     }
  54.  
  55.     static class Context {
  56.         public Properties properties;
  57.         public String data;
  58.     }
  59.  
  60.     // States
  61.     static interface State {
  62.         State proceed(Context context) throws Exception;
  63.     }
  64.  
  65.     static class GreetingsState implements State {
  66.         @Override
  67.         public State proceed(Context context) {
  68.             System.out.println("Enter value for A:");
  69.             return new InputState();
  70.         }
  71.     }
  72.  
  73.     static class InputState implements State {
  74.         @Override
  75.         public State proceed(Context context) throws Exception {
  76.             DataSource dataSource = getDataSource(context.properties);
  77.             context.data = dataSource.readLine();
  78.             return new ValidateState();
  79.         }
  80.     }
  81.  
  82.     static class ValidateState implements State {
  83.         @Override
  84.         public State proceed(Context context) {
  85.             return getValidator(context.properties).isValid(context.data) ? new FinishState() : new InvalidState();
  86.         }
  87.     }
  88.  
  89.     static class InvalidState implements State {
  90.         @Override
  91.         public State proceed(Context context) {
  92.             System.out.println("Try again:");
  93.             return new InputState();
  94.         }
  95.     }
  96.  
  97.     static class FinishState implements State {
  98.         @Override
  99.         public State proceed(Context context) {
  100.             System.out.println("A is " + context.data);
  101.             return null;
  102.         }
  103.     }
  104.  
  105.     static class ErrorState implements State {
  106.         private final Exception exception;
  107.  
  108.         public ErrorState(Exception exception) {
  109.             this.exception = exception;
  110.         }
  111.  
  112.         @Override
  113.         public State proceed(Context context) {
  114.             System.out.println("Something went wrong:\n" + exception.getMessage());
  115.             return null;
  116.         }
  117.     }
  118.  
  119.  
  120.     public static void main(String[] args) {
  121.         Context context = new Context();
  122.         context.properties = new Properties(); // todo: create INI file with configuration, load it here
  123.  
  124.         // Run states machine
  125.         State state = new GreetingsState();
  126.         while (state != null) {
  127.             try {
  128.                 state = state.proceed(context);
  129.             } catch (Exception e) {
  130.                 state = new ErrorState(e);
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement