title: Java: Change detection
body: <p>I have the following design where I need to interact with client's API. And the <code>WatchService</code> (or Change Detection Handler method) had to be in the <code>Testing</code> Class in this caseand not in the <code>FoodOrder</code> class or <code>ClientAPI</code> class.</p>
  <p>The <code>ClientAPI</code> class receives food orders from the various food machines via a callback method. All food orders have a unique reference number and are stored in a hashmap. Now when there is an update in <code>instructions</code> for the same food order (identified by the same reference number), a &quot;change&quot; is detected and new instruction is written to a file. The creation / modification of file will be picked up by the <code>WatchService</code> in the <code>Testing</code> class and trigger the <code>proceesInstruction</code> method to read the updated instructions.</p>
  <p>I am looking for a better way to detect the change and pass the updated instructions to the <code>Testing</code> class without reading and writing to file, replacing the <code>WatchService</code> entirely, as this is very clumsy?</p>
  <pre><code>Class Testing{
      public static void main(String[] args){
          // connect to API...
          ClientAPI clientAPI = new ClientAPI();
          clientAPI.connect();
  
          /*** Starting WatchService ***/
          WatchService watchService = null;
          try {
              watchService = FileSystems.getDefault().newWatchService();
          } catch (IOException e) {
              e.printStackTrace();
          }
          scheduler.schedule(new WatchFile(watchService), 5, TimeUnit.SECONDS);
      }
  
  
      private static class WatchFile implements Runnable{
          
          WatchService watchService;
          public WatchFile(WatchService watchService) {
              this.watchService = watchService;
          }
          
          
          public void run() { 
              try {
                  System.out.println(&quot;2. WatchService Started Running @ &quot; + LocalDateTime.now() + &quot; !!!&quot;);
                  Path path = Paths.get(&quot;C:\\Testing\\csv&quot;);
                  path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.OVERFLOW);
                  WatchKey key;
                  while((key = watchService.take()) != null) {
                      for(WatchEvent&lt;?&gt; event: key.pollEvents()) {
                          final Path changed = (Path) event.context();
                          if(changed.endsWith(&quot;newInstructions.csv&quot;) &amp;&amp; event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                              System.out.println(&quot;Instruction File modified!!!&quot;);
                              processInstruction(&quot;C:\\Testing\\csv\\newInstructions.csv&quot;);
                          }
                      }
                      key.reset();
                  }
  
              } catch (IOException | InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } 
  
          }
      }
  
      public static void proceesInstruction(string filePath){
          // read the csv file path and process instruction
      }
  
  }
  
  Class FoodOrder{
      int machineNumber;
      String item;
      String instruction;
      String referenceNumber;
      public FoodOrder(int machineNumber, String item, String instruction, String referenceNumber){
          this.machineNumber = machineNumber;
          this.item = item;
          this.instruction = instruction;
          this.referenceNumber = referenceNumber;
      }
  
      public void updateFoodOrder(int machineNumber, String item, String instruction, String referenceNumber){
          this.machineNumber = machineNumber;
          this.item = item;
          this.referenceNumber = referenceNumber;
  
          // Detect change in instruction
          if(!instruction.equals(this.instruction)){
              // my original code writed the new instruction to the 
              // C:\\Testing\\csv\\newInstructions.csv such that watchservice can detect file has been modified or created
              // and process the instructions in the csv
          }
          this.instruction = instruction;
  
      }
  
  
  }
  
  
  
  Class ClientAPI{
      private HashMap&lt;String,FoodOrder&gt; foodOrderHashMap;
      
      // Constructor
      public clientAPI(){
              foodOrderHashMap = new HashSet&lt;String,FoodOrder&gt;();
      }
  
      // API callback function 
      public void receiveMachineOrder(int machineNumber, String item, String instruction, String referenceNumber){
          // my original is to write the instruction 
          if(foodOrderHashMap.containsKey(referenceNumber)){
              foodOrderHashMap.get(referenceNumber).updateFoodOrder(machineNumber,item,instruction,referenceNumber);
          } else{
              foodOrderHashMap.put(referenceNumber, new FoodOrder(machineNumber, item, instruction, referenceNumber));
          }
      }
  }
  </code></pre>