/*
 * #%L
 * servo
 * %%
 * Copyright (C) 2011 Netflix
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.netflix.servo.publish;

import com.netflix.servo.MetricConfig;
import com.netflix.servo.Tag;
import com.netflix.servo.TagList;

import java.util.Map;
import java.util.NavigableMap;

/**
 * Filter that checks for a prefix match on a given tag. This can be useful
 * providing rules associated with the canonical class name.
 */
public final class PrefixMetricFilter implements MetricFilter {

    private final String tagKey;
    private final MetricFilter root;
    private final NavigableMap<String,MetricFilter> filters;

    /**
     * Creates a new prefix filter.
     *
     * @param tagKey   the tag to perform matching on, if null the name will be
     *                 checked
     * @param root     filter used if there are no prefix matches
     * @param filters  map of prefix to sub-filter. The filter associated with
     *                 the longest matching prefix will be used.
     */
    public PrefixMetricFilter(
            String tagKey,
            MetricFilter root,
            NavigableMap<String,MetricFilter> filters) {
        this.tagKey = tagKey;
        this.root = root;
        this.filters = filters;
    }

    /** {@inheritDoc} */
    public boolean matches(MetricConfig config) {
        String name = config.getName();
        TagList tags = config.getTags();
        String value = null;
        if (tagKey == null) {
            value = name;
        } else {
            Tag t = tags.getTag(tagKey);
            value = (t == null) ? null : t.getValue();
        }

        if (value == null) {
            return root.matches(config);
        } else {
            NavigableMap<String,MetricFilter> candidates =
                filters.headMap(value, true).descendingMap();
            if (candidates.isEmpty()) {
                return root.matches(config);
            } else {
                for (Map.Entry<String,MetricFilter> e : candidates.entrySet()) {
                    if (value.startsWith(e.getKey())) {
                        return e.getValue().matches(config);
                    }
                }
                return root.matches(config);
            }
        }
    }
}

@@ -659,16 +659,56 @@
 blish;%0A%0A
+import com.google.common.base.Strings;%0A%0A
 import c
@@ -2201,21 +2201,36 @@
 if (
-value == null
+Strings.isNullOrEmpty(value)
 ) %7B%0A
@@ -2279,32 +2279,82 @@
        %7D else %7B%0A
+            String start = value.substring(0, 1);%0A
             Navi
@@ -2424,16 +2424,28 @@
 ers.
-headMap(
+subMap(start, true, 
 valu

update to use subMap