001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     dmetzler
018 */
019package org.nuxeo.datadog.reporter;
020
021import java.util.ArrayList;
022import java.util.EnumSet;
023import java.util.List;
024import java.util.stream.Collectors;
025
026import org.coursera.metrics.datadog.DatadogReporter.Expansion;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XNodeList;
029import org.nuxeo.common.xmap.annotation.XObject;
030
031import com.google.common.collect.ImmutableSet;
032
033/**
034 * @since 10.1
035 */
036@XObject("filter")
037public class FilterDescriptor {
038
039    @XNode("@method")
040    protected String method;
041
042    @XNodeList(value = "includes/include", type = ArrayList.class, componentType = String.class)
043    protected List<String> includes = new ArrayList<>();
044
045    @XNodeList(value = "excludes/exclude", type = ArrayList.class, componentType = String.class)
046    protected List<String> excludes = new ArrayList<>();
047
048    @XNodeList(value = "expansions/expansion", type = ArrayList.class, componentType = String.class)
049    protected List<String> expansions = new ArrayList<>();
050
051    public boolean getUseSubstringMatching() {
052        return "substring".equals(method);
053    }
054
055    public boolean getUseRegexFilters() {
056        return "regex".equals(method);
057    }
058
059    public ImmutableSet<String> getIncludes() {
060        return ImmutableSet.<String> builder().addAll(includes).build();
061    }
062
063    public ImmutableSet<String> getExcludes() {
064        return ImmutableSet.<String> builder().addAll(excludes).build();
065    }
066
067    public EnumSet<Expansion> getExpansions() {
068        if (expansions.isEmpty()) {
069            return Expansion.ALL;
070        } else {
071            return Expansion.ALL.stream().filter(e -> expansions.contains(e.toString())).collect(
072                    Collectors.toCollection(() -> EnumSet.noneOf(Expansion.class)));
073        }
074    }
075}