<commit_before>package brooklyn.config.render;

import groovy.lang.Closure;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import brooklyn.entity.Entity;
import brooklyn.event.AttributeSensor;
import brooklyn.event.Sensor;

import com.google.common.base.Objects;

/** registry of hints for displaying items such as sensors, e.g. in the web console */
public class RendererHints {

    static Map<Object, Set<Hint>> registry = new LinkedHashMap<Object, Set<Hint>>();

    /** registers a hint against the given element (eg a sensor);
     * returns the element, for convenience when used in a with block after defining the element 
     */
    public synchronized static <T> T register(T element, Hint<T> hintForThatElement) {
        Set<Hint> set = registry.get(element);
        if (set == null) {
            set = new LinkedHashSet<Hint>();
            registry.put(element, set);
        }
        set.add(hintForThatElement);
        return element;
    }

    /** abstract superclass (marker) for 'hints' */
    public static abstract class Hint<T> {}

    public static interface NamedAction {
        String getActionName();
    }
    
    /** this hint describes a named action possible on something, e.g. a sensor;
     * currently used in web client to show actions on sensors
     */
    public static class NamedActionWithUrl extends Hint<Sensor> implements NamedAction {
        private final String actionName;
        private final Closure postProcessing;

        public NamedActionWithUrl(String actionName) {
            this(actionName, null);
        }

        public NamedActionWithUrl(String actionName, Closure postProcessing) {
            this.actionName = actionName;
            this.postProcessing = postProcessing;
        }

        public String getUrl(Entity e, AttributeSensor s) {
            return getUrlFromValue(e.getAttribute(s));
        }

        public String getActionName() {
            return actionName;
        }

        /** this is the method invoked by web console SensorSummary, at the moment */
        public String getUrlFromValue(Object v) {
            if (postProcessing != null) {
                v = postProcessing.call(v);<commit_msg>RendererHints: accept function as well as closure<commit_after>package brooklyn.config.render;

import groovy.lang.Closure;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import brooklyn.entity.Entity;
import brooklyn.event.AttributeSensor;
import brooklyn.event.Sensor;
import brooklyn.util.GroovyJavaMethods;

import com.google.common.base.Function;
import com.google.common.base.Objects;

/** registry of hints for displaying items such as sensors, e.g. in the web console */
public class RendererHints {

    static Map<Object, Set<Hint>> registry = new LinkedHashMap<Object, Set<Hint>>();

    /** registers a hint against the given element (eg a sensor);
     * returns the element, for convenience when used in a with block after defining the element 
     */
    public synchronized static <T> T register(T element, Hint<T> hintForThatElement) {
        Set<Hint> set = registry.get(element);
        if (set == null) {
            set = new LinkedHashSet<Hint>();
            registry.put(element, set);
        }
        set.add(hintForThatElement);
        return element;
    }

    /** abstract superclass (marker) for 'hints' */
    public static abstract class Hint<T> {}

    public static interface NamedAction {
        String getActionName();
    }
    
    /** this hint describes a named action possible on something, e.g. a sensor;
     * currently used in web client to show actions on sensors
     */
    public static class NamedActionWithUrl extends Hint<Sensor> implements NamedAction {
        private final String actionName;
        private final Function<Object,String> postProcessing;

        public NamedActionWithUrl(String actionName) {
            this(actionName, (Function<Object,String>)null);
        }

        public NamedActionWithUrl(String actionName, Closure<String> postProcessing) {
            this.actionName = actionName;
            this.postProcessing = (postProcessing == null) ? null : GroovyJavaMethods.functionFromClosure(postProcessing);
        }

        public NamedActionWithUrl(String actionName, Function<Object,String> postProcessing) {
            this.actionName = actionName;
            this.postProcessing = postProcessing;
        }

        public String getUrl(Entity e, AttributeSensor s) {
            return getUrlFromValue(e.getAttribute(s));
        }

        public String getActionName() {
            return actionName;
        }

        /** this is the method invoked by web console SensorSummary, at the moment */
        public String getUrlFromValue(Object v) {
            if (postProcessing != null) {
                v = postProcessing.apply(v);
{'fraction_non_alphanumeric': 0.08627292884499092, 'fraction_numerical': 0.0, 'mean_word_length': 2.8110599078341014, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7485681', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1295, 'n_words': 409}