001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.monitoring.statistics;
016    
017    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018    import com.liferay.portal.kernel.util.ListUtil;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    /**
024     * @author Michael C. Han
025     * @author Brian Wing Shun Chan
026     */
027    public class DataSampleThreadLocal implements Cloneable {
028    
029            public static void addDataSample(DataSample dataSample) {
030                    _dataSampleThreadLocal.get()._addDataSample(dataSample);
031            }
032    
033            public static void clearDataSamples() {
034                    _dataSampleThreadLocal.remove();
035            }
036    
037            public static List<DataSample> getDataSamples() {
038                    return ListUtil.copy(_dataSampleThreadLocal.get()._getDataSamples());
039            }
040    
041            @Override
042            public Object clone() {
043                    return new DataSampleThreadLocal();
044            }
045    
046            public long getMonitorTime() {
047                    return _monitorTime;
048            }
049    
050            private DataSampleThreadLocal() {
051                    _monitorTime = System.currentTimeMillis();
052            }
053    
054            private void _addDataSample(DataSample dataSample) {
055                    _dataSamples.add(dataSample);
056            }
057    
058            private List<DataSample> _getDataSamples() {
059                    return _dataSamples;
060            }
061    
062            private static ThreadLocal<DataSampleThreadLocal> _dataSampleThreadLocal =
063                    new AutoResetThreadLocal<DataSampleThreadLocal>(
064                            DataSampleThreadLocal.class + "._dataSampleThreadLocal",
065                            new DataSampleThreadLocal());
066    
067            private List<DataSample> _dataSamples = new ArrayList<DataSample>();
068            private long _monitorTime;
069    
070    }