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

com.landawn.abacus.util.SetMultimap Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.3.16
Show newest version
/*
 * Copyright (C) 2017 HaiYang Li
 *
 * 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.landawn.abacus.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.IntFunction;
import java.util.function.Supplier;

import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.Internal;

/**
 *
 * @author Haiyang Li
 * @param  the key type
 * @param 
 * @see N#newSetMultimap()
 * @see N#newSetMultimap(Class, Class)
 * @see N#newSetMultimap(Supplier, Supplier)
 * @since 0.9
 */
public final class SetMultimap extends Multimap> {

    SetMultimap() {
        this(HashMap.class, HashSet.class);
    }

    SetMultimap(int initialCapacity) {
        this(N.> newHashMap(initialCapacity), HashSet.class);
    }

    @SuppressWarnings("rawtypes")
    SetMultimap(final Class mapType, final Class valueType) {
        super(mapType, valueType);
    }

    SetMultimap(final Supplier>> mapSupplier, final Supplier> valueSupplier) {
        super(mapSupplier, valueSupplier);
    }

    @Internal
    @SuppressWarnings("rawtypes")
    SetMultimap(final Map> valueMap, final Class valueType) {
        super(valueMap, valueType2Supplier(valueType));
    }

    @Internal
    SetMultimap(final Map> valueMap, final Supplier> valueSupplier) {
        super(valueMap, valueSupplier);
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param k1
     * @param v1
     * @return
     */
    public static  SetMultimap of(final K k1, final E v1) {
        final SetMultimap map = new SetMultimap<>(1);

        map.put(k1, v1);

        return map;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param k1
     * @param v1
     * @param k2
     * @param v2
     * @return
     */
    public static  SetMultimap of(final K k1, final E v1, final K k2, final E v2) {
        final SetMultimap map = new SetMultimap<>(2);

        map.put(k1, v1);
        map.put(k2, v2);

        return map;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param k1
     * @param v1
     * @param k2
     * @param v2
     * @param k3
     * @param v3
     * @return
     */
    public static  SetMultimap of(final K k1, final E v1, final K k2, final E v2, final K k3, final E v3) {
        final SetMultimap map = new SetMultimap<>(3);

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);

        return map;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param k1
     * @param v1
     * @param k2
     * @param v2
     * @param k3
     * @param v3
     * @param k4
     * @param v4
     * @return
     */
    public static  SetMultimap of(final K k1, final E v1, final K k2, final E v2, final K k3, final E v3, final K k4, final E v4) {
        final SetMultimap map = new SetMultimap<>(4);

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);

        return map;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param k1
     * @param v1
     * @param k2
     * @param v2
     * @param k3
     * @param v3
     * @param k4
     * @param v4
     * @param k5
     * @param v5
     * @return
     */
    public static  SetMultimap of(final K k1, final E v1, final K k2, final E v2, final K k3, final E v3, final K k4, final E v4, final K k5,
            final E v5) {
        final SetMultimap map = new SetMultimap<>(5);

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        map.put(k5, v5);

        return map;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param k1
     * @param v1
     * @param k2
     * @param v2
     * @param k3
     * @param v3
     * @param k4
     * @param v4
     * @param k5
     * @param v5
     * @param k6
     * @param v6
     * @return
     */
    public static  SetMultimap of(final K k1, final E v1, final K k2, final E v2, final K k3, final E v3, final K k4, final E v4, final K k5,
            final E v5, final K k6, final E v6) {
        final SetMultimap map = new SetMultimap<>(6);

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        map.put(k5, v5);
        map.put(k6, v6);

        return map;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param k1
     * @param v1
     * @param k2
     * @param v2
     * @param k3
     * @param v3
     * @param k4
     * @param v4
     * @param k5
     * @param v5
     * @param k6
     * @param v6
     * @param k7
     * @param v7
     * @return
     */
    public static  SetMultimap of(final K k1, final E v1, final K k2, final E v2, final K k3, final E v3, final K k4, final E v4, final K k5,
            final E v5, final K k6, final E v6, final K k7, final E v7) {
        final SetMultimap map = new SetMultimap<>();

        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        map.put(k4, v4);
        map.put(k5, v5);
        map.put(k6, v6);
        map.put(k7, v7);

        return map;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param map
     * @return
     */
    public static  SetMultimap from(final Map map) {
        final SetMultimap multimap = new SetMultimap<>(Maps.newTargetMap(map), HashSet.class);

        if (N.notNullOrEmpty(map)) {
            multimap.putAll(map);
        }

        return multimap;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param map
     * @return
     */
    @Beta
    public static  SetMultimap fromm(final Map> map) {
        final SetMultimap multimap = new SetMultimap<>(Maps.newTargetMap(map), HashSet.class);

        if (N.notNullOrEmpty(map)) {
            for (Map.Entry> entry : map.entrySet()) {
                multimap.putAll(entry.getKey(), entry.getValue());
            }
        }

        return multimap;
    }

    /**
     *
     * @param 
     * @param  the key type
     * @param 
     * @param c
     * @param keyMapper
     * @return
     * @throws X the x
     */
    public static  SetMultimap from(final Collection c,
            final Throwables.Function keyMapper) throws X {
        N.checkArgNotNull(keyMapper);

        final SetMultimap multimap = N.newSetMultimap(N.size(c));

        if (N.notNullOrEmpty(c)) {
            for (T e : c) {
                multimap.put(keyMapper.apply(e), e);
            }
        }

        return multimap;
    }

    /**
     *
     * @param 
     * @param  the key type
     * @param 
     * @param 
     * @param 
     * @param c
     * @param keyMapper
     * @param valueExtractor
     * @return
     * @throws X the x
     * @throws X2 the x2
     */
    public static  SetMultimap from(final Collection c,
            final Throwables.Function keyMapper, final Throwables.Function valueExtractor) throws X, X2 {
        N.checkArgNotNull(keyMapper);
        N.checkArgNotNull(valueExtractor);

        final SetMultimap multimap = N.newSetMultimap(N.size(c));

        if (N.notNullOrEmpty(c)) {
            for (T e : c) {
                multimap.put(keyMapper.apply(e), valueExtractor.apply(e));
            }
        }

        return multimap;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param map
     * @return
     * @see Multimap#invertFrom(Map, Supplier)
     */
    public static  SetMultimap invertFrom(final Map map) {
        final SetMultimap multimap = new SetMultimap<>(Maps.newOrderingMap(map), HashSet.class);

        if (N.notNullOrEmpty(map)) {
            for (Map.Entry entry : map.entrySet()) {
                multimap.put(entry.getValue(), entry.getKey());
            }
        }

        return multimap;
    }

    /**
     * Flat invert from.
     *
     * @param  the key type
     * @param 
     * @param map
     * @return
     * @see Multimap#flatInvertFrom(Map, Supplier)
     */
    public static  SetMultimap flatInvertFrom(final Map> map) {
        final SetMultimap multimap = new SetMultimap<>(Maps.newOrderingMap(map), HashSet.class);

        if (N.notNullOrEmpty(map)) {
            for (Map.Entry> entry : map.entrySet()) {
                final Collection c = entry.getValue();

                if (N.notNullOrEmpty(c)) {
                    for (E e : c) {
                        multimap.put(e, entry.getKey());
                    }
                }
            }
        }

        return multimap;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param map
     * @return
     */
    public static > SetMultimap invertFrom(final Multimap map) {
        final SetMultimap multimap = new SetMultimap<>(Maps.newOrderingMap(map.valueMap), HashSet.class);

        if (N.notNullOrEmpty(map)) {
            for (Map.Entry entry : map.entrySet()) {
                final V c = entry.getValue();

                if (N.notNullOrEmpty(c)) {
                    for (E e : c) {
                        multimap.put(e, entry.getKey());
                    }
                }
            }
        }

        return multimap;
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param a
     * @param b
     * @return
     */
    public static  SetMultimap concat(final Map a, final Map b) {
        if (a == null) {
            return b == null ? N. newSetMultimap() : from(b);
        } else {
            final SetMultimap res = from(a);
            res.putAll(b);
            return res;
        }
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param a
     * @param b
     * @param c
     * @return
     */
    public static  SetMultimap concat(final Map a, final Map b,
            final Map c) {
        if (a == null) {
            if (b == null) {
                return c == null ? N. newSetMultimap() : from(c);
            } else {
                final SetMultimap res = from(b);
                res.putAll(c);
                return res;
            }
        } else {
            final SetMultimap res = from(a);
            res.putAll(b);
            res.putAll(c);
            return res;
        }
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param map
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static > SetMultimap wrap(final Map map) {
        N.checkArgNotNull(map);
        N.checkArgument(N.anyNull(map.values()), "The specified map contains null value: %s", map);

        Class valueType = HashSet.class;

        for (V v : map.values()) {
            if (v != null) {
                valueType = v.getClass();
                break;
            }
        }

        return new SetMultimap<>((Map>) map, valueType);
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param map
     * @param valueSupplier
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static > SetMultimap wrapp(final Map map, final Supplier valueSupplier) {
        N.checkArgNotNull(map, "map");
        N.checkArgNotNull(valueSupplier, "valueSupplier");

        return new SetMultimap<>((Map) map, valueSupplier);
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param map
     * @param multimapSupplier
     * @return
     */
    @Deprecated
    public static , M extends Multimap> M from(final Map map,
            final IntFunction multimapSupplier) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param map
     * @param multimapSupplier
     * @return
     */
    @Deprecated
    public static , M extends Multimap> M fromm(final Map> map,
            final IntFunction multimapSupplier) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param 
     * @param  the key type
     * @param  the value type
     * @param 
     * @param 
     * @param c
     * @param keyMapper
     * @param multimapSupplier
     * @return
     * @throws X the x
     */
    @Deprecated
    public static , M extends Multimap, X extends Exception> M from(final Collection c,
            final Throwables.Function keyMapper, final IntFunction multimapSupplier) throws X {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param 
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param 
     * @param 
     * @param c
     * @param keyMapper
     * @param valueExtractor
     * @param multimapSupplier
     * @return
     * @throws X the x
     * @throws X2 the x2
     */
    @Deprecated
    public static , M extends Multimap, X extends Exception, X2 extends Exception> M from(
            final Collection c, final Throwables.Function keyMapper,
            final Throwables.Function valueExtractor, final IntFunction multimapSupplier) throws X, X2 {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param map
     * @param multimapSupplier
     * @return
     */
    @Deprecated
    public static , M extends Multimap> M invertFrom(final Map map,
            final IntFunction multimapSupplier) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Flat invert from.
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param map
     * @param multimapSupplier
     * @return
     */
    @Deprecated
    public static , M extends Multimap> M flatInvertFrom(final Map> map,
            final IntFunction multimapSupplier) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param 
     * @param multimap
     * @param multimapSupplier
     * @return
     */
    @Deprecated
    public static , VV extends Collection, M extends Multimap> M invertFrom(final Multimap multimap,
            final IntFunction multimapSupplier) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param a
     * @param b
     * @param multimapSupplier
     * @return
     */
    @Deprecated
    public static , M extends Multimap> M concat(final Map a,
            final Map b, final IntFunction multimapSupplier) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param 
     * @param a
     * @param b
     * @param c
     * @param multimapSupplier
     * @return
     */
    @Deprecated
    public static , M extends Multimap> M concat(final Map a,
            final Map b, final Map c, final IntFunction multimapSupplier)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param  the key type
     * @param 
     * @param  the value type
     * @param map
     * @param valueSupplier
     * @return
     */
    @Deprecated
    public static > Multimap wrap(final Map map, final Supplier valueSupplier)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Filter by key.
     *
     * @param 
     * @param filter
     * @return
     * @throws X the x
     */
    @Override
    public  SetMultimap filterByKey(Throwables.Predicate filter) throws X {
        final SetMultimap result = new SetMultimap<>(mapSupplier, valueSupplier);

        for (Map.Entry> entry : valueMap.entrySet()) {
            if (filter.test(entry.getKey())) {
                result.valueMap.put(entry.getKey(), entry.getValue());
            }
        }

        return result;
    }

    /**
     * Filter by value.
     *
     * @param 
     * @param filter
     * @return
     * @throws X the x
     */
    @Override
    public  SetMultimap filterByValue(Throwables.Predicate, X> filter) throws X {
        final SetMultimap result = new SetMultimap<>(mapSupplier, valueSupplier);

        for (Map.Entry> entry : valueMap.entrySet()) {
            if (filter.test(entry.getValue())) {
                result.valueMap.put(entry.getKey(), entry.getValue());
            }
        }

        return result;
    }

    /**
     *
     * @param 
     * @param filter
     * @return
     * @throws X the x
     */
    @Override
    public  SetMultimap filter(Throwables.BiPredicate, X> filter) throws X {
        final SetMultimap result = new SetMultimap<>(mapSupplier, valueSupplier);

        for (Map.Entry> entry : valueMap.entrySet()) {
            if (filter.test(entry.getKey(), entry.getValue())) {
                result.valueMap.put(entry.getKey(), entry.getValue());
            }
        }

        return result;
    }

    @Override
    public SetMultimap copy() {
        final SetMultimap copy = new SetMultimap<>(mapSupplier, valueSupplier);

        copy.putAll(this);

        return copy;
    }

    /**
     * To immutable map.
     *
     * @return
     */
    public ImmutableMap> toImmutableMap() {
        final Map> map = Maps.newOrderingMap(valueMap);

        for (Map.Entry> entry : valueMap.entrySet()) {
            map.put(entry.getKey(), ImmutableSet.copyOf(entry.getValue()));
        }

        return ImmutableMap.of(map);
    }

    /**
     * To immutable map.
     *
     * @param mapSupplier
     * @return
     */
    public ImmutableMap> toImmutableMap(final IntFunction>> mapSupplier) {
        final Map> map = mapSupplier.apply(valueMap.size());

        for (Map.Entry> entry : valueMap.entrySet()) {
            map.put(entry.getKey(), ImmutableSet.copyOf(entry.getValue()));
        }

        return ImmutableMap.of(map);
    }

    // It won't work.
    //    /**
    //     * Returns a synchronized {@code SetMultimap} which shares the same internal {@code Map} with this {@code SetMultimap}.
    //     * That's to say the changes in one of the returned {@code SetMultimap} and this {@code SetMultimap} will impact another one.
    //     *
    //     * @see Collections#synchronizedMap(Map)
    //     */
    //    @Override
    //    public SetMultimap synchronizedd() {
    //        return new SetMultimap<>(Collections.synchronizedMap(valueMap), concreteValueType);
    //    }

    //    public SetMultimap inversed() {
    //        final SetMultimap multimap = new SetMultimap(valueMap.getClass(), concreteValueType);
    //
    //        if (N.notNullOrEmpty(valueMap)) {
    //            for (Map.Entry> entry : valueMap.entrySet()) {
    //                final Set c = entry.getValue();
    //
    //                if (N.notNullOrEmpty(c)) {
    //                    for (E e : c) {
    //                        multimap.put(e, entry.getKey());
    //                    }
    //                }
    //            }
    //        }
    //
    //        return multimap;
    //    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy