<commit_before>/*
 * Copyright (c) 2010 Stiftung Deutsches Elektronen-Synchrotron,
 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
 *
 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
 * OR MODIFICATIONS.
 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
 */
package org.csstudio.domain.desy.calc;



import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.epics.pvmanager.Function;
import org.epics.pvmanager.ValueCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Abstract value cache wrapper that offers an accumulation method to modify
 * the cached value.
 *
 * @author bknerr
 * @since 26.11.2010
 * @param <A> the type of the value to be accumulated
 * @param <R> the return type of the accumulation result
 */
public abstract class AbstractAccumulatorCache<A, R> extends Function<R> {

    private static final Logger LOG =
        LoggerFactory.getLogger(AbstractAccumulatorCache.class);

    private final ValueCache<R> _accumulatedValue;
    private long _num;

    /**
     * Constructor.
     * @param type
     */
    public AbstractAccumulatorCache(@Nonnull final Class<R> type) {
        _accumulatedValue = new ValueCache<R>(type);
        _num = 0;
    }

    public void accumulate(@Nonnull final A nextValue) {
        final R val = calculateAccumulation(_accumulatedValue.getValue(), nextValue);
        if (_num + 1 >= Integer.MAX_VALUE) {
            LOG.warn("Potential accumulation overflow detected - accumulator is cleared.");
            clear();
        }
        _num++;
        _accumulatedValue.setValue(val);
    }


    public void clear() {
        _accumulatedValue.setValue(null);
        _num = 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @CheckForNull
    public R getValue() {<commit_msg>o.c.domain.desy: changed overflow detection behaviour, keep last accumulated value, reset number of accumulations<commit_after>/*
 * Copyright (c) 2010 Stiftung Deutsches Elektronen-Synchrotron,
 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
 *
 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
 * OR MODIFICATIONS.
 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
 */
package org.csstudio.domain.desy.calc;



import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.epics.pvmanager.Function;
import org.epics.pvmanager.ValueCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Abstract value cache wrapper that offers an accumulation method to modify
 * the cached value.
 *
 * @author bknerr
 * @since 26.11.2010
 * @param <A> the type of the value to be accumulated
 * @param <R> the return type of the accumulation result
 */
public abstract class AbstractAccumulatorCache<A, R> extends Function<R> {

    private static final Logger LOG =
        LoggerFactory.getLogger(AbstractAccumulatorCache.class);

    private final ValueCache<R> _accumulatedValue;
    private long _num;

    /**
     * Constructor.
     * @param type
     */
    public AbstractAccumulatorCache(@Nonnull final Class<R> type) {
        _accumulatedValue = new ValueCache<R>(type);
        _num = 0;
    }

    public void accumulate(@Nonnull final A nextValue) {
        final R val = calculateAccumulation(_accumulatedValue.getValue(), nextValue);
        if (_num + 1 >= Integer.MAX_VALUE) {
            LOG.warn("Potential accumulation overflow detected - number of accumulations is reset to zero.");
            _num = 0;
        }
        _num++;
        _accumulatedValue.setValue(val);
    }


    public void clear() {
        _accumulatedValue.setValue(null);
        _num = 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @CheckForNull
    public R getValue() {
{'fraction_non_alphanumeric': 0.0771568461928077, 'fraction_numerical': 0.005909167651527942, 'mean_word_length': 3.460843373493976, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5223160', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1694, 'n_words': 643}