org.boon.Maps Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of boon Show documentation
Show all versions of boon Show documentation
Simple opinionated Java for the novice to expert level Java Programmer.
Low Ceremony. High Productivity. A real boon to Java to developers!
package org.boon;
import org.boon.core.Typ;
import org.boon.core.reflection.BeanUtils;
import org.boon.core.Conversions;
import org.boon.core.reflection.MapObjectConversion;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class Maps {
/**
* 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 );
}
@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 );
}
}
@Universal
public static V after( NavigableMap map, final K index ) {
return map.get( map.higherKey( index ) );
}
@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 EntryImpl<>( k, v );
}
public static Entry entry( Entry entry ) {
return new EntryImpl<>( entry );
}
public static interface Entry extends Comparable,
Serializable, Cloneable {
K key();
V value();
boolean equals( Entry o );
}
public static class EntryImpl implements Entry {
private K k;
private V v;
public EntryImpl() {
}
public EntryImpl( EntryImpl impl ) {
Objects.requireNonNull( impl );
Objects.requireNonNull( impl.k );
this.k = impl.k;
this.v = impl.v;
}
public EntryImpl( Entry entry ) {
Objects.requireNonNull( entry );
Objects.requireNonNull( entry.key() );
this.k = entry.key();
this.v = entry.value();
}
public EntryImpl( K k, V v ) {
Objects.requireNonNull( k );
this.k = k;
this.v = v;
}
@Override
public K key() {
return k;
}
@Override
public V value() {
return v;
}
@Override
public boolean equals( Object o ) {
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
EntryImpl entry = ( EntryImpl ) o;
return this.equals( entry );
}
@Override
public boolean equals( Entry entry ) {
if ( k != null ? !k.equals( entry.key() ) : entry.key() != null ) return false;
return !( v != null ? !v.equals( entry.value() ) : entry.value() != null );
}
@Override
public int hashCode() {
int result = k != null ? k.hashCode() : 0;
result = 31 * result + ( v != null ? v.hashCode() : 0 );
return result;
}
@Override
public int compareTo( Entry entry ) {
Objects.requireNonNull( entry );
return this.key().toString().compareTo( entry.key().toString() );
}
@Override
public String toString() {
return "{" +
"\"k\":" + k +
", \"v\":" + v +
'}';
}
}
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( Collection keys, Collection 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( 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;
}
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( 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