package com.xjeffrose.xio.config;

import com.typesafe.config.Config;
import com.xjeffrose.xio.storage.ReadProvider;
import com.xjeffrose.xio.storage.WriteProvider;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class Ruleset {

  private final IpAddressDeterministicRuleEngineConfig ipRules = new IpAddressDeterministicRuleEngineConfig();
  boolean ipRulesDirty = false;

  private final String ipFilterPath;
  private final String http1FilterPath;

  public Ruleset(Config config) {
    ipFilterPath = config.getString("ipFilter.path");
    http1FilterPath = config.getString("http1Filter.path");

  }

  public long write(WriteProvider writer) {
    long recordsWritten = 0;
    if (ipRulesDirty) {
      writer.write(ipFilterPath, ipRules);
      recordsWritten += ipRules.size();
      ipRulesDirty = false;
    }

    return recordsWritten;
  }

  public void read(ReadProvider reader) {
    reader.read(ipFilterPath, ipRules);
  }

  public IpAddressDeterministicRuleEngineConfig mutateIpRules() {
    ipRulesDirty = true;
    return ipRules;
  }
}

@@ -237,30 +237,431 @@
 et %7B%0A%0A  
-private final 
+static public class Markable%3CT%3E %7B%0A    private final T data;%0A    private boolean marked = false;%0A%0A    Markable(T data) %7B%0A      this.data = data;%0A    %7D%0A%0A    public T mutate() %7B%0A      marked = true;%0A      return data;%0A    %7D%0A%0A    public T get() %7B%0A      return data;%0A    %7D%0A%0A    public boolean marked() %7B%0A      return marked;%0A    %7D%0A%0A    public void clearMark() %7B%0A      marked = false;%0A    %7D%0A  %7D%0A%0A  private final Markable%3C
 IpAddres
@@ -690,16 +690,17 @@
 neConfig
+%3E
  ipRules
@@ -706,16 +706,31 @@
 s = new 
+Markable%3C%3E(new 
 IpAddres
@@ -765,40 +765,141 @@
 ig()
+)
 ;%0A  
-boolean ipRulesDirty = false
+private final Markable%3CHttp1DeterministicRuleEngineConfig%3E http1Rules = new Markable%3C%3E(new Http1DeterministicRuleEngineConfig())
 ;%0A%0A 
@@ -1202,16 +1202,17 @@
 en = 0;%0A
+%0A
     if (
@@ -1218,21 +1218,25 @@
 (ipRules
-Dirty
+.marked()
 ) %7B%0A    
@@ -1267,24 +1267,30 @@
 ath, ipRules
+.get()
 );%0A      rec
@@ -1312,16 +1312,22 @@
 ipRules.
+get().
 size();%0A
@@ -1343,21 +1343,192 @@
 ules
-Dirty = false
+.clearMark();%0A    %7D%0A%0A    if (http1Rules.marked()) %7B%0A      writer.write(http1FilterPath, http1Rules.get());%0A      recordsWritten += http1Rules.get().size();%0A      http1Rules.clearMark()
 ;%0A  
@@ -1643,16 +1643,74 @@
  ipRules
+.get());%0A    reader.read(http1FilterPath, http1Rules.get()
 );%0A  %7D%0A%0A
@@ -1783,47 +1783,132 @@
     
-ipRulesDirty = true;%0A    return ipRules
+return ipRules.mutate();%0A  %7D%0A%0A  public Http1DeterministicRuleEngineConfig mutateHttpRules() %7B%0A    return http1Rules.mutate()
 ;%0A  

Refactor dirty flag marking in Ruleset.