stack/core/src/main/java/org/apache/usergrid/utils/MapUtils.java [37:268]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class MapUtils extends org.apache.commons.collections.MapUtils {

    public static <A, B> void addMapSet( Map<A, Set<B>> map, A a, B b ) {
        addMapSet( map, false, a, b );
    }


    @SuppressWarnings("unchecked")
    public static <A, B> void addMapSet( Map<A, Set<B>> map, boolean ignoreCase, A a, B b ) {

        Set<B> setB = map.get( a );
        if ( setB == null ) {
            if ( ignoreCase && ( b instanceof String ) ) {
                setB = ( Set<B> ) new TreeSet<String>( String.CASE_INSENSITIVE_ORDER );
            }
            else {
                setB = new LinkedHashSet<B>();
            }
            map.put( a, setB );
        }
        setB.add( b );
    }


    public static <A, B, C> void addMapMapSet( Map<A, Map<B, Set<C>>> map, A a, B b, C c ) {
        addMapMapSet( map, false, a, b, c );
    }


    @SuppressWarnings("unchecked")
    public static <A, B, C> void addMapMapSet( Map<A, Map<B, Set<C>>> map, boolean ignoreCase, A a, B b, C c ) {

        Map<B, Set<C>> mapB = map.get( a );
        if ( mapB == null ) {
            if ( ignoreCase && ( b instanceof String ) ) {
                mapB = ( Map<B, Set<C>> ) new TreeMap<String, Set<C>>( String.CASE_INSENSITIVE_ORDER );
            }
            else {
                mapB = new LinkedHashMap<B, Set<C>>();
            }
            map.put( a, mapB );
        }
        addMapSet( mapB, ignoreCase, b, c );
    }


    @SuppressWarnings("unchecked")
    public static <A, B, C, D> void addMapMapMapSet( Map<A, Map<B, Map<C, Set<D>>>> map, boolean ignoreCase, A a, B b,
                                                     C c, D d ) {
        Map<B, Map<C, Set<D>>> mapB = map.get( a );
        if ( mapB == null ) {
            if ( ignoreCase && ( b instanceof String ) ) {
                mapB = ( Map<B, Map<C, Set<D>>> ) new TreeMap<String, Map<C, Set<D>>>( String.CASE_INSENSITIVE_ORDER );
            }
            else {
                mapB = new LinkedHashMap<B, Map<C, Set<D>>>();
            }
            map.put( a, mapB );
        }
        addMapMapSet( mapB, ignoreCase, b, c, d );
    }


    public static <A, B, C> C getMapMap( Map<A, Map<B, C>> map, A a, B b ) {

        Map<B, C> mapB = map.get( a );
        if ( mapB == null ) {
            return null;
        }
        return mapB.get( b );
    }


    public static <A, B> void addMapList( Map<A, List<B>> map, A a, B b ) {

        List<B> listB = map.get( a );
        if ( listB == null ) {
            listB = new ArrayList<B>();
            map.put( a, listB );
        }
        listB.add( b );
    }


    public static <A, B> void addListToMapList( Map<A, List<B>> map, A a, List<B> b ) {

        List<B> listB = map.get( a );
        if ( listB == null ) {
            listB = new ArrayList<B>();
            map.put( a, listB );
        }
        listB.addAll( b );
    }


    @SuppressWarnings("unchecked")
    public static <K, V> V getValue( Map<K, ?> map, K k ) {
        V v = null;
        try {
            v = ( V ) map.get( k );
        }
        catch ( ClassCastException e ) {
            //logger.war( "Map value {} was not the expected class", map.get( k ), e );
        }

        return v;
    }


    @SuppressWarnings("unchecked")
    public static <K, V> Map<?, ?> map( Object... objects ) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        int i = 0;
        while ( i < objects.length ) {
            if ( objects[i] instanceof Map.Entry ) {
                Map.Entry<K, V> entry = ( Entry<K, V> ) objects[i];
                map.put( entry.getKey(), entry.getValue() );
                i++;
            }
            else if ( objects[i] instanceof Map ) {
                map.putAll( ( Map<? extends K, ? extends V> ) objects[i] );
                i++;
            }
            else if ( i < ( objects.length - 1 ) ) {
                K k = ( K ) objects[i];
                V v = ( V ) objects[i + 1];
                map.put( k, v );
                i += 2;
            }
            else {
                break;
            }
        }
        return map;
    }


    private static class SimpleMapEntry<K, V> implements Map.Entry<K, V> {

        private final K k;
        private V v;


        public SimpleMapEntry( K k, V v ) {
            this.k = k;
            this.v = v;
        }


        @Override
        public K getKey() {
            return k;
        }


        @Override
        public V getValue() {
            return v;
        }


        @Override
        public V setValue( V v ) {
            V oldV = this.v;
            this.v = v;
            return oldV;
        }
    }


    public static <K, V> Map.Entry<K, V> entry( K k, V v ) {
        return new SimpleMapEntry<K, V>( k, v );
    }


    public static <K, V> K getFirstKey( Map<K, V> map ) {
        if ( map == null ) {
            return null;
        }
        Entry<K, V> e = map.entrySet().iterator().next();
        if ( e != null ) {
            return e.getKey();
        }
        return null;
    }


    public static <V> Map<String, V> filter( Map<String, V> map, String prefix, boolean removePrefix ) {
        Map<String, V> filteredMap = new LinkedHashMap<String, V>();
        for ( Entry<String, V> entry : map.entrySet() ) {
            if ( entry.getKey().startsWith( prefix ) ) {
                if ( removePrefix ) {
                    filteredMap.put( entry.getKey().substring( prefix.length() ), entry.getValue() );
                }
                else {
                    filteredMap.put( entry.getKey(), entry.getValue() );
                }
            }
        }
        return filteredMap;
    }


    public static <V> Map<String, V> filter( Map<String, V> map, String prefix ) {
        return filter( map, prefix, false );
    }


    public static Properties filter( Properties properties, String prefix, boolean removePrefix ) {
        Properties filteredProperties = new Properties();
        for ( Entry<String, String> entry : asMap( properties ).entrySet() ) {
            if ( entry.getKey().startsWith( prefix ) ) {
                if ( removePrefix ) {
                    filteredProperties.put( entry.getKey().substring( prefix.length() ), entry.getValue() );
                }
                else {
                    filteredProperties.put( entry.getKey(), entry.getValue() );
                }
            }
        }
        return filteredProperties;
    }


    public static Properties filter( Properties properties, String prefix ) {
        return filter( properties, prefix, false );
    }


    @SuppressWarnings("unchecked")
    public static Map<String, String> asMap( Properties properties ) {
        return cast( properties );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/utils/MapUtils.java [37:268]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class MapUtils extends org.apache.commons.collections.MapUtils {

    public static <A, B> void addMapSet( Map<A, Set<B>> map, A a, B b ) {
        addMapSet( map, false, a, b );
    }


    @SuppressWarnings("unchecked")
    public static <A, B> void addMapSet( Map<A, Set<B>> map, boolean ignoreCase, A a, B b ) {

        Set<B> setB = map.get( a );
        if ( setB == null ) {
            if ( ignoreCase && ( b instanceof String ) ) {
                setB = ( Set<B> ) new TreeSet<String>( String.CASE_INSENSITIVE_ORDER );
            }
            else {
                setB = new LinkedHashSet<B>();
            }
            map.put( a, setB );
        }
        setB.add( b );
    }


    public static <A, B, C> void addMapMapSet( Map<A, Map<B, Set<C>>> map, A a, B b, C c ) {
        addMapMapSet( map, false, a, b, c );
    }


    @SuppressWarnings("unchecked")
    public static <A, B, C> void addMapMapSet( Map<A, Map<B, Set<C>>> map, boolean ignoreCase, A a, B b, C c ) {

        Map<B, Set<C>> mapB = map.get( a );
        if ( mapB == null ) {
            if ( ignoreCase && ( b instanceof String ) ) {
                mapB = ( Map<B, Set<C>> ) new TreeMap<String, Set<C>>( String.CASE_INSENSITIVE_ORDER );
            }
            else {
                mapB = new LinkedHashMap<B, Set<C>>();
            }
            map.put( a, mapB );
        }
        addMapSet( mapB, ignoreCase, b, c );
    }


    @SuppressWarnings("unchecked")
    public static <A, B, C, D> void addMapMapMapSet( Map<A, Map<B, Map<C, Set<D>>>> map, boolean ignoreCase, A a, B b,
                                                     C c, D d ) {
        Map<B, Map<C, Set<D>>> mapB = map.get( a );
        if ( mapB == null ) {
            if ( ignoreCase && ( b instanceof String ) ) {
                mapB = ( Map<B, Map<C, Set<D>>> ) new TreeMap<String, Map<C, Set<D>>>( String.CASE_INSENSITIVE_ORDER );
            }
            else {
                mapB = new LinkedHashMap<B, Map<C, Set<D>>>();
            }
            map.put( a, mapB );
        }
        addMapMapSet( mapB, ignoreCase, b, c, d );
    }


    public static <A, B, C> C getMapMap( Map<A, Map<B, C>> map, A a, B b ) {

        Map<B, C> mapB = map.get( a );
        if ( mapB == null ) {
            return null;
        }
        return mapB.get( b );
    }


    public static <A, B> void addMapList( Map<A, List<B>> map, A a, B b ) {

        List<B> listB = map.get( a );
        if ( listB == null ) {
            listB = new ArrayList<B>();
            map.put( a, listB );
        }
        listB.add( b );
    }


    public static <A, B> void addListToMapList( Map<A, List<B>> map, A a, List<B> b ) {

        List<B> listB = map.get( a );
        if ( listB == null ) {
            listB = new ArrayList<B>();
            map.put( a, listB );
        }
        listB.addAll( b );
    }


    @SuppressWarnings("unchecked")
    public static <K, V> V getValue( Map<K, ?> map, K k ) {
        V v = null;
        try {
            v = ( V ) map.get( k );
        }
        catch ( ClassCastException e ) {
            //LOG.war( "Map value {} was not the expected class", map.get( k ), e );
        }

        return v;
    }


    @SuppressWarnings("unchecked")
    public static <K, V> Map<?, ?> map( Object... objects ) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        int i = 0;
        while ( i < objects.length ) {
            if ( objects[i] instanceof Map.Entry ) {
                Map.Entry<K, V> entry = ( Entry<K, V> ) objects[i];
                map.put( entry.getKey(), entry.getValue() );
                i++;
            }
            else if ( objects[i] instanceof Map ) {
                map.putAll( ( Map<? extends K, ? extends V> ) objects[i] );
                i++;
            }
            else if ( i < ( objects.length - 1 ) ) {
                K k = ( K ) objects[i];
                V v = ( V ) objects[i + 1];
                map.put( k, v );
                i += 2;
            }
            else {
                break;
            }
        }
        return map;
    }


    private static class SimpleMapEntry<K, V> implements Map.Entry<K, V> {

        private final K k;
        private V v;


        public SimpleMapEntry( K k, V v ) {
            this.k = k;
            this.v = v;
        }


        @Override
        public K getKey() {
            return k;
        }


        @Override
        public V getValue() {
            return v;
        }


        @Override
        public V setValue( V v ) {
            V oldV = this.v;
            this.v = v;
            return oldV;
        }
    }


    public static <K, V> Map.Entry<K, V> entry( K k, V v ) {
        return new SimpleMapEntry<K, V>( k, v );
    }


    public static <K, V> K getFirstKey( Map<K, V> map ) {
        if ( map == null ) {
            return null;
        }
        Entry<K, V> e = map.entrySet().iterator().next();
        if ( e != null ) {
            return e.getKey();
        }
        return null;
    }


    public static <V> Map<String, V> filter( Map<String, V> map, String prefix, boolean removePrefix ) {
        Map<String, V> filteredMap = new LinkedHashMap<String, V>();
        for ( Entry<String, V> entry : map.entrySet() ) {
            if ( entry.getKey().startsWith( prefix ) ) {
                if ( removePrefix ) {
                    filteredMap.put( entry.getKey().substring( prefix.length() ), entry.getValue() );
                }
                else {
                    filteredMap.put( entry.getKey(), entry.getValue() );
                }
            }
        }
        return filteredMap;
    }


    public static <V> Map<String, V> filter( Map<String, V> map, String prefix ) {
        return filter( map, prefix, false );
    }


    public static Properties filter( Properties properties, String prefix, boolean removePrefix ) {
        Properties filteredProperties = new Properties();
        for ( Entry<String, String> entry : asMap( properties ).entrySet() ) {
            if ( entry.getKey().startsWith( prefix ) ) {
                if ( removePrefix ) {
                    filteredProperties.put( entry.getKey().substring( prefix.length() ), entry.getValue() );
                }
                else {
                    filteredProperties.put( entry.getKey(), entry.getValue() );
                }
            }
        }
        return filteredProperties;
    }


    public static Properties filter( Properties properties, String prefix ) {
        return filter( properties, prefix, false );
    }


    @SuppressWarnings("unchecked")
    public static Map<String, String> asMap( Properties properties ) {
        return cast( properties );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



