package com.github.davidmoten.logan;

import java.util.Date;

import com.github.davidmoten.bplustree.LargeByteBuffer;
import com.github.davidmoten.bplustree.Serializer;

public class PropertyWithTimestamp {

    final String key;
    final double value;
    final long time;
    final String stringValue;

    /**
     * If stringValue is not null then this is a string property (double value
     * ignored) otherwise this is a numeric (double) value.
     * 
     * @param key
     *            the property key
     * @param value
     *            the property value if numeric
     * @param stringValue
     *            the property value if non-numeric
     * @param time
     *            property timestamp
     */
    public PropertyWithTimestamp(String key, double value, String stringValue, long time) {
        this.key = key;
        this.value = value;
        this.stringValue = stringValue;
        this.time = time;
    }

    public static final Serializer<PropertyWithTimestamp> SERIALIZER = new Serializer<PropertyWithTimestamp>() {

        @Override
        public PropertyWithTimestamp read(LargeByteBuffer bb) {
            String key = bb.getString();
            byte isString = bb.get();
            if (isString == 0) {
                double value = bb.getDouble();
                long time = bb.getLong();
                return new PropertyWithTimestamp(key, value, null, time);
            } else {
                String stringValue = bb.getString();
                long time = bb.getLong();
                return new PropertyWithTimestamp(key, 0, stringValue, time);
            }
        }

        @Override
        public void write(LargeByteBuffer bb, PropertyWithTimestamp t) {
            bb.putString(t.key);
            if (t.stringValue == null) {
                bb.put((byte) 0);
                bb.putDouble(t.value);
                bb.putLong(t.time);
            } else {
                bb.put((byte) 1);
                bb.putString(t.stringValue);
                bb.putLong(t.time);
            }
        }

        @Override
        public int maxSize() {
            return 0;
        }
    };

    @Override
    public String toString() {
        return "PropertyWithTimestamp [key=" + key + ", value=" + (stringValue == null? value:"\"" + stringValue + "\"") + ", time=" + new Date(time) + "]";
    }

}

@@ -1205,24 +1205,120 @@
 = bb.get();%0A
+            %0A            final double value;%0A            final String stringValue;%0A            %0A
             
@@ -1354,23 +1354,16 @@
         
-double 
 value = 
@@ -1398,108 +1398,52 @@
     
-long time = bb.getLong();%0A                return new PropertyWithTimestamp(key, value, null, time);%0A
+stringValue = null;%0A            %7D else %7B%0A   
     
@@ -1446,32 +1446,35 @@
             
-%7D else %7B
+ value = 0;
 %0A           
@@ -1478,23 +1478,16 @@
         
-String 
 stringVa
@@ -1504,32 +1504,42 @@
 etString();%0A    
+        %7D%0A
             long
@@ -1556,28 +1556,24 @@
 .getLong();%0A
-    
             
@@ -1610,17 +1610,21 @@
 mp(key, 
-0
+value
 , string
@@ -1633,38 +1633,24 @@
 lue, time);%0A
-            %7D%0A
         %7D%0A%0A 
@@ -2280,16 +2280,32 @@
  value=%22
+%0A               
  + (stri
@@ -2323,16 +2323,19 @@
 null
+ 
 ? value
-:
+ : 
 %22%5C%22%22

clean up