/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.kernel.monitoring.statistics;

import com.liferay.portal.kernel.util.AutoResetThreadLocal;
import com.liferay.portal.kernel.util.ListUtil;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author Michael C. Han
 * @author Brian Wing Shun Chan
 */
public class DataSampleThreadLocal {

	public static void addDataSample(DataSample dataSample) {
		DataSampleThreadLocal dataSampleThreadLocal =
			_dataSampleThreadLocal.get();

		dataSampleThreadLocal._addDataSample(dataSample);
	}

	public static void clearDataSamples() {
		_dataSampleThreadLocal.remove();
	}

	public static List<DataSample> getDataSamples() {
		DataSampleThreadLocal dataSampleThreadLocal =
			_dataSampleThreadLocal.get();

		return ListUtil.fromCollection(dataSampleThreadLocal._getDataSamples());
	}

	public static void initialize() {
		_dataSampleThreadLocal.get();
	}

	@Override
	public Object clone() {
		return new DataSampleThreadLocal();
	}

	public long getMonitorTime() {
		return _monitorTime;
	}

	private DataSampleThreadLocal() {
		_monitorTime = System.currentTimeMillis();
	}

	private void _addDataSample(DataSample dataSample) {
		_dataSamples.add(dataSample);
	}

	private Queue<DataSample> _getDataSamples() {
		return _dataSamples;
	}

	private static ThreadLocal<DataSampleThreadLocal> _dataSampleThreadLocal =
		new AutoResetThreadLocal<DataSampleThreadLocal>(
			DataSampleThreadLocal.class + "._dataSampleThreadLocal") {

				@Override
				protected DataSampleThreadLocal copy(
					DataSampleThreadLocal dataSampleThreadLocal) {

					return dataSampleThreadLocal;
				}

				@Override
				protected DataSampleThreadLocal initialValue() {
					return new DataSampleThreadLocal();
				}

			};

	private Queue<DataSample> _dataSamples =
		new ConcurrentLinkedQueue<DataSample>();
	private long _monitorTime;

}