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

org.weakref.jmx.guice.MapExportBinder Maven / Gradle / Ivy

There is a newer version: 1.26
Show newest version
package org.weakref.jmx.guice;

import com.google.common.base.Throwables;
import com.google.inject.multibindings.Multibinder;
import org.weakref.jmx.ObjectNames;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.util.Map;

public class MapExportBinder
{
    protected final Multibinder> binder;
    protected final Class keyClass;
    private final Class valueClass;

    MapExportBinder(Multibinder> binder, Class keyClass, Class valueClass)
    {
        this.binder = binder;
        this.keyClass = keyClass;
        this.valueClass = valueClass;
    }

    public void withGeneratedName(final NamingFunction valueNamingFunction)
    {
        ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
        {
            public ObjectName name(Map.Entry entry)
            {
                try {
                    String itemName = valueNamingFunction.name(entry.getValue());
                    return new ObjectName(ObjectNames.generatedNameOf(valueClass, itemName));
                }
                catch (MalformedObjectNameException e) {
                    throw Throwables.propagate(e);
                }
            }
        };

        binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
    }

    public void withGeneratedName(final ObjectNameFunction valueNamingFunction)
    {
        ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
        {
            public ObjectName name(Map.Entry entry)
            {
                return valueNamingFunction.name(entry.getValue());
            }
        };

        binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
    }

    public void withGeneratedName(final MapNamingFunction valueNamingFunction)
    {
        ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
        {
            public ObjectName name(Map.Entry entry)
            {
                try {
                    String itemName = valueNamingFunction.name(entry.getKey(), entry.getValue());
                    return new ObjectName(ObjectNames.generatedNameOf(valueClass, itemName));
                }
                catch (MalformedObjectNameException e) {
                    throw Throwables.propagate(e);
                }
            }
        };

        binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
    }

    public void withGeneratedName(final MapObjectNameFunction valueNamingFunction)
    {
        ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
        {
            public ObjectName name(Map.Entry entry)
            {
                return valueNamingFunction.name(entry.getKey(), entry.getValue());
            }
        };

        binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy