/*
 * Copyright 2010-2011 Ning, Inc.
 *
 * Ning licenses this file to you 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 com.ning.metrics.serialization.writer;

import com.ning.metrics.serialization.event.Event;
import com.ning.metrics.serialization.event.EventSerializer;

import java.io.FileOutputStream;
import java.io.IOException;

public class ObjectOutputterFactory
{
    // TODO Does this actually work!? This is using generics in crazy ways!
    public static <T extends Event> ObjectOutputter<T> createObjectOutputter(FileOutputStream out, SyncType type, int batchSize) throws IOException
    {
        return createObjectOutputter(out, type, batchSize, new ObjectOutputEventSerializer<T>());
    }

    /**
     * @param out
     * @param type
     * @param batchSize
     * @param eventSerializer does not have to be tied to 'out'. We will call eventSerializer.open(out) later.
     * If eventSerializer == null, it's the same as calling the default createObjectOutputter()
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T extends Event> ObjectOutputter<T> createObjectOutputter(FileOutputStream out, SyncType type, int batchSize, EventSerializer<T> eventSerializer) throws IOException
    {
        if (eventSerializer == null) {
            return createObjectOutputter(out, type, batchSize);
        }

        switch (type) {
            case NONE:
                return new DefaultObjectOutputter<T>(out, eventSerializer);
            case FLUSH:
                return new FlushingObjectOutputter<T>(out, eventSerializer, batchSize);
            case SYNC:
                return new SyncingObjectOutputter<T>(out, eventSerializer, batchSize);
        }

        throw new IllegalArgumentException("unable to construct ObjectOutputter given type");
    }
}

@@ -882,84 +882,8 @@
 y%0A%7B%0A
-    // TODO Does this actually work!? This is using generics in crazy ways!%0A
     
@@ -947,32 +947,38 @@
 ObjectOutputter(
+final 
 FileOutputStream
@@ -974,32 +974,38 @@
 utputStream out,
+ final
  SyncType type, 
@@ -995,32 +995,38 @@
 l SyncType type,
+ final
  int batchSize) 
@@ -1184,51 +1184,187 @@
  out
-%0A     * @param type%0A     * @param batchSize
+             underlying FileOutputStream%0A     * @param type            type of outputter (flush, sync, ...)%0A     * @param batchSize       number of events between flushes or syncs
 %0A   
@@ -1477,16 +1477,39 @@
 .%0A     *
+                       
  If even
@@ -1611,50 +1611,181 @@
  %3CT%3E
-%0A     * @return%0A     * @throws IOException
+             Event type (e.g. ThriftEnvelopeEvent)%0A     * @return a new outputter object of type type%0A     * @throws IOException when unable to open the FileOutputStream out
 %0A   
@@ -1866,16 +1866,31 @@
 tputter(
+%0A        final 
 FileOutp
@@ -1902,16 +1902,30 @@
 eam out,
+%0A        final
  SyncTyp
@@ -1931,16 +1931,30 @@
 pe type,
+%0A        final
  int bat
@@ -1960,16 +1960,30 @@
 tchSize,
+%0A        final
  EventSe
@@ -2009,16 +2009,21 @@
 rializer
+%0A    
 ) throws
@@ -2555,17 +2555,17 @@
 eption(%22
-u
+U
 nable to
@@ -2602,16 +2602,23 @@
 en type%22
+ + type
 );%0A    %7D

fix Javadocs in ObjectOutputterFactory