<commit_before>package brooklyn.management.internal;

import java.util.Collection;
import java.util.Collections;

import brooklyn.entity.Entity;
import brooklyn.event.Sensor;
import brooklyn.event.SensorEventListener;
import brooklyn.management.SubscriptionContext;
import brooklyn.management.SubscriptionHandle;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;

/**
 * Tracks subscriptions associated that are registered with particular entities. Gives utilities for unsubscribing from all
 * subscriptions on a given entity, etc.
 */
public class SubscriptionTracker {

    protected transient SubscriptionContext subscription;
    
    private SetMultimap<Entity, SubscriptionHandle> subscriptions = HashMultimap.create();

    public SubscriptionTracker(SubscriptionContext subscriptionContext) {
        this.subscription = subscriptionContext;
    }
    
    /** @see SubscriptionContext#subscribe(Entity, Sensor, SensorEventListener) */
    public <T> SubscriptionHandle subscribe(Entity producer, Sensor<T> sensor, SensorEventListener<? super T> listener) {
        SubscriptionHandle handle = subscription.subscribe(producer, sensor, listener);
        subscriptions.put(producer, handle);
        return handle;
    }
    
    /** @see SubscriptionContext#subscribeToChildren(Entity, Sensor, SensorEventListener) */
    public <T> SubscriptionHandle subscribeToChildren(Entity parent, Sensor<T> sensor, SensorEventListener<? super T> listener) {
        SubscriptionHandle handle = subscription.subscribeToChildren(parent, sensor, listener);
        subscriptions.put(parent, handle);
        return handle;
    }

    /** @see SubscriptionContext#subscribeToMembers(Entity, Sensor, SensorEventListener) */
    public <T> SubscriptionHandle subscribeToMembers(Entity parent, Sensor<T> sensor, SensorEventListener<? super T> listener) {
        SubscriptionHandle handle = subscription.subscribeToMembers(parent, sensor, listener);
        subscriptions.put(parent, handle);
        return handle;
    }    

    /**
     * Unsubscribes the given producer.
     *
     * @see SubscriptionContext#unsubscribe(SubscriptionHandle)
     */
    public boolean unsubscribe(Entity producer) {
        Collection<SubscriptionHandle> handles = subscriptions.removeAll(producer);
        if (handles != null) {
            for (SubscriptionHandle handle : handles) {
                subscription.unsubscribe(handle);
            }
            return true;
        }
        return false;
    }

    /**
    * Unsubscribes the given producer.
    *
    * @see SubscriptionContext#unsubscribe(SubscriptionHandle)
    */
   public boolean unsubscribe(Entity producer, SubscriptionHandle handle) {
       subscriptions.remove(producer, handle);<commit_msg>fix compile error leftover from Aled's pull request<commit_after>package brooklyn.management.internal;

import java.util.Collection;
import java.util.Collections;

import brooklyn.entity.Entity;
import brooklyn.entity.Group;
import brooklyn.event.Sensor;
import brooklyn.event.SensorEventListener;
import brooklyn.management.SubscriptionContext;
import brooklyn.management.SubscriptionHandle;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;

/**
 * Tracks subscriptions associated that are registered with particular entities. Gives utilities for unsubscribing from all
 * subscriptions on a given entity, etc.
 */
public class SubscriptionTracker {

    protected transient SubscriptionContext subscription;
    
    private SetMultimap<Entity, SubscriptionHandle> subscriptions = HashMultimap.create();

    public SubscriptionTracker(SubscriptionContext subscriptionContext) {
        this.subscription = subscriptionContext;
    }
    
    /** @see SubscriptionContext#subscribe(Entity, Sensor, SensorEventListener) */
    public <T> SubscriptionHandle subscribe(Entity producer, Sensor<T> sensor, SensorEventListener<? super T> listener) {
        SubscriptionHandle handle = subscription.subscribe(producer, sensor, listener);
        subscriptions.put(producer, handle);
        return handle;
    }
    
    /** @see SubscriptionContext#subscribeToChildren(Entity, Sensor, SensorEventListener) */
    public <T> SubscriptionHandle subscribeToChildren(Entity parent, Sensor<T> sensor, SensorEventListener<? super T> listener) {
        SubscriptionHandle handle = subscription.subscribeToChildren(parent, sensor, listener);
        subscriptions.put(parent, handle);
        return handle;
    }

    /** @see SubscriptionContext#subscribeToMembers(Entity, Sensor, SensorEventListener) */
    public <T> SubscriptionHandle subscribeToMembers(Group parent, Sensor<T> sensor, SensorEventListener<? super T> listener) {
        SubscriptionHandle handle = subscription.subscribeToMembers(parent, sensor, listener);
        subscriptions.put(parent, handle);
        return handle;
    }    

    /**
     * Unsubscribes the given producer.
     *
     * @see SubscriptionContext#unsubscribe(SubscriptionHandle)
     */
    public boolean unsubscribe(Entity producer) {
        Collection<SubscriptionHandle> handles = subscriptions.removeAll(producer);
        if (handles != null) {
            for (SubscriptionHandle handle : handles) {
                subscription.unsubscribe(handle);
            }
            return true;
        }
        return false;
    }

    /**
    * Unsubscribes the given producer.
    *
    * @see SubscriptionContext#unsubscribe(SubscriptionHandle)
    */
   public boolean unsubscribe(Entity producer, SubscriptionHandle handle) {
       subscriptions.remove(producer, handle);
{'fraction_non_alphanumeric': 0.08482063968899099, 'fraction_numerical': 0.0, 'mean_word_length': 3.8961937716262978, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '5529119', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1412, 'n_words': 346}