/*
 * Copyright 2016 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.logicalTypes;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.Temporal;
import java.util.Collections;
import org.apache.avro.AbstractLogicalType;
import org.apache.avro.Schema;

/**
 * Decimal represents arbitrary-precision fixed-scale decimal numbers
 */
public final class AnyTemporal extends AbstractLogicalType<Temporal> {


  AnyTemporal(Schema.Type type) {
    super(type, Collections.EMPTY_SET, "any_temporal",
            Collections.EMPTY_MAP, Temporal.class);
    if (type != Schema.Type.STRING) {
       throw new IllegalArgumentException(this.logicalTypeName + " must be backed by string, not" + type);
    }
  }

  @Override
  public Temporal deserialize(Object object) {
    switch (type) {
      case STRING:
        String strVal = (String) object;
        int idx = strVal.indexOf('-');
        if (idx < 0) {
          return Year.parse(strVal);
        }
        idx = strVal.indexOf('-', idx + 1);
        if (idx < 0) {
          return YearMonth.parse(strVal);
        }
        idx = strVal.indexOf('T', idx + 1);
        if (idx <  0) {
          return LocalDate.parse(strVal);
        }
        return LocalDateTime.parse(strVal);
      default:
        throw new UnsupportedOperationException("Unsupported type " + type + " for " + this);
    }

  }

  @Override
  public Object serialize(Temporal temporal) {
    switch (type) {
      case STRING:
        return temporal.toString();
      default:
        throw new UnsupportedOperationException("Unsupported type " + type + " for " + this);
    }
  }

}

@@ -1345,24 +1345,242 @@
 %0A    %7D%0A  %7D%0A%0A
+  public static int indexOf(final CharSequence cs, final int from, final int to, final char c) %7B%0A    for (int i = from; i %3C to; i++) %7B%0A      if (c == cs.charAt(i)) %7B%0A        return i;%0A      %7D%0A    %7D%0A    return -1;%0A  %7D%0A%0A
   @Override%0A
@@ -1677,31 +1677,43 @@
     
-String strVal = (String
+CharSequence strVal = (CharSequence
 ) ob
@@ -1730,19 +1730,17 @@
     int 
-idx
+l
  = strVa
@@ -1741,24 +1741,66 @@
  strVal.
-indexOf(
+length();%0A        int idx = indexOf(strVal, 0, l, 
 '-');%0A  
@@ -1881,31 +1881,24 @@
   idx = 
-strVal.
 indexOf(
 '-', idx
@@ -1889,19 +1889,22 @@
 indexOf(
-'-'
+strVal
 , idx + 
@@ -1896,32 +1896,40 @@
 (strVal, idx + 1
+, l, '-'
 );%0A        if (i
@@ -2008,23 +2008,16 @@
 x = 
-strVal.
 indexOf(
 'T',
@@ -2016,11 +2016,14 @@
 xOf(
-'T'
+strVal
 , id
@@ -2027,16 +2027,24 @@
  idx + 1
+, l, 'T'
 );%0A     
@@ -2154,24 +2154,25 @@
 se(strVal);%0A
+%0A
       defaul

make it work with any string implementation