/*
 * Copyright 2014 The Apache Software Foundation.
 *
 * 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 org.apache.avro.joda;

import java.util.Collections;
import java.util.Set;
import org.apache.avro.LogicalType;
import org.apache.avro.Schema;
import org.codehaus.jackson.JsonNode;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public  class IsoDate extends LogicalType {
    private static final Set<String> RESERVED = Collections.EMPTY_SET;
    
    private IsoDate() {
      super(RESERVED, "isodate");
    }

    public IsoDate(JsonNode node) {
      super(RESERVED, "isodate");
    }

    @Override
    public void validate(Schema schema) {
      // validate the type
      if (schema.getType() != Schema.Type.INT && schema.getType() != Schema.Type.LONG &&
          schema.getType() != Schema.Type.STRING) {
        throw new IllegalArgumentException(
            "Logical type " + this + " must be backed by long or int or string");
      }
    }

    @Override
    public Set<String> reserved() {
      return RESERVED;
    }

    @Override
    public Class<?> getLogicalJavaType() {
        return LocalDate.class;
    }

    public static final DateTimeFormatter FMT = ISODateTimeFormat.date();
    
    // This can be anything really, the number serialized will be the number of days between this date and the date.
    // this serialized number will be negative for anything before 1970-01-01.
    private static final LocalDate EPOCH = new LocalDate(0L, DateTimeZone.UTC);
    
    @Override
    public Object deserialize(Schema.Type type, Object object) {
                  switch (type) {
              case STRING:
                  return FMT.parseLocalDate(((CharSequence) object).toString());
              case LONG: // start of day millis
                  return new DateTime((Long) object, DateTimeZone.UTC).toLocalDate();
              case INT: // nr of days since epoch                  
                return EPOCH.plusDays((Integer) object);
              default:
                  throw new UnsupportedOperationException();
          }
    }

    @Override
    public Object serialize(Schema.Type type, Object object) {
                  switch (type) {
              case STRING:
                  return FMT.print((ReadablePartial) object);
              case LONG:
                  return ((LocalDate) object).toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis();                
              case INT:
                  return Days.daysBetween(EPOCH, (ReadablePartial) object).getDays();
              default:
                  throw new UnsupportedOperationException();
          }
    }

  }


@@ -641,16 +641,150 @@
 .joda;%0A%0A
+import com.google.common.cache.CacheBuilder;%0Aimport com.google.common.cache.CacheLoader;%0Aimport com.google.common.cache.LoadingCache;%0A
 import j
@@ -2349,24 +2349,695 @@
 .UTC);%0A    %0A
+    private static final LoadingCache%3CLocalDate, String%3E D2S_CONV_CACHE = CacheBuilder.newBuilder()%0A            .maximumSize(2048)%0A            .build(new CacheLoader%3CLocalDate, String%3E () %7B%0A%0A      @Override%0A      public String load(final LocalDate key) throws Exception %7B%0A        return FMT.print(key);%0A      %7D%0A    %7D);%0A    %0A    %0A    private static final LoadingCache%3CString, LocalDate%3E S2D_CONV_CACHE = CacheBuilder.newBuilder()%0A            .maximumSize(2048)%0A            .build(new CacheLoader%3CString, LocalDate%3E () %7B%0A%0A      @Override%0A      public LocalDate load(final String key) throws Exception %7B%0A        return FMT.parseLocalDate(key);%0A      %7D%0A    %7D);%0A        %0A    %0A
     @Overrid
@@ -3030,32 +3030,32 @@
  %0A    @Override%0A
-
     public Objec
@@ -3193,50 +3193,42 @@
 urn 
-FMT.parseLocalDate(((CharSequence) 
+S2D_CONV_CACHE.getUnchecked(
 object
-)
 .toS
@@ -3759,41 +3759,46 @@
     
-return FMT.print((ReadablePartial
+D2S_CONV_CACHE.getUnchecked((LocalDate
 ) ob

improve LocalDate, parsing/formatting performance.