001/*
002 * (C) Copyright 2017 Nuxeo SA (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 *     Taken from https://github.com/concord/concord-jvm
018 */
019package org.nuxeo.ecm.platform.importer.mqueues.computation;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.stream.Collectors;
024
025public class ComputationMetadataMapping extends ComputationMetadata {
026
027    private final Map<String, String> mapping;
028    private final Map<String, String> reverseMapping;
029
030    public ComputationMetadataMapping(ComputationMetadata metadata, Map<String, String> mapping) {
031        super(mapping.getOrDefault(metadata.name, metadata.name),
032                metadata.istreams.stream().map(s -> mapping.getOrDefault(s, s)).collect(Collectors.toSet()),
033                metadata.ostreams.stream().map(s -> mapping.getOrDefault(s, s)).collect(Collectors.toSet()));
034        this.mapping = mapping;
035        reverseMapping = new HashMap<>(mapping.size());
036        mapping.forEach((key, value) -> reverseMapping.put(value, key));
037    }
038
039    public String map(String name) {
040        return mapping.getOrDefault(name, name);
041    }
042
043    public String reverseMap(String name) {
044        return reverseMapping.getOrDefault(name, name);
045    }
046
047}