/*
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * 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 reactor.core.publisher;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.scheduler.TimedScheduler;

/**
 * @author Stephane Maldini
 */
final class FluxBufferTimeOrSize<T, C extends Collection<? super T>> extends FluxBatch<T, C> {

	final Supplier<C> bufferSupplier;

	public FluxBufferTimeOrSize(Publisher<T> source,
			int maxSize,
			long timespan,
			TimedScheduler timer,
			Supplier<C> bufferSupplier) {
		super(source, maxSize, timespan, timer);
		this.bufferSupplier = Objects.requireNonNull(bufferSupplier, "bufferSupplier");
	}

	@Override
	public void subscribe(Subscriber<? super C> subscriber) {
		source.subscribe(new BufferAction<>(prepareSub(subscriber),
				batchSize,
				timespan,
				timer.createWorker(),
				bufferSupplier));
	}

	final static class BufferAction<T, C extends Collection<? super T>> extends BatchAction<T, C> {

		final Supplier<C> bufferSupplier;
		volatile C values;

		public BufferAction(Subscriber<? super C> actual,
				int maxSize,
				long timespan,
				TimedScheduler.TimedWorker timer,
				Supplier<C> bufferSupplier) {
			super(actual, maxSize, false, timespan, timer);
			this.bufferSupplier = bufferSupplier;
		}

		@Override
		protected void doOnSubscribe(Subscription subscription) {
			values = bufferSupplier.get();
			subscriber.onSubscribe(this);
		}

		@Override
		protected void checkedError(Throwable ev) {
			C v = values;
			if(v != null) {
				v.clear();
				values = null;
			}
			subscriber.onError(ev);
		}

		@Override
		public void nextCallback(T value) {
			C v = values;
			if(v == null) {
				v = bufferSupplier.get();
				values = v;
			}
			v.add(value);
		}

		@Override
		public void flushCallback(T ev) {
			C v = values;
			if(v != null && !v.isEmpty()) {
				values = bufferSupplier.get();
				subscriber.onNext(v);
			}
		}
	}
}


@@ -2184,24 +2184,50 @@
 wable ev) %7B%0A
+%09%09%09synchronized (this) %7B%0A%09
 %09%09%09C v = val
@@ -2226,32 +2226,33 @@
  v = values;%0A%09%09%09
+%09
 if(v != null) %7B%0A
@@ -2255,16 +2255,17 @@
 ) %7B%0A%09%09%09%09
+%09
 v.clear(
@@ -2263,24 +2263,25 @@
 %09v.clear();%0A
+%09
 %09%09%09%09values =
@@ -2287,16 +2287,22 @@
 = null;%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09
@@ -2376,24 +2376,50 @@
 (T value) %7B%0A
+%09%09%09synchronized (this) %7B%0A%09
 %09%09%09C v = val
@@ -2415,32 +2415,33 @@
 %09%09C v = values;%0A
+%09
 %09%09%09if(v == null)
@@ -2439,24 +2439,25 @@
  == null) %7B%0A
+%09
 %09%09%09%09v = buff
@@ -2474,24 +2474,25 @@
 .get();%0A%09%09%09%09
+%09
 values = v;%0A
@@ -2491,24 +2491,25 @@
 es = v;%0A
+%09
 %09%09%09%7D%0A%09%09%09
 v.add(va
@@ -2500,16 +2500,17 @@
 %09%09%09%7D%0A%09%09%09
+%09
 v.add(va
@@ -2515,16 +2515,21 @@
 value);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09@
@@ -2590,21 +2590,74 @@
 values;%0A
+%09%09%09boolean flush = false;%0A%09%09%09synchronized (this) %7B%0A%09
 %09%09%09if
+ 
 (v != nu
@@ -2678,24 +2678,25 @@
 ty()) %7B%0A%09%09%09%09
+%09
 values = buf
@@ -2710,24 +2710,71 @@
 lier.get();%0A
+%09%09%09%09%09flush = true;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09if (flush) %7B%0A
 %09%09%09%09subscrib

fix #320 FluxBufferTimeOrSize concurrently modified buffer