<commit_before>package com.netflix.hystrix.examples.basic;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import rx.Observable;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.observers.TestSubscriber;
import rx.schedulers.Schedulers;

import com.netflix.hystrix.HystrixCollapser.CollapsedRequest;
import com.netflix.hystrix.HystrixObservableCollapser;
import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.HystrixRequestLog;
import com.netflix.hystrix.examples.basic.ObservableCommandNumbersToWords.NumberWord;
import com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

/**
 * Example that uses {@link HystrixObservableCollapser} to batch multiple {@link ObservableCommandNumbersToWords} requests.
 *
 * @author <NAME>
 */
public class ObservableCollapserGetWordForNumber extends HystrixObservableCollapser<Integer, NumberWord, String, Integer>
{
	private final Integer number;

	private final static AtomicInteger counter = new AtomicInteger();

	public static void resetCmdCounter()
	{
		counter.set(0);
	}

	public static int getCmdCount()
	{
		return counter.get();
	}

	public ObservableCollapserGetWordForNumber(final Integer number)
	{
		this.number = number;
	}

	@Override
	public Integer getRequestArgument()
	{
		return number;
	}

	@SuppressWarnings("boxing")
	@Override
	protected HystrixObservableCommand<NumberWord> createCommand(final Collection<CollapsedRequest<String, Integer>> requests)
	{
		final int count = counter.incrementAndGet();
		System.out.println("Creating batch for " + requests.size() + " requests. Total invocations so far: " + count);

		final List<Integer> numbers = new ArrayList<Integer>();
		for (final CollapsedRequest<String, Integer> request : requests)
		{
			numbers.add(request.getArgument());
		}

		return new ObservableCommandNumbersToWords(numbers);
	}

	@Override
	protected Func1<NumberWord, Integer> getBatchReturnTypeKeySelector()
	{
		return new Func1<NumberWord, Integer>()
		{
			@Override
			public Integer call(final NumberWord nw)
			{
				return nw.getNumber();
			}
		};
	}

	@Override
	protected Func1<Integer, Integer> getRequestArgumentKeySelector()
	{
		return new Func1<Integer, Integer>()
		{
			@Override
			public Integer call(final Integer no)
			{
				return no;
			}

		};
	}

	@Override
	protected Func1<NumberWord, String> getBatchReturnTypeToResponseTypeMapper()
	{
		return new Func1<NumberWord, String>()
		{
			@Override
			public String call(final NumberWord nw)
			{
				return nw.getWord();
			}
		};
	}

	@Override
	protected void onMissingResponse(final CollapsedRequest<String, Integer> request)
	{
		request.setException(new Exception("No word"));
	}

	public static class ObservableCollapserGetWordForNumberTest
	{
		private HystrixRequestContext ctx;
<commit_msg>add java 8 examples as comment<commit_after>package com.netflix.hystrix.examples.basic;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import rx.Observable;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.observers.TestSubscriber;
import rx.schedulers.Schedulers;

import com.netflix.hystrix.HystrixCollapser.CollapsedRequest;
import com.netflix.hystrix.HystrixObservableCollapser;
import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.HystrixRequestLog;
import com.netflix.hystrix.examples.basic.ObservableCommandNumbersToWords.NumberWord;
import com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

/**
 * Example that uses {@link HystrixObservableCollapser} to batch multiple {@link ObservableCommandNumbersToWords} requests.
 *
 * @author <NAME>
 */
public class ObservableCollapserGetWordForNumber extends HystrixObservableCollapser<Integer, NumberWord, String, Integer>
{
	private final Integer number;

	private final static AtomicInteger counter = new AtomicInteger();

	public static void resetCmdCounter()
	{
		counter.set(0);
	}

	public static int getCmdCount()
	{
		return counter.get();
	}

	public ObservableCollapserGetWordForNumber(final Integer number)
	{
		this.number = number;
	}

	@Override
	public Integer getRequestArgument()
	{
		return number;
	}

	@SuppressWarnings("boxing")
	@Override
	protected HystrixObservableCommand<NumberWord> createCommand(final Collection<CollapsedRequest<String, Integer>> requests)
	{
		final int count = counter.incrementAndGet();
		System.out.println("Creating batch for " + requests.size() + " requests. Total invocations so far: " + count);

		final List<Integer> numbers = new ArrayList<Integer>();
		for (final CollapsedRequest<String, Integer> request : requests)
		{
			numbers.add(request.getArgument());
		}

		return new ObservableCommandNumbersToWords(numbers);
	}

	@Override
	protected Func1<NumberWord, Integer> getBatchReturnTypeKeySelector()
	{
		// Java 8: (final NumberWord nw) -> nw.getNumber();

		return new Func1<NumberWord, Integer>()
		{
			@Override
			public Integer call(final NumberWord nw)
			{
				return nw.getNumber();
			}
		};
	}

	@Override
	protected Func1<Integer, Integer> getRequestArgumentKeySelector()
	{
		// Java 8: return (final Integer no) -> no;

		return new Func1<Integer, Integer>()
		{
			@Override
			public Integer call(final Integer no)
			{
				return no;
			}

		};
	}

	@Override
	protected Func1<NumberWord, String> getBatchReturnTypeToResponseTypeMapper()
	{
		// Java 8: return (final NumberWord nw) -> nw.getWord();

		return new Func1<NumberWord, String>()
		{
			@Override
			public String call(final NumberWord nw)
			{
				return nw.getWord();
			}
		};
	}

	@Override
	protected void onMissingResponse(final CollapsedRequest<String, Integer> request)
	{
		request.setException(new Exception("No word"));
	}

	public static class ObservableCollapserGetWordForNumberTest
	{
		private HystrixRequestContext ctx;

{'fraction_non_alphanumeric': 0.0991083174974419, 'fraction_numerical': 0.00321590410758661, 'mean_word_length': 5.231329690346084, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4050465', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2115, 'n_words': 465}