All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.diirt.datasource.extra.DynamicGroupFunction Maven / Gradle / Ivy

/**
 * Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.datasource.extra;

import java.util.ArrayList;
import java.util.List;
import org.diirt.datasource.ReadFunction;

/**
 * Function that implements the dynamic group.
 *
 * @author carcassi
 */
class DynamicGroupFunction implements ReadFunction> {
    
    // Guarded by this
    private final List> arguments = new ArrayList>();
    // Guarded by this
    private List exceptions = new ArrayList();
    // Gaurded by this
    private List previousValues = new ArrayList();

    @Override
    public synchronized List readValue() {
        List result = new ArrayList();
        for (int i = 0; i < arguments.size(); i++) {
            ReadFunction function = arguments.get(i);
            try {
                // Compute the new value for the ith function.
                // If the value changed, reset the exception
                result.add(function.readValue());
                if (result.get(i) != previousValues.get(i)) {
                    exceptions.set(i, null);
                }
            } catch (Exception ex) {
                // Computation of value failed. Leave last value
                // and update exception
                exceptions.set(i, ex);
            }
        }
        previousValues = result;
        return result;
    }

    List> getArguments() {
        return arguments;
    }
    
    List getExceptions() {
        return exceptions;
    }
    
    List getPreviousValues() {
        return previousValues;
    }
    
}