<commit_before>/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.pvmanager;

import com.sun.org.apache.bcel.internal.generic.DSTORE;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implements a {@link ChannelHandler} on top of a single subscription and
 * multiplexes all reads on top of it.
 *
 * @param ConnectionPayload type of the payload for the connection
 * @param MessagePayload type of the payload for each message
 * @author carcassi
 */
public abstract class MultiplexedChannelHandler<ConnectionPayload, MessagePayload> extends ChannelHandler {

    private static final Logger log = Logger.getLogger(MultiplexedChannelHandler.class.getName());
    private int readUsageCounter = 0;
    private int writeUsageCounter = 0;
    private MessagePayload lastMessage;
    private ConnectionPayload connectionPayload;
    private Map<Collector<?>, MonitorHandler> monitors = new ConcurrentHashMap<Collector<?>, MonitorHandler>();
    private List<ExceptionHandler> writeExceptionHandlers = new ArrayList<ExceptionHandler>();

    private class MonitorHandler {

        private final Collector<?> collector;
        private final ValueCache<?> cache;
        private final ExceptionHandler exceptionHandler;
        private DataSourceTypeAdapter<ConnectionPayload, MessagePayload> typeAdapter;

        public MonitorHandler(Collector<?> collector, ValueCache<?> cache, ExceptionHandler exceptionHandler) {
            this.collector = collector;
            this.cache = cache;
            this.exceptionHandler = exceptionHandler;
        }

        public final void processValue(MessagePayload payload) {
            if (typeAdapter == null)
                return;
            
            // Lock the collector and prepare the new value.
            synchronized (collector) {
                try {
                    if (typeAdapter.updateCache(cache, getConnectionPayload(), payload)) {
                        collector.collect();
                    }
                } catch (RuntimeException e) {
                    exceptionHandler.handleException(e);
                }
            }
        }
        
        public final void findTypeAdapter() {
            if (getConnectionPayload() == null) {
                typeAdapter = null;
            } else {
                try {
                    typeAdapter = MultiplexedChannelHandler.this.findTypeAdapter(cache, getConnectionPayload());
                } catch(RuntimeException ex) {
                    exceptionHandler.handleException(ex);
                }
            }
        }
        
    }
    
    protected synchronized final void notifyAllReaders(Exception ex) {
        for (MonitorHandler monitor : monitors.values()) {
            monitor.exceptionHandler.handleException(ex);
        }
    }

    protected final ConnectionPayload getConnectionPayload() {
        return connectionPayload;
    }
    
    protected final MessagePayload getLastMessagePayload() {
        return lastMessage;
    }

    protected final void processConnection(ConnectionPayload connectionPayload) {
        this.connectionPayload = connectionPayload;
        
        for (MonitorHandler monitor : monitors.values()) {
            monitor.findTypeAdapter();
        }
    }
    
    private static DataSourceTypeAdapter<?, ?> defaultTypeAdapter = new DataSourceTypeAdapter<Object, Object>() {

            @Override
            public int match(ValueCache<?> cache, Object connection) {
                return 1;
            }

            @Override
            public Object getSubscriptionParameter(ValueCache<?> cache, Object connection) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public boolean updateCache(ValueCache cache, Object connection, Object message) {<commit_msg>Fixed synchronization.<commit_after>/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.pvmanager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implements a {@link ChannelHandler} on top of a single subscription and
 * multiplexes all reads on top of it.
 *
 * @param ConnectionPayload type of the payload for the connection
 * @param MessagePayload type of the payload for each message
 * @author carcassi
 */
public abstract class MultiplexedChannelHandler<ConnectionPayload, MessagePayload> extends ChannelHandler {

    private static final Logger log = Logger.getLogger(MultiplexedChannelHandler.class.getName());
    private int readUsageCounter = 0;
    private int writeUsageCounter = 0;
    private MessagePayload lastMessage;
    private ConnectionPayload connectionPayload;
    private Map<Collector<?>, MonitorHandler> monitors = new ConcurrentHashMap<Collector<?>, MonitorHandler>();
    private List<ExceptionHandler> writeExceptionHandlers = new ArrayList<ExceptionHandler>();

    private class MonitorHandler {

        private final Collector<?> collector;
        private final ValueCache<?> cache;
        private final ExceptionHandler exceptionHandler;
        private DataSourceTypeAdapter<ConnectionPayload, MessagePayload> typeAdapter;

        public MonitorHandler(Collector<?> collector, ValueCache<?> cache, ExceptionHandler exceptionHandler) {
            this.collector = collector;
            this.cache = cache;
            this.exceptionHandler = exceptionHandler;
        }

        public final void processValue(MessagePayload payload) {
            if (typeAdapter == null)
                return;
            
            // Lock the collector and prepare the new value.
            synchronized (collector) {
                try {
                    if (typeAdapter.updateCache(cache, getConnectionPayload(), payload)) {
                        collector.collect();
                    }
                } catch (RuntimeException e) {
                    exceptionHandler.handleException(e);
                }
            }
        }
        
        public final void findTypeAdapter() {
            if (getConnectionPayload() == null) {
                typeAdapter = null;
            } else {
                try {
                    typeAdapter = MultiplexedChannelHandler.this.findTypeAdapter(cache, getConnectionPayload());
                } catch(RuntimeException ex) {
                    exceptionHandler.handleException(ex);
                }
            }
        }
        
    }
    
    protected synchronized final void notifyAllReaders(Exception ex) {
        for (MonitorHandler monitor : monitors.values()) {
            monitor.exceptionHandler.handleException(ex);
        }
    }

    protected synchronized final ConnectionPayload getConnectionPayload() {
        return connectionPayload;
    }
    
    protected synchronized final MessagePayload getLastMessagePayload() {
        return lastMessage;
    }

    protected synchronized final void processConnection(ConnectionPayload connectionPayload) {
        this.connectionPayload = connectionPayload;
        
        for (MonitorHandler monitor : monitors.values()) {
            monitor.findTypeAdapter();
        }
    }
    
    private static DataSourceTypeAdapter<?, ?> defaultTypeAdapter = new DataSourceTypeAdapter<Object, Object>() {

            @Override
            public int match(ValueCache<?> cache, Object connection) {
                return 1;
            }

            @Override
            public Object getSubscriptionParameter(ValueCache<?> cache, Object connection) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public boolean updateCache(ValueCache cache, Object connection, Object message) {
{'fraction_non_alphanumeric': 0.0751907457543687, 'fraction_numerical': 0.002215111986217081, 'mean_word_length': 2.4104070499370542, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '141294', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1775, 'n_words': 520}