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

com.eg.agent.android.measurement.MeasurementPool Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package com.eg.agent.android.measurement;


import com.eg.agent.android.logging.AgentLog;
import com.eg.agent.android.logging.AgentLogManager;
import com.eg.agent.android.measurement.consumer.MeasurementConsumer;
import com.eg.agent.android.measurement.producer.BaseMeasurementProducer;
import com.eg.agent.android.measurement.producer.MeasurementProducer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import static com.eg.agent.android.instrumentation.NetworkInformation.ExceptionHelper.exceptionToErrorCode;
import static com.eg.agent.android.measurement.MeasurementType.Any;

public class MeasurementPool extends BaseMeasurementProducer implements MeasurementConsumer {
    private static final AgentLog log = AgentLogManager.getAgentLog();
    private final Collection consumers = new CopyOnWriteArrayList();
    private final Collection producers = new CopyOnWriteArrayList();

    public MeasurementPool() {
        super(Any);
        addMeasurementProducer(this);
    }

    public void addMeasurementProducer(MeasurementProducer producer) {
        if (producer != null) {
            synchronized (this.producers) {
                if (this.producers.contains(producer)) {
                    log.debug("Attempted to add the same MeasurementProducer " + producer + "  multiple times.");
                    return;
                }
                this.producers.add(producer);
                return;
            }
        }
        log.debug("Attempted to add null MeasurementProducer.");
    }

    public void removeMeasurementProducer(MeasurementProducer producer) {
        synchronized (this.producers) {
            if (this.producers.contains(producer)) {
                this.producers.remove(producer);
                return;
            }
            log.debug("Attempted to remove MeasurementProducer " + producer + " which is not registered.");
        }
    }

    public void addMeasurementConsumer(MeasurementConsumer consumer) {
        if (consumer != null) {
            synchronized (this.consumers) {
                if (this.consumers.contains(consumer)) {
                    log.debug("Attempted to add the same MeasurementConsumer " + consumer + " multiple times.");
                    return;
                }
                this.consumers.add(consumer);
                return;
            }
        }
        log.debug("Attempted to add null MeasurementConsumer.");
    }

    public void removeMeasurementConsumer(MeasurementConsumer consumer) {
        synchronized (this.consumers) {
            if (this.consumers.contains(consumer)) {
                this.consumers.remove(consumer);
                return;
            }
            log.debug("Attempted to remove MeasurementConsumer " + consumer + " which is not registered.");
        }
    }

    public void broadcastMeasurements() {
        List allProducedMeasurements = new ArrayList();

        synchronized (this.producers) {
            for (MeasurementProducer producer : this.producers) {
                Collection measurements = producer.drainMeasurements();
                if (measurements.size() > 0) {
                    allProducedMeasurements.addAll(measurements);
                    while (allProducedMeasurements.remove(null)) {
                    }
                }
            }
        }
        if (allProducedMeasurements.size() > 0) {
            Iterator i$;
            MeasurementConsumer consumer;
            synchronized (this.consumers) {
                for (i$ = this.consumers.iterator(); i$.hasNext(); ) {
                    consumer = (MeasurementConsumer) i$.next();
                    List measurements = new ArrayList(allProducedMeasurements);
                    for (Measurement measurement : measurements) {
                        if ((consumer.getMeasurementType() == measurement.getType()) || (consumer.getMeasurementType() == Any)) {
                            try {
                                consumer.consumeMeasurement(measurement);
                            } catch (Exception e) {
                                exceptionToErrorCode(e);
                                log.error("broadcastMeasurements exception[" + e.getClass().getName() + "]");
                            }
                        }
                    }
                }
            }
        }
   /*     if (allProducedMeasurements.size() > 0) {
            synchronized (this.consumers) {
                for (MeasurementConsumer consumer : this.consumers) {
                    for (Measurement measurement : new ArrayList(allProducedMeasurements)) {
                        if (consumer.getMeasurementType() == measurement.getType() || consumer.getMeasurementType() == MeasurementType.Any) {
                            try {
                                consumer.consumeMeasurement(measurement);
                            } catch (Exception e) {
                                ExceptionHelper.exceptionToErrorCode(e);
                                log.error("broadcastMeasurements exception[" + e.getClass().getName() + "]");
                            }
                        }
                    }
                }
            }
        }*/
    }

    public void consumeMeasurement(Measurement measurement) {
        produceMeasurement(measurement);
    }

    public void consumeMeasurements(Collection measurements) {
        produceMeasurements(measurements);
    }

    public MeasurementType getMeasurementType() {
        return Any;
    }

    public Collection getMeasurementProducers() {
        return this.producers;
    }

    public Collection getMeasurementConsumers() {
        return this.consumers;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy