001/*
002 * (C) Copyright 2020 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 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.log;
020
021import java.util.List;
022
023/**
024 * @since 11.1
025 */
026public abstract class AbstractLogConfig implements LogConfig {
027
028    protected final List<String> patterns;
029
030    protected final boolean defaultConfig;
031
032    public AbstractLogConfig(boolean defaultConfig, List<String> patterns) {
033        this.defaultConfig = defaultConfig;
034        if (patterns == null) {
035            throw new IllegalArgumentException("patterns required");
036        }
037        this.patterns = patterns;
038    }
039
040    @Override
041    public boolean isDefault() {
042        return defaultConfig;
043    }
044
045    @Override
046    public boolean match(Name name) {
047        return patterns.stream().anyMatch(pattern -> name.getUrn().startsWith(pattern));
048    }
049
050    @Override
051    public boolean match(Name name, Name group) {
052        if (patterns.stream().anyMatch(pattern -> group.getUrn().startsWith(pattern))) {
053            return true;
054        }
055        return match(name);
056    }
057
058
059}