/*
 * 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 com.addthis.hydra.task.source;

import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicLong;

import com.addthis.bundle.core.Bundle;
import com.addthis.bundle.core.kvp.KVBundle;
import com.addthis.codec.annotations.FieldConfig;

/**
 * This data source <span class="hydra-summary">creates an arbitrary number of empty bundles</span>.
 * <p/>
 * <p>The maxPackets parameter determines how many bundles are created. The default
 * value of maxPackets (-1) indicates that an infinite number of bundles are created.
 *
 * @user-reference
 * @hydra-name empty
 */
public class DataSourceEmpty extends TaskDataSource {

    /**
     * Number of bundles that will be created.
     * Default is -1 which creates an infinite number of bundles.
     */
    @FieldConfig(codable = true)
    private long maxPackets = -1; // go forever

    private AtomicLong packetsCreated = new AtomicLong(0);
    private final KVBundle peek = createBundle();

    private KVBundle createBundle() {
        KVBundle bundle = new KVBundle();
        return bundle;
    }

    @Override
    public void init() {
    }

    @Override
    public Bundle peek() {
        if (maxPackets < 0 || packetsCreated.get() < maxPackets) {
            return peek;
        } else {
            return null;
        }
    }

    @Override
    public Bundle next() {
        if (peek() != null) {
            packetsCreated.getAndIncrement();
            return createBundle();
        } else {
            throw new NoSuchElementException();
        }
    }

    @Override
    public void close() {
    }
}

@@ -606,83 +606,71 @@
 java
-.util.NoSuchElementException;%0Aimport java.util.concurrent.atomic.AtomicL
+x.annotation.Nullable;%0A%0Aimport java.util.NoSuchElementExcepti
 on
-g
 ;%0A%0Ai
@@ -1337,28 +1337,8 @@
 nfig
-(codable = true)%0A   
  pri
@@ -1395,53 +1395,39 @@
 ate 
-AtomicLong packetsCreated = new AtomicLong(0)
+volatile boolean closed = false
 ;%0A  
@@ -1491,74 +1491,76 @@
 ate 
-KVBundle createBundle() %7B%0A        KVBundle bundle = new KV
+long packetsCreated = 0;%0A%0A    private static KVBundle create
 Bundle()
 ;%0A  
@@ -1547,33 +1547,34 @@
 e createBundle()
-;
+ %7B
 %0A        return 
@@ -1573,22 +1573,30 @@
  return 
-b
+new KVB
 undle
+()
 ;%0A    %7D%0A
@@ -1601,36 +1601,32 @@
 %7D%0A%0A    @Override
-%0A   
  public void ini
@@ -1630,23 +1630,32 @@
 init() %7B
+%7D%0A
 %0A    
-%7D%0A
+@Nullable
 %0A    @Ov
@@ -1699,16 +1699,41 @@
      if 
+(!closed%0A            && (
 (maxPack
@@ -1743,12 +1743,14 @@
  %3C 0
+)
  %7C%7C 
+(
 pack
@@ -1763,14 +1763,8 @@
 ated
-.get()
  %3C m
@@ -1773,16 +1773,18 @@
 Packets)
+))
  %7B%0A     
@@ -1867,36 +1867,32 @@
 %7D%0A%0A    @Override
-%0A   
  public Bundle n
@@ -1959,26 +1959,13 @@
 ated
-.getAndIncrement()
+ += 1
 ;%0A  
@@ -2084,36 +2084,32 @@
 %7D%0A%0A    @Override
-%0A   
  public void clo
@@ -2111,20 +2111,43 @@
 d close() %7B%0A
+        closed = true;%0A
     %7D%0A%7D%0A

clean up DSEmpty and have it support "stop"