package no.ssb.vtl.connectors.utils;

import no.ssb.vtl.connectors.Connector;
import no.ssb.vtl.connectors.ConnectorException;
import no.ssb.vtl.model.Dataset;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkNotNull;

public class RegexConnector extends ForwardingConnector {

    private final Connector delegate;
    private final Pattern pattern;
    private final String replaceAll;

    private RegexConnector(Connector connector, Pattern pattern, String replaceAll) {
        this.delegate = checkNotNull(connector);
        this.pattern = checkNotNull(pattern);
        this.replaceAll = checkNotNull(replaceAll);
    }

    public static final RegexConnector create(Connector connector, Pattern pattern, String replaceAll) {
        return new RegexConnector(connector, pattern, replaceAll);
    }

    String transformId(String identifier) {
        Matcher matcher = pattern.matcher(identifier);
        if (!matcher.matches())
            return identifier;

        return matcher.replaceAll(replaceAll);
    }

    @Override
    public boolean canHandle(String identifier) {
        return delegate().canHandle(transformId(identifier));
    }

    @Override
    public Dataset getDataset(String identifier) throws ConnectorException {
        return delegate().getDataset(transformId(identifier));
    }

    @Override
    public Dataset putDataset(String identifier, Dataset dataset) throws ConnectorException {
        return delegate().putDataset(transformId(identifier), dataset);
    }

    @Override
    protected Connector delegate() {
        return delegate;
    }

}

@@ -31,16 +31,831 @@
 utils;%0A%0A
+/*-%0A * ========================LICENSE_START=================================%0A * Java VTL Utility connectors%0A * %25%25%0A * Copyright (C) 2017 Statistics Norway and contributors%0A * %25%25%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A *      http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A * =========================LICENSE_END==================================%0A */%0A%0A
 import n

Update license header