de.fhg.iais.asc.sipmaker.SipMakerKey.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.sipmaker.SipMakerKey.java

Source

package de.fhg.iais.asc.sipmaker;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * 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.                                             *
 ******************************************************************************/

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import de.fhg.iais.commons.annotation.OnlyCalledFromTests;

public final class SipMakerKey {
    private static final Pattern COMMA = Pattern.compile(",");

    private final List<String> directories;
    private final String version;

    private SipMakerKey(List<String> directories, String version) {
        this.directories = directories;
        this.version = version;
    }

    public static SipMakerKey fromTrunk(String metadataFormat, String... subFormats) {
        return fromTrunk(metadataFormat, Arrays.asList(subFormats));
    }

    public static SipMakerKey fromTrunk(String metadataFormat, List<String> subFormats) {
        if (StringUtils.isEmpty(metadataFormat)) {
            throw new IllegalArgumentException("Metadata format may not be empty");
        }

        String current = metadataFormat;
        List<String> result = new ArrayList<String>();
        result.add(current);

        for (String dir : subFormats) {
            if (!StringUtils.isEmpty(dir)) {
                current = current.concat(File.separator).concat(dir);
                result.add(current);
            }
        }

        Collections.reverse(result);
        result = Collections.unmodifiableList(result);
        return new SipMakerKey(result, "");
    }

    @OnlyCalledFromTests
    public SipMakerKey withVersion(String version) {
        return new SipMakerKey(this.directories, version);
    }

    public static SipMakerKey fromCommaSeparated(String commaSeparated) {
        return from(COMMA.split(commaSeparated));
    }

    private static SipMakerKey from(String[] splitted) {
        List<String> list = Arrays.asList(splitted);
        return fromTrunk(splitted[0], list.subList(1, list.size()));
    }

    public List<String> getDirectories() {
        return this.directories;
    }

    public String getVersion() {
        return this.version;
    }
}