/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2017, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.wildfly.clustering.marshalling.spi.util;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Calendar;
import java.util.TimeZone;

import org.wildfly.clustering.marshalling.Externalizer;

/**
 * @author Paul Ferraro
 */
public class CalendarExternalizer implements Externalizer<Calendar> {

    @Override
    public void writeObject(ObjectOutput output, Calendar calendar) throws IOException {
        output.writeUTF(calendar.getCalendarType());
        output.writeLong(calendar.getTimeInMillis());
        output.writeBoolean(calendar.isLenient());
        output.writeUTF(calendar.getTimeZone().getID());
        output.writeInt(calendar.getFirstDayOfWeek());
        output.writeInt(calendar.getMinimalDaysInFirstWeek());
    }

    @Override
    public Calendar readObject(ObjectInput input) throws IOException, ClassNotFoundException {
        return new Calendar.Builder()
                .setCalendarType(input.readUTF())
                .setInstant(input.readLong())
                .setLenient(input.readBoolean())
                .setTimeZone(TimeZone.getTimeZone(input.readUTF()))
                .setWeekDefinition(input.readInt(), input.readInt())
                .build();
    }

    @Override
    public Class<Calendar> getTargetClass() {
        return Calendar.class;
    }
}

@@ -1173,24 +1173,97 @@
 va.util.
-Calendar
+Arrays;%0Aimport java.util.Calendar;%0Aimport java.util.HashMap;%0Aimport java.util.Map
 ;%0Aimport
@@ -1344,110 +1344,895 @@
 er;%0A
-%0A/**%0A * @author Paul Ferraro%0A */%0Apublic class CalendarExternalizer implements Externalizer%3CCalendar%3E %7B
+import org.wildfly.clustering.marshalling.spi.DefaultExternalizer;%0Aimport org.wildfly.clustering.marshalling.spi.IndexSerializer;%0Aimport org.wildfly.clustering.marshalling.spi.IntSerializer;%0A%0A/**%0A * @author Paul Ferraro%0A */%0Apublic class CalendarExternalizer implements Externalizer%3CCalendar%3E %7B%0A    private static final String%5B%5D CALENDAR_TYPE_NAMES;%0A    private static final Map%3CString, Integer%3E CALENDAR_TYPE_IDS = new HashMap%3C%3E();%0A    private static final IntSerializer CALENDAR_TYPE_SERIALIZER;%0A    static %7B%0A        CALENDAR_TYPE_NAMES = Calendar.getAvailableCalendarTypes().toArray(new String%5B0%5D);%0A        CALENDAR_TYPE_SERIALIZER = IndexSerializer.select(CALENDAR_TYPE_NAMES.length);%0A        Arrays.sort(CALENDAR_TYPE_NAMES);%0A        for (int i = 0; i %3C CALENDAR_TYPE_NAMES.length; ++i) %7B%0A            CALENDAR_TYPE_IDS.put(CALENDAR_TYPE_NAMES%5Bi%5D, Integer.valueOf(i));%0A        %7D%0A    %7D
 %0A%0A  
@@ -2336,39 +2336,87 @@
 n %7B%0A        
-output.writeUTF
+CALENDAR_TYPE_SERIALIZER.writeInt(output, CALENDAR_TYPE_IDS.get
 (calendar.ge
@@ -2431,16 +2431,17 @@
 rType())
+)
 ;%0A      
@@ -2551,24 +2551,79 @@
     
-output.writeUTF(
+DefaultExternalizer.TIME_ZONE.cast(TimeZone.class).writeObject(output, 
 cale
@@ -2644,16 +2644,8 @@
 ne()
-.getID()
 );%0A 
@@ -2643,38 +2643,61 @@
 one());%0A        
-output
+IndexSerializer.UNSIGNED_BYTE
 .writeInt(calend
@@ -2682,32 +2682,40 @@
 D_BYTE.writeInt(
+output, 
 calendar.getFirs
@@ -2733,30 +2733,53 @@
 ));%0A        
-output
+IndexSerializer.UNSIGNED_BYTE
 .writeInt(ca
@@ -2776,16 +2776,24 @@
 riteInt(
+output, 
 calendar
@@ -3010,31 +3010,76 @@
 darType(
-input.readUTF()
+CALENDAR_TYPE_NAMES%5BCALENDAR_TYPE_SERIALIZER.readInt(input)%5D
 )%0A      
@@ -3200,44 +3200,75 @@
 one(
-TimeZone.getTimeZone(input.readUTF()
+DefaultExternalizer.TIME_ZONE.cast(TimeZone.class).readObject(input
 ))%0A 
@@ -3301,21 +3301,45 @@
 inition(
-input
+IndexSerializer.UNSIGNED_BYTE
 .readInt
@@ -3343,16 +3343,45 @@
 Int(
-), 
 input
+), IndexSerializer.UNSIGNED_BYTE
 .rea
@@ -3385,16 +3385,21 @@
 readInt(
+input
 ))%0A     

Optimize Calendar externalizer