<commit_before>/*******************************************************************************
 * Copyright (c) 2015-2018 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.display.builder.representation.javafx.widgets.plots;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

import org.csstudio.javafx.rtplot.data.InstrumentedReadWriteLock;
import org.csstudio.javafx.rtplot.data.PlotDataItem;
import org.csstudio.javafx.rtplot.data.PlotDataProvider;
import org.csstudio.javafx.rtplot.data.SimpleDataItem;
import org.diirt.util.array.ArrayDouble;
import org.diirt.util.array.ListNumber;

/** Data provider for RTPlot
 *
 *  <p>Adapts waveforms received from PV
 *  into samples for a trace in the RTPlot.
 *  <ul>
 *  <li>X and Y waveform: Plots Y over X
 *  <li>Y waveform with <code>null</code> for X: Plots Y over array index
 *  </ul>
 *
 *  Error data may be
 *  <ul>
 *  <li><code>null</code> or zero elements: No error bars
 *  <li>One element: Use that error for all samples
 *  <li>One element per sample: Error bar for each sample
 *  </ul>
 *
 *  @author <NAME>
 */
public class XYVTypeDataProvider implements PlotDataProvider<Double>
{
    public static final ListNumber EMPTY = new ArrayDouble(new double[0], true);

    /** Sharing the _read_ half of just one lock.
     *  Never using the _write_ half, since this class is immutable
     */
    private static final ReadWriteLock lock = new InstrumentedReadWriteLock();


    private final PlotDataItem<Double>[] items;

    /** Set the plot's data
     *  @param x_data X data, may be <code>null</code>
     *  @param y_data Y data
     *  @param error_data Error data
     */
    @SuppressWarnings("unchecked")
    public XYVTypeDataProvider(ListNumber x_data, ListNumber y_data, ListNumber error_data)
    {
        // In principle, error_data should have 1 element or same size as X and Y..
        if (error_data != null)
            error_data = EMPTY;

        // Could create each PlotDataItem lazily in get(),
        // but create array of PlotDataItems right now because
        // plot will likely iterate over the data elements at least once to plot value,
        // maybe again to plot outline, find value at cursor etc.
        final int size = x_data == null ? y_data.size() : Math.min(x_data.size(), y_data.size());
        items = new PlotDataItem[size];<commit_msg>XYPlot: Fix NPE with missing error data<commit_after>/*******************************************************************************
 * Copyright (c) 2015-2018 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.display.builder.representation.javafx.widgets.plots;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

import org.csstudio.javafx.rtplot.data.InstrumentedReadWriteLock;
import org.csstudio.javafx.rtplot.data.PlotDataItem;
import org.csstudio.javafx.rtplot.data.PlotDataProvider;
import org.csstudio.javafx.rtplot.data.SimpleDataItem;
import org.diirt.util.array.ArrayDouble;
import org.diirt.util.array.ListNumber;

/** Data provider for RTPlot
 *
 *  <p>Adapts waveforms received from PV
 *  into samples for a trace in the RTPlot.
 *  <ul>
 *  <li>X and Y waveform: Plots Y over X
 *  <li>Y waveform with <code>null</code> for X: Plots Y over array index
 *  </ul>
 *
 *  Error data may be
 *  <ul>
 *  <li><code>null</code> or zero elements: No error bars
 *  <li>One element: Use that error for all samples
 *  <li>One element per sample: Error bar for each sample
 *  </ul>
 *
 *  @author <NAME>
 */
public class XYVTypeDataProvider implements PlotDataProvider<Double>
{
    public static final ListNumber EMPTY = new ArrayDouble(new double[0], true);

    /** Sharing the _read_ half of just one lock.
     *  Never using the _write_ half, since this class is immutable
     */
    private static final ReadWriteLock lock = new InstrumentedReadWriteLock();


    private final PlotDataItem<Double>[] items;

    /** Set the plot's data
     *  @param x_data X data, may be <code>null</code>
     *  @param y_data Y data
     *  @param error_data Error data
     */
    @SuppressWarnings("unchecked")
    public XYVTypeDataProvider(ListNumber x_data, ListNumber y_data, ListNumber error_data)
    {
        // In principle, error_data should have 1 element or same size as X and Y..
        if (error_data == null)
            error_data = EMPTY;

        // Could create each PlotDataItem lazily in get(),
        // but create array of PlotDataItems right now because
        // plot will likely iterate over the data elements at least once to plot value,
        // maybe again to plot outline, find value at cursor etc.
        final int size = x_data == null ? y_data.size() : Math.min(x_data.size(), y_data.size());
        items = new PlotDataItem[size];
{'fraction_non_alphanumeric': 0.14680073126142595, 'fraction_numerical': 0.005118829981718464, 'mean_word_length': 3.6482582837723023, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '7065954', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1545, 'n_words': 535}