/*
 * 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.smile;

import com.ning.metrics.serialization.event.Granularity;
import com.ning.metrics.serialization.event.SmileBucketEvent;
import com.ning.metrics.serialization.event.SmileEnvelopeEvent;
import org.codehaus.jackson.JsonNode;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;

public class JsonStreamToSmileBucketEvent
{
    /**
     * Given a stream of Json (smile or plain Json) events of specified type,
     * construct a SmileBucketEvent wrapper. Granularity is set to HOURLY.
     * We return a collection here because events are grouped by output path.
     *
     * @param eventName events type (all events in the stream are supposed to be of this type)
     * @param in        Json (smile or plain) stream of events
     * @return Event wrappers around these events
     * @throws IOException generic serialization exception
     */
    public static Collection<SmileBucketEvent> extractEvent(String eventName, InputStream in) throws IOException
    {
        return extractEvent(eventName, Granularity.HOURLY, in);
    }

    /**
     * Given a stream of Json (smile or plain Json) events of specified type and granularity,
     * construct SmileBucketEvent wrappers. We return a collection here because events are grouped by
     * output path.
     *
     * @param eventName   events type (all events in the stream are supposed to be of this type)
     * @param granularity events granularity
     * @param in          Json (smile or plain) stream of events
     * @return Event wrappers around these events
     * @throws IOException generic serialization exception
     */
    public static Collection<SmileBucketEvent> extractEvent(String eventName, Granularity granularity, InputStream in) throws IOException
    {
        SmileBucket bucket = SmileBucketDeserializer.deserialize(in);

        ArrayList<SmileEnvelopeEvent> events = new ArrayList<SmileEnvelopeEvent>();
        for (JsonNode node : bucket) {
            events.add(new SmileEnvelopeEvent(eventName, granularity, node));
        }

        return SmileEnvelopeEventsToSmileBucketEvents.extractEvents(events);
    }
}

@@ -668,65 +668,8 @@
 e;%0A%0A
-import com.ning.metrics.serialization.event.Granularity;%0A
 impo
@@ -722,24 +722,24 @@
 ucketEvent;%0A
+
 import com.n
@@ -991,719 +991,8 @@
 t%0A%7B%0A
-    /**%0A     * Given a stream of Json (smile or plain Json) events of specified type,%0A     * construct a SmileBucketEvent wrapper. Granularity is set to HOURLY.%0A     * We return a collection here because events are grouped by output path.%0A     *%0A     * @param eventName events type (all events in the stream are supposed to be of this type)%0A     * @param in        Json (smile or plain) stream of events%0A     * @return Event wrappers around these events%0A     * @throws IOException generic serialization exception%0A     */%0A    public static Collection%3CSmileBucketEvent%3E extractEvent(String eventName, InputStream in) throws IOException%0A    %7B%0A        return extractEvent(eventName, Granularity.HOURLY, in);%0A    %7D%0A%0A
     
@@ -1319,53 +1319,8 @@
 pe)%0A
-     * @param granularity events granularity%0A
     
@@ -1578,33 +1578,8 @@
 ame,
- Granularity granularity,
  Inp
@@ -1810,16 +1810,16 @@
 cket) %7B%0A
+
         
@@ -1866,29 +1866,16 @@
 entName,
- granularity,
  node));

simplify utility function JsonStreamToSmileBucketEvent