/**
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The ASF 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 org.apache.storm.testing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Tuple;


public class TupleCaptureBolt implements IRichBolt {
    public static final transient Map<String, Map<String, List<FixedTuple>>> emitted_tuples = new HashMap<>();

    private String name;
    private OutputCollector collector;

    public TupleCaptureBolt() {
        name = UUID.randomUUID().toString();
        emitted_tuples.put(name, new HashMap<String, List<FixedTuple>>());
    }

    @Override
    public void prepare(Map<String, Object> topoConf, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
    }

    @Override
    public void execute(Tuple input) {
        String component = input.getSourceComponent();
        Map<String, List<FixedTuple>> captured = emitted_tuples.get(name);
        if (!captured.containsKey(component)) {
            captured.put(component, new ArrayList<FixedTuple>());
        }
        captured.get(component).add(new FixedTuple(input.getSourceStreamId(), input.getValues()));
        collector.ack(input);
    }

    public Map<String, List<FixedTuple>> getResults() {
        return emitted_tuples.get(name);
    }

    @Override
    public void cleanup() {
    }

    public Map<String, List<FixedTuple>> getAndRemoveResults() {
        return emitted_tuples.remove(name);
    }

    public Map<String, List<FixedTuple>> getAndClearResults() {
        Map<String, List<FixedTuple>> ret = new HashMap<>(emitted_tuples.get(name));
        emitted_tuples.get(name).clear();
        return ret;
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        return null;
    }

}

@@ -936,16 +936,63 @@
 l.UUID;%0A
+import java.util.concurrent.ConcurrentHashMap;%0A
 import o
@@ -1212,17 +1212,16 @@
 Tuple;%0A%0A
-%0A
 public c
@@ -1265,26 +1265,380 @@
 hBolt %7B%0A
+%0A
     
-public
+/*%0A     * Even though normally bolts do not need to care about thread safety, this particular bolt is different.%0A     * It maintains a static field that is prepopulated before the topology starts, is written into by the topology,%0A     * and is then read from after the topology is completed - all of this by potentially different threads.%0A     */%0A%0A    private
  static 
@@ -1713,24 +1713,34 @@
 uples = new 
+Concurrent
 HashMap%3C%3E();
@@ -1753,16 +1753,22 @@
 private 
+final 
 String n
@@ -1922,24 +1922,34 @@
 t(name, new 
+Concurrent
 HashMap%3CStri
@@ -2258,34 +2258,92 @@
 ();%0A        
-Map%3C
+emitted_tuples.get(name)%0A            .compute(component, (
 String
+ key
 , List%3CFixed
@@ -2352,130 +2352,88 @@
 ple%3E
-%3E captured = emitted_tuples.get(name);%0A        if (!captured.containsKey(component)) %7B%0A            captured.put(component,
+ tuples) -%3E %7B%0A                if (tuples == null) %7B%0A                    tuples =
  new
@@ -2447,22 +2447,11 @@
 ist%3C
-FixedTuple
 %3E()
-)
 ;%0A  
@@ -2456,16 +2456,24 @@
         
+  
+      
 %7D%0A      
@@ -2478,31 +2478,22 @@
     
-captured.get(component)
+        tuples
 .add
@@ -2552,24 +2552,71 @@
 Values()));%0A
+                return tuples;%0A            %7D);%0A
         coll
@@ -3017,26 +3017,16 @@
 %3E re
-t = new HashMap%3C%3E(
+sults = 
 emit
@@ -3045,17 +3045,16 @@
 et(name)
-)
 ;%0A      
@@ -3059,32 +3059,83 @@
     
-emitted_tuples.get(name)
+Map%3CString, List%3CFixedTuple%3E%3E ret = new HashMap%3C%3E(results);%0A        results
 .cle

Make the TupleCaptureBolt thread-safe