package hu.supercluster.overpasser.library.query;

import java.util.Locale;
import java.util.Set;

class OverpassQueryBuilder {
    private static final Locale LOCALE = Locale.US;
    private StringBuilder builder;

    public OverpassQueryBuilder() {
        builder = new StringBuilder();
    }

    public OverpassQueryBuilder append(String statement) {
        builder.append(statement);

        return this;
    }

    public OverpassQueryBuilder boundingBox(double lat1, double lon1, double lat2, double lon2) {
        builder.append(
                String.format(
                        LOCALE,
                        "(%s,%s,%s,%s)",
                        lat1, lon1,
                        lat2, lon2
                )
        );

        return this;
    }

    public OverpassQueryBuilder standaloneParam(String name) {
        return paramRel(name, "", "");
    }

    public OverpassQueryBuilder clause(String name, String value) {
        return paramRel(name, ":", value);
    }

    public OverpassQueryBuilder equals(String name, String value) {
        return paramRel(name, "=", value);
    }

    public OverpassQueryBuilder multipleValues(String name, Set<String> values) {
        StringBuilder joiner = new StringBuilder();
        for (String value : values) {
            joiner.append(value);
            joiner.append("|");
        }

        joiner.setLength(joiner.length() - 1);

        return paramRel(name, "~", joiner.toString());
    }

    public OverpassQueryBuilder notEquals(String name, String value) {
        return paramRel(name, "!=", value);
    }

    public OverpassQueryBuilder regexMatches(String name, String value) {
        return paramRel(name, "~", value);
    }

    public OverpassQueryBuilder regexDoesntMatch(String name, String value) {
        return paramRel(name, "!~", value);
    }

    private OverpassQueryBuilder paramRel(String name, String rel, String value) {
        String quotedValue = value.isEmpty() ? "" : String.format("\"%s\"", value);

        builder.append(String.format(LOCALE, "[\"%s\"%s%s]", name, rel, quotedValue));

        return this;
    }

    public String build() {
        return builder.toString();
    }
}

@@ -406,32 +406,409 @@
 rn this;%0A    %7D%0A%0A
+    private OverpassQueryBuilder paramRel(String name, String rel, String value) %7B%0A        String quotedValue = value.isEmpty() ? %22%22 : String.format(%22%5C%22%25s%5C%22%22, value);%0A%0A        return append(%0A                String.format(%0A                        LOCALE,%0A                        %22%5B%5C%22%25s%5C%22%25s%25s%5D%22,%0A                        name, rel, quotedValue%0A                )%0A        );%0A    %7D%0A%0A
     public Overp
@@ -893,32 +893,31 @@
 ) %7B%0A        
-builder.
+return 
 append(%0A    
@@ -1055,32 +1055,8 @@
 on1,
-%0A                       
  lat
@@ -1091,38 +1091,16 @@
       );
-%0A%0A        return this;
 %0A    %7D%0A%0A
@@ -1448,367 +1448,8 @@
  %7D%0A%0A
-    public OverpassQueryBuilder multipleValues(String name, Set%3CString%3E values) %7B%0A        StringBuilder joiner = new StringBuilder();%0A        for (String value : values) %7B%0A            joiner.append(value);%0A            joiner.append(%22%7C%22);%0A        %7D%0A%0A        joiner.setLength(joiner.length() - 1);%0A%0A        return paramRel(name, %22~%22, joiner.toString());%0A    %7D%0A%0A
     
@@ -1816,38 +1816,37 @@
 e);%0A    %7D%0A%0A    p
-rivate
+ublic
  OverpassQueryBu
@@ -1847,32 +1847,38 @@
 ueryBuilder 
-paramRel
+multipleValues
 (String name
@@ -1880,39 +1880,33 @@
  name, S
-tring rel, 
+et%3C
 String
+%3E
  value
+s
 ) %7B%0A    
@@ -1919,78 +1919,118 @@
 ring
- quotedV
+Builder joiner = new StringBuilder();%0A        for (String v
 alue 
-=
+:
  value
-.isEmpty() ? %22%22 : String.format(%22%5C%22%25s%5C%22%22, 
+s) %7B%0A            joiner.append(
 value);%0A
 %0A   
@@ -2025,25 +2025,25 @@
 value);%0A
-%0A
+ 
         
 builder.
@@ -2026,37 +2026,39 @@
 alue);%0A         
-build
+   join
 er.append(String
@@ -2055,92 +2055,126 @@
 end(
-String.format(LOCALE, %22%5B%5C%22%25s%5C%22%25s%25s%5D%22, name, rel, quotedValue));%0A%0A        return this
+%22%7C%22);%0A        %7D%0A%0A        joiner.setLength(joiner.length() - 1);%0A%0A        return paramRel(name, %22~%22, joiner.toString())
 ;%0A  

Rearrange methods