All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.couchbase.connect.kafka.util.TopicMap Maven / Gradle / Ivy

/*
 * Copyright 2020 Couchbase, Inc.
 *
 * 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.
 */

package com.couchbase.connect.kafka.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static java.util.stream.Collectors.toMap;

public class TopicMap {
  private TopicMap() {
    throw new AssertionError("not instantiable");
  }

  public static Map parseTopicToCollection(List topicToCollection) {
    return mapValues(parseCommon(topicToCollection), ScopeAndCollection::parse);
  }

  public static Map parseCollectionToTopic(List collectionToTopic) {
    return mapKeys(parseCommon(collectionToTopic), ScopeAndCollection::parse);
  }

  private static Map parseCommon(List map) {
    Map result = new HashMap<>();
    for (String entry : map) {
      String[] components = entry.split("=", -1);
      if (components.length != 2) {
        throw new IllegalArgumentException("Bad entry: '" + entry + "'. Expected exactly one equals (=) character separating topic and collection.");
      }
      result.put(components[0], components[1]);
    }
    return result;
  }

  private static  Map mapValues(Map map, Function valueTransformer) {
    return map.entrySet().stream()
        .collect(toMap(
            Map.Entry::getKey,
            entry -> valueTransformer.apply(entry.getValue())
        ));
  }

  private static  Map mapKeys(Map map, Function keyTransformer) {
    return map.entrySet().stream()
        .collect(toMap(
            entry -> keyTransformer.apply(entry.getKey()),
            Map.Entry::getValue
        ));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy