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

org.boon.Maps Maven / Gradle / Ivy

Go to download

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity. A real boon to Java to developers!

There is a newer version: 0.34
Show newest version
/*
 * Copyright 2013-2014 Richard M. Hightower
 * 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.
 *
 * __________                              _____          __   .__
 * \______   \ ____   ____   ____   /\    /     \ _____  |  | _|__| ____    ____
 *  |    |  _//  _ \ /  _ \ /    \  \/   /  \ /  \\__  \ |  |/ /  |/    \  / ___\
 *  |    |   (  <_> |  <_> )   |  \ /\  /    Y    \/ __ \|    <|  |   |  \/ /_/  >
 *  |______  /\____/ \____/|___|  / \/  \____|__  (____  /__|_ \__|___|  /\___  /
 *         \/                   \/              \/     \/     \/       \//_____/
 *      ____.                     ___________   _____    ______________.___.
 *     |    |____ ___  _______    \_   _____/  /  _  \  /   _____/\__  |   |
 *     |    \__  \\  \/ /\__  \    |    __)_  /  /_\  \ \_____  \  /   |   |
 * /\__|    |/ __ \\   /  / __ \_  |        \/    |    \/        \ \____   |
 * \________(____  /\_/  (____  / /_______  /\____|__  /_______  / / ______|
 *               \/           \/          \/         \/        \/  \/
 */

package org.boon;


import org.boon.core.Typ;
import org.boon.core.Type;
import org.boon.core.reflection.BeanUtils;
import org.boon.core.Conversions;
import org.boon.core.reflection.MapObjectConversion;
import org.boon.primitive.CharBuf;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;

import static org.boon.Exceptions.die;
import static org.boon.Exceptions.requireNonNull;

public class Maps {



    public static  List lazyCreate( List lazy ) {
        if (lazy == null) {

            lazy = new ArrayList<>();
        }
        return lazy;
    }

    public static  Map lazyCreate( Map lazy ) {
        if (lazy == null) {

            lazy = new LinkedHashMap();
        }
        return lazy;
    }


    public static  Map lazyCreate( HashMap lazy ) {
        if (lazy == null) {

            lazy = new HashMap();
        }
        return lazy;
    }


    public static  Map lazyCreate( LinkedHashMap lazy ) {
        if (lazy == null) {

            lazy = new LinkedHashMap();
        }
        return lazy;
    }


    public static  Map lazyCreateLinked( Map lazy ) {
        if (lazy == null) {

            lazy = new LinkedHashMap();
        }
        return lazy;
    }



    public static  Map lazyCreate( ConcurrentHashMap lazy ) {
        if (lazy == null) {

            lazy = new ConcurrentHashMap();
        }
        return lazy;
    }

    public static  Map lazyCreateSafe( Map lazy ) {
        if (lazy == null) {

            lazy = new ConcurrentHashMap();
        }
        return lazy;
    }


    @Universal
    public static int lengthOf( Map map ) {
        return len ( map );
    }

    @Universal
    public static  V atIndex( Map map, K k ) {
        return idx(map, k );
    }

    @Universal
    public static  SortedMap sliceOf( NavigableMap map, K startIndex, K endIndex ) {
        return slc(map, startIndex, endIndex);
    }


    @Universal
    public static  SortedMap endSliceOf( NavigableMap map, K fromKey ) {
        return slcEnd(map, fromKey);
    }

        /**
         * Universal methods.
         */
    @Universal
    public static int len( Map map ) {
        return map.size();
    }


    @Universal
    public static  boolean in( K key, Map map ) {
        return map.containsKey( key );
    }

    @Universal
    public static  void add( Map map, Entry entry ) {
        map.put( entry.key(), entry.value() );
    }

    @Universal
    public static  V idx( Map map, K k ) {
        return map.get( k );
    }

    @Universal
    public static  void idx( Map map, K k, V v ) {
        map.put( k, v );
    }



    public static  String idxStr( Map map, K k ) {
        return (String)map.get( k );
    }


    public static  Integer idxInt( Map map, K k ) {
        return (Integer)map.get( k );
    }



    public static  Long idxLong( Map map, K k ) {
        return (Long)map.get( k );
    }


    public static  Map idxMap( Map map, K k ) {
        return  (Map) map.get( k );
    }


    public static  List idxList( Map map, K k ) {
        return  (List) map.get( k );
    }



    public static  long toLong( Map map, K key ) {
        V value = map.get(key);
        long l = Conversions.toLong ( value, Long.MIN_VALUE );
        if ( l == Long.MIN_VALUE ) {
            die("Cannot convert", key, "into long value", value);
        }
        return l;
    }


    public static  int toInt( Map map, K key ) {
        V value = map.get(key);
        int v = Conversions.toInt ( value, Integer.MIN_VALUE );
        if ( v == Integer.MIN_VALUE ) {
            die("Cannot convert", key, "into int value", value);
        }
        return v;
    }

    @Universal
    public static  SortedMap copy( SortedMap map ) {
        if ( map instanceof TreeMap ) {
            return new TreeMap<>( map );
        } else if ( map instanceof ConcurrentSkipListMap ) {
            return new ConcurrentSkipListMap<>( map );
        } else {
            return new TreeMap<>( map );
        }
    }

    @Universal
    public static  Map copy( Map map ) {
        if ( map instanceof LinkedHashMap ) {
            return new LinkedHashMap<>( map );
        } else if ( map instanceof ConcurrentHashMap ) {
            return new ConcurrentHashMap<>( map );
        } else {
            return new LinkedHashMap<>( map );
        }
    }


    /** Grabs the first value from a tree map (Navigable map). */
    @Universal
    public static  V first( NavigableMap map ) {
        return map.firstEntry().getValue();
    }



    /** Grabs the last value from a tree map (Navigable map). */
    @Universal
    public static  V last( NavigableMap map ) {
        return map.lastEntry().getValue()   ;
    }



    /** Grabs the value after this key from a tree map (Navigable map). */
    @Universal
    public static  V after( NavigableMap map, final K index ) {
        return map.get( map.higherKey( index ) );
    }

    /** Grabs the value before this key from a tree map (Navigable map). */
    @Universal
    public static  V before( NavigableMap map, final K index ) {
        return map.get( map.lowerKey( index ) );
    }


    @Universal
    public static  SortedMap slc( NavigableMap map, K startIndex, K endIndex ) {
        return map.subMap( startIndex, endIndex );
    }


    @Universal
    public static  SortedMap slcEnd( NavigableMap map, K fromKey ) {
        return map.tailMap( fromKey );
    }

    @Universal
    public static  SortedMap slc( NavigableMap map, K toKey ) {
        return map.headMap( toKey );
    }

    /**
     * End universal methods.
     */

    public static  boolean valueIn( V value, Map map ) {
        return map.containsValue( value );
    }


    public static  Entry entry( final K k, final V v ) {
        return new Pair<>( k, v );
    }

    public static  Entry entry( Entry entry ) {
        return new Pair<>( entry );
    }

    public static Map mapFromArray( Object... args ) {
        Map map = map(Object.class, Object.class);

        if (args.length % 2 != 0) {
            return die(Map.class, "mapFromArray arguments must be equal");
        }

        Object lastKey = null;
        for (int index = 0; index < args.length; index++) {

            if (index % 2 == 0) {
                lastKey = args[index];
            } else {
                map.put( lastKey, args[index] );
            }

        }
        return map;

    }


    public static  Map map( Class keyClass, Class valueClass ) {
        return new LinkedHashMap<>( 10 );
    }

    public static  Map safeMap( Class keyClass, Class valueClass ) {
        return new ConcurrentHashMap<>( 10 );
    }

    public static  Map map( K k0, V v0 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        return map;
    }


    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        map.put( k5, v5 );
        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        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  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        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  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9 ) {
        Map map = new LinkedHashMap<>( 10 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        return map;
    }


    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10 ) {
        Map map = new LinkedHashMap<>( 11 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );

        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11 ) {
        Map map = new LinkedHashMap<>( 12 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );

        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12 ) {
        Map map = new LinkedHashMap<>( 13 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );

        return map;
    }



    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13 ) {
        Map map = new LinkedHashMap<>( 14 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );
        map.put( k13, v13 );

        return map;
    }


    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13,
                                        K k14, V v14) {
        Map map = new LinkedHashMap<>( 15 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );
        map.put( k13, v13 );
        map.put( k14, v14 );

        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13,
                                        K k14, V v14, K k15, V v15) {
        Map map = new LinkedHashMap<>( 16 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );
        map.put( k13, v13 );
        map.put( k14, v14 );
        map.put( k15, v15 );

        return map;
    }


    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13,
                                        K k14, V v14, K k15, V v15, K k16, V v16) {
        Map map = new LinkedHashMap<>( 17 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );
        map.put( k13, v13 );
        map.put( k14, v14 );
        map.put( k15, v15 );
        map.put( k16, v16 );

        return map;
    }


    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13,
                                        K k14, V v14, K k15, V v15, K k16, V v16, K k17, V v17) {
        Map map = new LinkedHashMap<>( 18 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );
        map.put( k13, v13 );
        map.put( k14, v14 );
        map.put( k15, v15 );
        map.put( k16, v16 );
        map.put( k17, v17 );


        return map;
    }

    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13,
                                        K k14, V v14, K k15, V v15, K k16, V v16, K k17, V v17,
                                        K k18, V v18) {
        Map map = new LinkedHashMap<>( 19 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );
        map.put( k13, v13 );
        map.put( k14, v14 );
        map.put( k15, v15 );
        map.put( k16, v16 );
        map.put( k17, v17 );
        map.put( k18, v18 );

        return map;
    }


    public static  Map map( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                        V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                        K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13,
                                        K k14, V v14, K k15, V v15, K k16, V v16, K k17, V v17,
                                        K k18, V v18, K k19, V v19) {
        Map map = new LinkedHashMap<>( 20 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        map.put( k10, v10 );
        map.put( k11, v11 );
        map.put( k12, v12 );
        map.put( k13, v13 );
        map.put( k14, v14 );
        map.put( k15, v15 );
        map.put( k16, v16 );
        map.put( k17, v17 );
        map.put( k18, v18 );
        map.put( k19, v19 );

        return map;
    }





    public static  Map map( List keys, List values ) {
        Map map = new LinkedHashMap<>( 10 + keys.size() );
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }


    public static  Map map( LinkedHashSet keys, LinkedHashSet values ) {
        Map map = new LinkedHashMap<>( 10 + keys.size() );
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    /**
     * Note, you need to make sure that the iterators are from some sort of ordered collection.
     * @param keys
     * @param values
     * @param 
     * @param 
     * @return
     */
    public static  Map map( Iterable keys, Iterable values ) {
        Map map = new LinkedHashMap<>();
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    public static  Map map( K[] keys, V[] values ) {

        Map map = new LinkedHashMap<>( 10 + keys.length );
        int index = 0;
        for ( K k : keys ) {
            if ( index < keys.length ) {
                V v = values[ index ];
                map.put( k, v );
            } else {
                map.put( k, null );
            }
            index++;
        }
        return map;
    }


    @SafeVarargs
    public static  Map map( Entry... entries ) {
        Map map = new LinkedHashMap<>( entries.length );
        for ( Entry entry : entries ) {
            map.put( entry.key(), entry.value() );
        }
        return map;
    }


    @SafeVarargs
    public static  Map mapByEntries( Entry... entries ) {
        Map map = new LinkedHashMap<>( entries.length );
        for ( Entry entry : entries ) {
            map.put( entry.key(), entry.value() );
        }
        return map;
    }


    public static  NavigableMap sortedMap( K k0, V v0 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        return map;
    }

    public static  NavigableMap sortedMap( K k0, V v0, K k1, V v1 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        return map;
    }

    public static  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        return map;
    }

    public static  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        return map;
    }

    public static  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        return map;
    }

    public static  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        map.put( k5, v5 );
        return map;
    }

    public static  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        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  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        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  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        return map;
    }

    public static  NavigableMap sortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                                       K k9, V v9 ) {
        NavigableMap map = new TreeMap<>();
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        return map;
    }

    public static  NavigableMap sortedMap( Collection keys, Collection values ) {
        NavigableMap map = new TreeMap<>();
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }


    public static  NavigableMap sortedMap( Iterable keys, Iterable values ) {
        NavigableMap map = new TreeMap<>();
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }


    public static  NavigableMap sortedMap( K[] keys, V[] values ) {

        NavigableMap map = new TreeMap<>();
        int index = 0;
        for ( K k : keys ) {
            if ( index < keys.length ) {
                V v = values[ index ];
                map.put( k, v );
            } else {
                map.put( k, null );
            }
            index++;
        }
        return map;
    }


    public static  NavigableMap sortedMap( List> entries ) {
        NavigableMap map = new TreeMap<>();
        for ( Entry entry : entries ) {
            map.put( entry.key(), entry.value() );
        }
        return map;
    }


    //

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        map.put( k5, v5 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        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  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        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  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                       V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                                       K k9, V v9 ) {
        NavigableMap map = new TreeMap<>( comparator );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, Collection keys, Collection values ) {
        NavigableMap map = new TreeMap<>( comparator );
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    public static  NavigableMap sortedMap( Comparator comparator, K[] keys, V[] values ) {

        NavigableMap map = new TreeMap<>( comparator );
        int index = 0;
        for ( K k : keys ) {
            if ( index < keys.length ) {
                V v = values[ index ];
                map.put( k, v );
            } else {
                map.put( k, null );
            }
            index++;
        }
        return map;
    }


    public static  NavigableMap sortedMap( Comparator comparator, List> entries ) {
        NavigableMap map = new TreeMap<>( comparator );
        for ( Entry entry : entries ) {
            map.put( entry.key(), entry.value() );
        }
        return map;
    }


    public static  Map safeMap( Map map ) {
        return new ConcurrentHashMap<>(map);
    }

    public static  Map safeMap( K k0, V v0 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        return map;
    }

    public static  Map safeMap( K k0, V v0, K k1, V v1 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        return map;
    }


    public static  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        return map;
    }

    public static  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                            V v3 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        return map;
    }

    public static  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                            V v3, K k4, V v4 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        return map;
    }

    public static  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                            V v3, K k4, V v4, K k5, V v5 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        map.put( k5, v5 );
        return map;
    }

    public static  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                            V v3, K k4, V v4, K k5, V v5, K k6, V v6 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        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  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                            V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        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  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                            V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        return map;
    }

    public static  Map safeMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                            V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                            K k9, V v9 ) {
        Map map = new ConcurrentHashMap<>( 10 );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        return map;
    }

    public static  Map safeMap( Collection keys, Collection values ) {
        Map map = new ConcurrentHashMap<>( 10 + keys.size() );
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    public static  Map safeMap( Iterable keys, Iterable values ) {
        Map map = new ConcurrentHashMap<>();
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    public static  Map safeMap( K[] keys, V[] values ) {

        Map map = new ConcurrentHashMap<>( 10 + keys.length );
        int index = 0;
        for ( K k : keys ) {
            if ( index < keys.length ) {
                V v = values[ index ];
                map.put( k, v );
            } else {
                map.put( k, null );
            }
            index++;
        }
        return map;
    }


    @SafeVarargs
    public static  Map safeMap( Entry... entries ) {
        Map map = new ConcurrentHashMap<>( entries.length );
        for ( Entry entry : entries ) {
            map.put( entry.key(), entry.value() );
        }
        return map;
    }


    public static  NavigableMap safeSortedMap( K k0, V v0 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        return map;
    }

    public static  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        return map;
    }


    public static  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        return map;
    }

    public static  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        return map;
    }

    public static  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        return map;
    }

    public static  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        map.put( k5, v5 );
        return map;
    }

    public static  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        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  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        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  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        return map;
    }

    public static  NavigableMap safeSortedMap( K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                                           K k9, V v9 ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Collection keys, Collection values ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    public static  NavigableMap safeSortedMap( Iterable keys, Iterable values ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    public static  NavigableMap safeSortedMap( K[] keys, V[] values ) {

        NavigableMap map = new ConcurrentSkipListMap<>();
        int index = 0;
        for ( K k : keys ) {
            if ( index < keys.length ) {
                V v = values[ index ];
                map.put( k, v );
            } else {
                map.put( k, null );
            }
            index++;
        }
        return map;
    }


    @SafeVarargs
    public static  NavigableMap safeSortedMap( Entry... entries ) {
        NavigableMap map = new ConcurrentSkipListMap<>();
        for ( Entry entry : entries ) {
            map.put( entry.key(), entry.value() );
        }
        return map;
    }


    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        map.put( k1, v1 );
        map.put( k2, v2 );
        map.put( k3, v3 );
        map.put( k4, v4 );
        map.put( k5, v5 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        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  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        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  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K k0, V v0, K k1, V v1, K k2, V v2, K k3,
                                                           V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8,
                                                           K k9, V v9 ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        map.put( k0, v0 );
        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 );
        map.put( k8, v8 );
        map.put( k9, v9 );
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, Collection keys, Collection values ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        Iterator iterator = values.iterator();
        for ( K k : keys ) {
            if ( iterator.hasNext() ) {
                V v = iterator.next();
                map.put( k, v );
            } else {
                map.put( k, null );
            }
        }
        return map;
    }

    public static  NavigableMap safeSortedMap( Comparator comparator, K[] keys, V[] values ) {

        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        int index = 0;
        for ( K k : keys ) {
            if ( index < keys.length ) {
                V v = values[ index ];
                map.put( k, v );
            } else {
                map.put( k, null );
            }
            index++;
        }
        return map;
    }


    public static  NavigableMap safeSortedMap( Comparator comparator, List> entries ) {
        NavigableMap map = new ConcurrentSkipListMap<>( comparator );
        for ( Entry entry : entries ) {
            map.put( entry.key(), entry.value() );
        }
        return map;
    }


    @SuppressWarnings ( { "unchecked", "rawtypes" } )
    public static  T idx( Class clz, Map map, Object key ) {
        Object value = map.get( key.toString() );
        if ( value == null ) {
            return ( T ) value;
        }
        if ( value.getClass() != clz ) {
            T t = Conversions.coerce( clz, value );
            return t;
        } else {
            return ( T ) value;
        }
    }


    public static  T fromMap( Map map, Class clazz ) {
        return MapObjectConversion.fromMap( map, clazz );
    }

    public static Object fromMap( final Map map ) {
        return MapObjectConversion.fromMap ( map );
    }


    public static Map toMap( final Object object ) {
        return MapObjectConversion.toMap ( object );
    }


    public static  Map> toMultiValueMap( final String propertyPath, final Collection collection ) {
        LinkedHashMap> map = new LinkedHashMap<>( collection.size() );

        for ( T item : collection ) {
            Object oKey = BeanUtils.idx( item, propertyPath );
            if ( oKey == null ) {
                continue;
            }
            String key = Conversions.coerce( Typ.string, oKey );

            List list = map.get( key );
            if ( list == null ) {
                list = new ArrayList<>();
                map.put( key, list );
            }
            list.add( item );

        }
        return map;

    }


    public static  Map toMap( final String propertyPath, final Collection collection ) {
        return toMap( Typ.string, propertyPath, collection );
    }

    public static  NavigableMap toSortedMap( final String propertyPath, final Collection collection ) {
        return toSortedMap( Typ.string, propertyPath, collection );
    }

    public static  NavigableMap toSafeSortedMap( final String propertyPath, final Collection collection ) {
        return toSafeSortedMap( Typ.string, propertyPath, collection );
    }

    public static  Map toSafeMap( final String propertyPath, final Collection collection ) {
        return toSafeMap( Typ.string, propertyPath, collection );
    }


    public static  Map toMap( Class keyType, final String propertyPath, final Collection collection ) {
        LinkedHashMap map = new LinkedHashMap<>( collection.size() );
        doPopulateMapWithCollectionAndPropPath( keyType, propertyPath, collection, map );
        return map;
    }

    public static  NavigableMap toSortedMap( Class keyType, final String propertyPath, final Collection collection ) {
        TreeMap map = new TreeMap<>();
        doPopulateMapWithCollectionAndPropPath( keyType, propertyPath, collection, map );
        return map;
    }

    public static  NavigableMap toSafeSortedMap( Class keyType, final String propertyPath, final Collection collection ) {
        ConcurrentSkipListMap map = new ConcurrentSkipListMap<>();
        doPopulateMapWithCollectionAndPropPath( keyType, propertyPath, collection, map );
        return map;
    }

    public static  Map toSafeMap( Class keyType, final String propertyPath, final Collection collection ) {
        ConcurrentHashMap map = new ConcurrentHashMap<>();
        doPopulateMapWithCollectionAndPropPath( keyType, propertyPath, collection, map );
        return map;
    }


    private static  void doPopulateMapWithCollectionAndPropPath( Class keyType, String propertyPath, Collection collection, Map map ) {
        for ( T item : collection ) {
            Object oKey = BeanUtils.idx( item, propertyPath );
            if ( oKey == null ) {
                continue;
            }
            K key = Conversions.coerce( keyType, oKey );
            map.put( key, item );

        }
    }


    public static   void copyKeys(Collection keys, Map sourceMap, Map destinationMap) {
        for (K key : keys) {
            V value = sourceMap.get(key);
            if (value != null) {
                destinationMap.put(key, value);
            }
        }
    }

    public static  Map copyKeys(Collection keys, Map sourceMap) {
        Map destinationMap = new ConcurrentHashMap<>();
        for (K key : keys) {
            V value = sourceMap.get(key);
            if (value != null) {
                destinationMap.put(key, value);
            }
        }
        return destinationMap;
    }


    public static String asPrettyJsonString(Map map) {
        CharBuf buf = CharBuf.createCharBuf();
        return buf.prettyPrintMap(map).toString();
    }

    public static Map  toPrettyMap(Object object) {
        return MapObjectConversion.toPrettyMap(object);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy