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

com.synopsys.integration.datastructure.SetMap Maven / Gradle / Ivy

Go to download

The base library for all other integration libraries to encompass and export common dependencies and code.

There is a newer version: 26.1.2
Show newest version
/*
 * integration-common
 *
 * Copyright (c) 2021 Synopsys, Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.synopsys.integration.datastructure;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class SetMap extends AbstractMap> {
    private final Map> map;

    public SetMap(final Map> map) {
        this.map = map;
    }

    private SetMap() {
        this(new HashMap<>());
    }

    public static final  SetMap createDefault() {
        return new SetMap<>();
    }

    public static final  SetMap createLinked() {
        LinkedHashMap> initializer = new LinkedHashMap<>();
        return new SetMap<>(initializer);
    }

    public Set getValue(K key) {
        return map.get(key);
    }

    public Set add(K key, S value) {
        Set set = this.computeIfAbsent(key, ignoredKey -> new LinkedHashSet<>());
        set.add(value);
        return set;
    }

    public Set addAll(K key, Set value) {
        Set set = this.computeIfAbsent(key, ignoredKey -> new LinkedHashSet<>());
        set.addAll(value);
        return set;
    }

    @Override
    public Set put(final K key, final Set value) {
        return map.put(key, value);
    }

    @Override
    public void putAll(final Map> mapToAdd) {
        map.putAll(mapToAdd);
    }

    @Override
    public Set>> entrySet() {
        return map.entrySet();
    }

    public Map> getMap() {
        return map;
    }

    public void combine(SetMap other) {
        other.forEach(this::addAll);
    }
}