/*
 * Apache HTTPD & NGINX Access log parsing made easy
 * Copyright (C) 2011-2018 Niels Basjes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.basjes.parse.core;

public class Value {

    enum Filled {
        STRING,
        LONG,
        DOUBLE
    }

    private Filled filled;
    private String s = null;
    private Long l = null;
    private Double d = null;

    public Value(String p) {
        filled = Filled.STRING;
        this.s = p;
    }

    public Value(Long p) {
        filled = Filled.LONG;
        this.l = p;
    }

    public Value(Double p) {
        filled = Filled.DOUBLE;
        this.d = p;
    }

    public String getString() {
        switch (filled) {
            case LONG:
                return l == null ? null : Long.toString(l);
            case DOUBLE:
                return d == null ? null : Double.toString(d);
            default: // == case STRING:
                return s;
        }
    }

    public Long getLong() {
        switch (filled) {
            case STRING:
                try {
                    return s == null ? null : Long.parseLong(s);
                } catch (NumberFormatException e) {
                    return null;
                }
            case DOUBLE:
                return d == null ? null : (long) Math.floor(d + 0.5d); // Apply rounding
            default: // == case LONG:
                return l;
        }
    }

    public Double getDouble() {
        switch (filled) {
            case STRING:
                try {
                    return s == null ? null : Double.parseDouble(s);
                } catch (NumberFormatException e) {
                    return null;
                }
            case LONG:
                return l == null ? null : new Double(l);
            default: // == case DOUBLE:
                return d;
        }
    }

    @Override
    public String toString() {
        return "Value{" +
                "filled=" + filled +
                ", s='" + s + '\'' +
                ", l=" + l +
                ", d=" + d +
                '}';
    }
}

@@ -2416,23 +2416,76 @@
     
-return 
+StringBuilder sb = new StringBuilder();%0A        sb  .append(
 %22Value%7B%22
  +%0A 
@@ -2480,18 +2480,17 @@
 %22Value%7B%22
- +
+)
 %0A       
@@ -2494,20 +2494,24 @@
         
-    
+.append(
 %22filled=
@@ -2515,85 +2515,167 @@
 ed=%22
- + filled +%0A                %22, s='%22 + s + '%5C'' +%0A                %22, l=%22 + l +
+).append(filled);%0A        if (s == null) %7B%0A            sb.append(%22, s=null%22);%0A        %7D else %7B%0A            sb.append(%22, s='%22).append(s).append('%5C'');%0A        %7D
 %0A   
@@ -2679,24 +2679,27 @@
         
+sb%0A
         
 %22, d=%22 +
@@ -2694,40 +2694,130 @@
     
+    .append(
 %22, 
-d=%22 + d +%0A                '%7D'
+l=%22).append(l)%0A            .append(%22, d=%22).append(d)%0A            .append('%7D');%0A        return sb.toString()
 ;%0A  

Improve Value.toString to show difference between the string "null" and a String that is null.