<commit_before>/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.trends.databrowser3.model;

import static org.csstudio.javafx.rtplot.Activator.logger;

import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.logging.Level;

import org.csstudio.javafx.rtplot.data.InstrumentedReadWriteLock;
import org.csstudio.javafx.rtplot.data.PlotDataProvider;

/** Base for classes that hold plot samples
 *  in a way accessible as {@link PlotDataProvider}
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
abstract public class PlotSamples implements PlotDataProvider<Instant>
{
    protected final ReadWriteLock lock = new InstrumentedReadWriteLock();

    /** To be set when samples change
     *  @see #testAndClearNewSamplesFlag()
     */
    final protected AtomicBoolean have_new_samples = new AtomicBoolean();

    /** Lock for writing */
    public boolean lockForWriting()
    {
        try
        {
            if (! lock.writeLock().tryLock(10, TimeUnit.SECONDS))
                throw new TimeoutException();
        }
        catch (Exception ex)
        {
            logger.log(Level.WARNING, "Cannot lock " + this, ex);
        }
        return false;
    }

    /** Un-lock after writing */
    public void unlockForWriting()
    {
        lock.writeLock().unlock();
        have_new_samples.set(true);
    }

    /** {@inheritDoc} */
    @Override
    public Lock getLock()
    {
        return lock.readLock();
    }

    /** {@inheritDoc} */
    @Override
    abstract public int size();

    /** {@inheritDoc} */
    @Override
    abstract public PlotSample get(int index);

    /** Test if samples changed since the last time
     *  <code>testAndClearNewSamplesFlag</code> was called.
     *  @return <code>true</code> if there were new samples
     */
    public boolean hasNewSamples()
    {
        return have_new_samples.get();
    }

    /** Test if samples changed since the last time this method was called.
     *  @return <code>true</code> if there were new samples
     */
    public boolean testAndClearNewSamplesFlag()
    {
        return have_new_samples.getAndSet(false);
    }

    /** @return Info text about PlotSamples for debugging */
    @SuppressWarnings("nls")
    @Override
    public String toString()
    {
        final StringBuilder buf = new StringBuilder();
        buf.append("Plot Samples, lock state: ").append(lock);
        if (getLock().tryLock())<commit_msg>Databrowser: Updated lockForWriting() failed to return 'true' on success<commit_after>/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.trends.databrowser3.model;

import static org.csstudio.javafx.rtplot.Activator.logger;

import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.logging.Level;

import org.csstudio.javafx.rtplot.data.InstrumentedReadWriteLock;
import org.csstudio.javafx.rtplot.data.PlotDataProvider;

/** Base for classes that hold plot samples
 *  in a way accessible as {@link PlotDataProvider}
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
abstract public class PlotSamples implements PlotDataProvider<Instant>
{
    protected final ReadWriteLock lock = new InstrumentedReadWriteLock();

    /** To be set when samples change
     *  @see #testAndClearNewSamplesFlag()
     */
    final protected AtomicBoolean have_new_samples = new AtomicBoolean();

    /** Lock for writing */
    public boolean lockForWriting()
    {
        try
        {
            if (lock.writeLock().tryLock(10, TimeUnit.SECONDS))
                return true;
            throw new TimeoutException();
        }
        catch (Exception ex)
        {
            logger.log(Level.WARNING, "Cannot lock " + this, ex);
        }
        return false;
    }

    /** Un-lock after writing */
    public void unlockForWriting()
    {
        lock.writeLock().unlock();
        have_new_samples.set(true);
    }

    /** {@inheritDoc} */
    @Override
    public Lock getLock()
    {
        return lock.readLock();
    }

    /** {@inheritDoc} */
    @Override
    abstract public int size();

    /** {@inheritDoc} */
    @Override
    abstract public PlotSample get(int index);

    /** Test if samples changed since the last time
     *  <code>testAndClearNewSamplesFlag</code> was called.
     *  @return <code>true</code> if there were new samples
     */
    public boolean hasNewSamples()
    {
        return have_new_samples.get();
    }

    /** Test if samples changed since the last time this method was called.
     *  @return <code>true</code> if there were new samples
     */
    public boolean testAndClearNewSamplesFlag()
    {
        return have_new_samples.getAndSet(false);
    }

    /** @return Info text about PlotSamples for debugging */
    @Override
    public String toString()
    {
        final StringBuilder buf = new StringBuilder();
        buf.append("Plot Samples, lock state: ").append(lock);
        if (getLock().tryLock())
{'fraction_non_alphanumeric': 0.1500730875426344, 'fraction_numerical': 0.003573168751015105, 'mean_word_length': 3.1190635451505018, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7065773', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1620, 'n_words': 462}