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

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

There is a newer version: 1.10.1
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.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.landawn.abacus.annotation.Internal;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.Supplier;

/**
 * 
 * @see N#newListMultimap()
 * @see N#newListMultimap(Class, Class)
 * @see N#newListMultimap(Supplier, Supplier)
 * 
 * @since 0.9
 * 
 * @author Haiyang Li
 */
public final class ListMultimap extends Multimap> {
    ListMultimap() {
        this(HashMap.class, ArrayList.class);
    }

    ListMultimap(int initialCapacity) {
        this(new HashMap>(initialCapacity), ArrayList.class);
    }

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

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

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

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

    public static  ListMultimap of(final K k1, final E v1) {
        final ListMultimap map = new ListMultimap<>();

        map.put(k1, v1);

        return map;
    }

    public static  ListMultimap of(final K k1, final E v1, final K k2, final E v2) {
        final ListMultimap map = new ListMultimap<>();

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

        return map;
    }

    public static  ListMultimap of(final K k1, final E v1, final K k2, final E v2, final K k3, final E v3) {
        final ListMultimap map = new ListMultimap<>();

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

        return map;
    }

    public static  ListMultimap 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 ListMultimap map = new ListMultimap<>();

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

        return map;
    }

    public static  ListMultimap 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 ListMultimap map = new ListMultimap<>();

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

        return map;
    }

    public static  ListMultimap 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 ListMultimap map = new ListMultimap<>();

        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;
    }

    public static  ListMultimap 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 ListMultimap map = new ListMultimap<>();

        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;
    }

    public static  ListMultimap from(final Map map) {
        final ListMultimap multimap = new ListMultimap<>(Maps.newTargetMap(map), ArrayList.class);

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

        return multimap;
    }

    public static  ListMultimap fromm(final Map> map) {
        final ListMultimap multimap = new ListMultimap<>(Maps.newTargetMap(map), ArrayList.class);

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

        return multimap;
    }

    public static  ListMultimap from(final Collection c,
            final Try.Function keyMapper) throws X {
        N.checkArgNotNull(keyMapper);

        final ListMultimap multimap = N.newListMultimap(N.initHashCapacity(c == null ? 0 : c.size()));

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

        return multimap;
    }

    public static  ListMultimap from(final Collection c,
            final Try.Function keyMapper, final Try.Function valueExtractor) throws X, X2 {
        N.checkArgNotNull(keyMapper);
        N.checkArgNotNull(valueExtractor);

        final ListMultimap multimap = N.newListMultimap(N.initHashCapacity(c == null ? 0 : c.size()));

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

        return multimap;
    }

    /**
     * 
     * @param map
     * @return
     * @see Multimap#invertFrom(Map, com.landawn.abacus.util.function.Supplier)
     */
    public static  ListMultimap invertFrom(final Map map) {
        final ListMultimap multimap = new ListMultimap<>(Maps.newOrderingMap(map), ArrayList.class);

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

        return multimap;
    }

    /**
     * 
     * @param map
     * @return
     * @see Multimap#flatInvertFrom(Map, com.landawn.abacus.util.function.Supplier)
     */
    public static  ListMultimap flatInvertFrom(final Map> map) {
        final ListMultimap multimap = new ListMultimap<>(Maps.newOrderingMap(map), ArrayList.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 map
     * @return
     */
    public static > ListMultimap invertFrom(final Multimap map) {
        final ListMultimap multimap = new ListMultimap<>(Maps.newOrderingMap(map.valueMap), ArrayList.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;
    }

    public static  ListMultimap concat(final Map a, final Map b) {
        if (a == null) {
            return b == null ? N. newListMultimap() : from(b);
        } else {
            final ListMultimap res = from(a);
            res.putAll(b);
            return res;
        }
    }

    public static  ListMultimap concat(final Map a, final Map b,
            final Map c) {
        if (a == null) {
            if (b == null) {
                return c == null ? N. newListMultimap() : from(c);
            } else {
                final ListMultimap res = from(b);
                res.putAll(c);
                return res;
            }
        } else {
            final ListMultimap res = from(a);
            res.putAll(b);
            res.putAll(c);
            return res;
        }
    }

    @SuppressWarnings("rawtypes")
    public static > ListMultimap wrap(final Map map) {
        N.checkArgNotNull(map);
        N.checkArgument(N.anyNull(map.values()), "The specified map contains null value: %s", map);

        Class valueType = ArrayList.class;

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

        return new ListMultimap((Map>) map, valueType);
    }

    @SuppressWarnings("rawtypes")
    public static > ListMultimap wrapp(final Map map, final Supplier valueSupplier) {
        N.checkArgNotNull(map, "map");
        N.checkArgNotNull(valueSupplier, "valueSupplier");

        return new ListMultimap((Map) map, valueSupplier);
    }

    @Deprecated
    public static , M extends Multimap> M from(final Map map,
            final IntFunction multimapSupplier) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , M extends Multimap> M fromm(final Map> map,
            final IntFunction multimapSupplier) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , M extends Multimap, X extends Exception> M from(final Collection c,
            final Try.Function keyMapper, final IntFunction multimapSupplier) throws X {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , M extends Multimap, X extends Exception, X2 extends Exception> M from(
            final Collection c, final Try.Function keyMapper,
            final Try.Function valueExtractor, final IntFunction multimapSupplier) throws X, X2 {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , M extends Multimap> M invertFrom(final Map map, final IntFunction multimapSupplier) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , M extends Multimap> M flatInvertFrom(final Map> map,
            final IntFunction multimapSupplier) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , VV extends Collection, M extends Multimap> M invertFrom(final Multimap multimap,
            final IntFunction multimapSupplier) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , M extends Multimap> M concat(final Map a,
            final Map b, final IntFunction multimapSupplier) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static , M extends Multimap> M concat(final Map a,
            final Map b, final Map c, final IntFunction multimapSupplier) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static > Multimap wrap(final Map map, final Supplier valueSupplier) {
        throw new UnsupportedOperationException();
    }

    @Override
    public  ListMultimap filterByKey(Try.Predicate filter) throws X {
        final ListMultimap result = new ListMultimap<>(mapSupplier, valueSupplier);

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

        return result;
    }

    @Override
    public  ListMultimap filterByValue(Try.Predicate, X> filter) throws X {
        final ListMultimap result = new ListMultimap<>(mapSupplier, valueSupplier);

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

        return result;
    }

    @Override
    public  ListMultimap filter(Try.BiPredicate, X> filter) throws X {
        final ListMultimap result = new ListMultimap<>(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 ListMultimap copy() {
        final ListMultimap copy = new ListMultimap<>(mapSupplier, valueSupplier);

        copy.putAll(this);

        return copy;
    }

    public ImmutableMap> toImmutableMap() {
        final Map> map = Maps.newOrderingMap(valueMap);

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

        return ImmutableMap.of(map);
    }

    public ImmutableMap> toImmutableMap(final IntFunction>> mapSupplier) {
        final Map> map = mapSupplier.apply(valueMap.size());

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

        return ImmutableMap.of(map);
    }

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

    //    public ListMultimap inversed() {
    //        final ListMultimap multimap = new ListMultimap(valueMap.getClass(), concreteValueType);
    //
    //        if (N.notNullOrEmpty(valueMap)) {
    //            for (Map.Entry> entry : valueMap.entrySet()) {
    //                final List 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