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

VCollections.src.org.violetlib.collections.impl.MapSetBuilderImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2023 Alan Snyder.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the license agreement. For details see
 * accompanying license terms.
 */

package org.violetlib.collections.impl;

import java.util.HashMap;
import java.util.Map;

import org.violetlib.collections.Binding;
import org.violetlib.collections.ICollection;
import org.violetlib.collections.IList;
import org.violetlib.collections.IMap;
import org.violetlib.collections.ISet;
import org.violetlib.collections.MapBuilder;
import org.violetlib.collections.MapSetBuilder;
import org.violetlib.collections.SetBuilder;

import org.jetbrains.annotations.*;

/**
  Build an immutable map from keys of type K to immutable sets of values of type V. Null values are not supported.
*/

public final class MapSetBuilderImpl
  implements MapSetBuilder
{
    /**
      Create an empty map builder.
    */

    public static  @NotNull MapSetBuilder create(boolean createOrderedSets)
    {
        return new MapSetBuilderImpl<>(createOrderedSets);
    }

    private final @NotNull Map> map;
    private final boolean createOrderedSets;

    private MapSetBuilderImpl(boolean createOrderedSets)
    {
        map = new HashMap<>();
        this.createOrderedSets = createOrderedSets;
    }

    @Override
    public int size()
    {
        return map.size();
    }

    @Override
    public void add(@NotNull K key, @NotNull V value)
    {
        SetBuilder b = map.get(key);
        if (b == null) {
            b = createOrderedSets ? ISet.builder(ISet.ORDERED) : ISet.builder();
            map.put(key, b);
        }
        b.add(value);
    }

    @Override
    public void addAll(@NotNull K key, @NotNull ICollection values)
    {
        if (!values.isEmpty()) {
            SetBuilder b = map.get(key);
            if (b == null) {
                b = ISet.builder();
                map.put(key, b);
            }
            b.addAll(values);
        }
    }

    @Override
    public void addAll(@NotNull IMap> values)
    {
        for (Binding> b : values) {
            addAll(b.getKey(), b.getValue());
        }
    }

    @Override
    public int getValueCount(@NotNull K key)
    {
        SetBuilder b = map.get(key);
        return b != null ? b.size() : 0;
    }

    @Override
    public @NotNull ISet getValues(@NotNull K key)
    {
        SetBuilder b = map.get(key);
        return b != null ? b.values() : ISet.empty();
    }

    @Override
    public @NotNull IList keys()
    {
        return IList.create(map.keySet());
    }

    @Override
    public @NotNull IMap> value()
    {
        MapBuilder> b = IMap.builder();
        for (Map.Entry> e : map.entrySet()) {
            b.put(e.getKey(), e.getValue().values());
        }
        return b.value();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy