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

org.weakref.jmx.ObjectNameBuilder Maven / Gradle / Ivy

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

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.weakref.jmx.ObjectNames.quoteValueIfNecessary;

public class ObjectNameBuilder
{
    private static final Pattern BAD_PACKAGENAME_PATTERN = Pattern.compile("[:?*]");
    private StringBuilder objectName;
    private final Set properties = new HashSet();

    public ObjectNameBuilder(String packageName)
    {
        checkNotNull(packageName, "packageName is null");
        checkArgument(!BAD_PACKAGENAME_PATTERN.matcher(packageName).find(), "packageName is invalid");
        this.objectName = new StringBuilder(packageName);
    }

    public ObjectNameBuilder withProperty(String name, String value)
    {
        if (properties.isEmpty()) {
            objectName.append(":");
        }
        else {
            objectName.append(",");
        }

        checkArgument(properties.add(name), "Duplicate property name " + name);

        objectName.append(name).append('=').append(quoteValueIfNecessary(value));
        return this;
    }

    public String build()
    {
        return objectName.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy