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

net.gdface.utils.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
// ______________________________________________________
// Generated by sql2java - https://github.com/10km/sql2java-2-6-7 (custom branch) 
// modified by guyadong from
// sql2java original version https://sourceforge.net/projects/sql2java/ 
// JDBC driver used at code generation time: com.mysql.jdbc.Driver
// template: collection.utils.java.vm
// ______________________________________________________
package net.gdface.utils;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import java.util.Objects;

/**
 * @author guyadong
 *
 */
public class CollectionUtils {
    private CollectionUtils() {}
    public static interface Function {
        /** 
         * function interface
         * @param input
         * @return output 
         */
        T apply( F input);
    }
    /**
     * dual transform type between L and R
* {@link #toRight(Object)} L -> R
* {@link #fromRight(Object)} R -> L * @author guyadong * * @param left type * @param right type */ public static interface DualTransformer{ /** * cast L to R * @param input * @return output R */ R toRight(L input); /** * cast R to L * @param input * @return output L */ L fromRight(R input); } public static final DualTransformer asDualTransformer(final Function t1,final Functiont2){ checkNotNull(t1); checkNotNull(t2); return new DualTransformer(){ @Override public R toRight(L input) { return t1.apply(input); } @Override public L fromRight(R input) { return t2.apply(input); } }; } public static final Function asToRightTransformer(final DualTransformer dual){ checkNotNull(dual); return new Function(){ @Override public R apply(L input) { return dual.toRight(input); } }; } public static final Function asFromRightTransformer(final DualTransformer dual){ checkNotNull(dual); return new Function(){ @Override public L apply(R input) { return dual.fromRight(input); } }; } /** * Ensures that an object reference passed as a parameter to the calling method is not null. * @return the non-null reference that was validated */ public static T checkNotNull(T reference) { if (reference == null) { throw new NullPointerException(); } return reference; } /** * Ensures that an object reference passed as a parameter to the calling method is not null. * @return the non-null reference that was validated */ public static T checkNotNull(T reference, Object errorMessage) { if (reference == null) { throw new NullPointerException(String.valueOf(errorMessage)); } return reference; } /** * Ensures that a collection reference passed as a parameter to the calling method not exists null element if it's not null. * @return the non-null-element reference that was validated */ public static> C checkNotNullElement(C c){ if(null != c){ for(Object e:c){ checkNotNull(e,"element of collection must not be null"); } } return c; } static V safeGet(Map map, Object key) { checkNotNull(map); try { return map.get(key); } catch (ClassCastException e) { return null; } catch (NullPointerException e) { return null; } } /** @see #tranformKeys(Map, DualTransformer)*/ public static finalMap tranformKeys(MapfromMap, Function transformer2, Function transformer1){ return tranformKeys(fromMap,asDualTransformer(transformer2,transformer1)); } /** * Returns a view of a map where each value is transformed by a function. All * other properties of the map, such as iteration order, are left intact.
* see also com.google.common.collect.Maps#transformEntries(Map, com.google.common.collect.Maps.EntryTransformer) * @param fromMap * @param transformer * @return Map transformed */ public static finalMap tranformKeys(MapfromMap, DualTransformer transformer){ return new TransformedMap(fromMap,transformer); } /** @see #transform(Set, DualTransformer)*/ public static final Settransform(Set fromSet, Function transformer2, Function transformer1){ return transform(fromSet,asDualTransformer(transformer2,transformer1)); } /** * Returns a view of a set where each key is transformed by a function. All * other properties of the map, such as iteration order, are left intact.
* see also com.google.common.collect.Maps#transformEntries(Map, com.google.common.collect.Maps.EntryTransformer) * @param fromSet * @param transformer * @return Set transformed */ public static final Settransform(Set fromSet, DualTransformer transformer){ return new TransformedSet(fromSet,transformer); } /** * Returns an iterator that applies {@code function} to each element of {@code fromIterator}.
* see also com.google.common.collect.Iterators#transform(Iterator, com.google.common.base.Function) * @param fromIterator * @param transformer * @return an iterator that applies {@code function} to each element of {@code fromIterator} */ public static Iterator transform(final Iterator fromIterator, final Function transformer) { return new TransformedIterator(fromIterator,transformer); } /** * 合并两个迭代器的数据为一个{@link Map}
* 要求两个迭代器的元素个数必须一致, * 如果{@code keys}中的元素为{@code null}, * 则该元素和{@code values}对应value的会自动被忽略 * @param keys * @param values * @return 合并后的{@link Map} */ public static Map merge(Iterator keys,Iteratorvalues){ if(keys == null || values == null){ throw new NullPointerException("keyItor or valueItor is null"); } HashMap map = new HashMap(); while(keys.hasNext() && values.hasNext()){ K key = keys.next(); if(key != null){ map.put(key, values.next()); } } if(keys.hasNext() || values.hasNext()){ throw new IllegalArgumentException("mismatch size of keys and values"); } return map; } /** * 合并两个{@link Iterable}的数据为一个{@link Map}
* 要求两个迭代器的元素个数必须一致 * 如果{@code keys}中的元素为{@code null}, * 则该元素和{@code values}对应value的会自动被忽略 * @param keys * @param values * @return 合并后的{@link Map} */ public static Map merge(Iterable keys,Iterablevalues){ if(keys == null || values == null){ throw new NullPointerException("keys or values is null"); } return merge(keys.iterator(),values.iterator()); } /** * 合并两个数组的数据为一个{@link Map}
* 如果{@code keys}中的元素为{@code null}, * 则该元素和{@code values}对应value的会自动被忽略 * @param keys * @param values * @return 合并后的{@link Map} */ public static Map merge(K[] keys,V[]values){ if(keys == null || values == null){ throw new NullPointerException("keys or values is null"); } return merge(Arrays.asList(keys),Arrays.asList(values)); } static class TransformedIterator implements Iterator { final Iterator backingIterator; private Function transformer; TransformedIterator(Iterator backingIterator,Function transformer) { this.backingIterator = checkNotNull(backingIterator); this.transformer = checkNotNull(transformer); } @Override public final boolean hasNext() { return backingIterator.hasNext(); } @Override public final T next() { return transformer.apply(backingIterator.next()); } @Override public final void remove() { backingIterator.remove(); } } static class TransformedSet extends AbstractSet { final Set fromSet; private final DualTransformer transformer; TransformedSet(Set fromSet, DualTransformer transformer) { checkNotNull(fromSet); checkNotNull(transformer); this.fromSet = fromSet; this.transformer = transformer; } @Override public int size() { return fromSet.size(); } @Override public Iterator iterator() { return transform(fromSet.iterator(), asToRightTransformer(transformer)); } @Override public boolean add(E2 e) { return fromSet.add(transformer.fromRight(e)); } @SuppressWarnings("unchecked") @Override public boolean contains(Object o) { try { return fromSet.contains(transformer.fromRight((E2) o)); } catch (ClassCastException e) { return false; } } @SuppressWarnings("unchecked") @Override public boolean remove(Object o) { try { return fromSet.remove(transformer.fromRight((E2) o)); } catch (ClassCastException e) { return false; } } } static class TransformedMap extends AbstractMap { final Map fromMap; private DualTransformer transformer; TransformedMap(Map fromMap, DualTransformer transformer) { checkNotNull(fromMap); checkNotNull(transformer); this.fromMap = fromMap; this.transformer = transformer; } @SuppressWarnings("unchecked") @Override public V get(Object key) { try { return fromMap.get(transformer.fromRight((K2) key)); } catch (ClassCastException e) { return null; } } @SuppressWarnings("unchecked") @Override public boolean containsKey(Object key) { try { return fromMap.containsKey(transformer.fromRight((K2) key)); } catch (ClassCastException e) { return false; } } @Override public V put(K2 key, V value) { return fromMap.put(transformer.fromRight(key), value); } @SuppressWarnings("unchecked") @Override public V remove(Object key) { try { return fromMap.remove(transformer.fromRight((K2) key)); } catch (ClassCastException e) { return null; } } @Override public Set> entrySet() { return new AbstractEntrySet(){ @Override Map map() { return TransformedMap.this; } @Override public Iterator> iterator() { return transform(fromMap.entrySet().iterator(), new Function,Entry>(){ @Override public java.util.Map.Entry apply(java.util.Map.Entry input) { return new TransformedEntry(input,asToRightTransformer(TransformedMap.this.transformer)); }}); }}; } abstract static class AbstractEntrySet extends AbstractSet> { /** * return map instance * @return */ abstract Map map(); @Override public int size() { return map().size(); } @Override public boolean contains(Object o) { if (o instanceof Entry) { Entry entry = (Entry) o; Object key = entry.getKey(); V value = safeGet(map(), key); return Objects.equals(value, entry.getValue()) && (value != null || map().containsKey(entry.getKey())); } return false; } @Override public boolean remove(Object o) { if (contains(o)) { Entry entry = (Entry) o; return map().keySet().remove(entry.getKey()); } return false; } } static class TransformedEntry implements Entry { final EntryfromEntry; final Function transformer2; TransformedEntry(EntryfromEntry,Function transformer2){ checkNotNull(fromEntry); this.fromEntry = fromEntry; checkNotNull(transformer2); this.transformer2 = transformer2; } @Override public K2 getKey() { return transformer2.apply(fromEntry.getKey()); } @Override public V getValue() { return fromEntry.getValue(); } @Override public V setValue(V value) { return fromEntry.setValue(value); } @Override public boolean equals(Object object) { if(object instanceof TransformedEntry){ return fromEntry.equals((TransformedEntry)object); } return super.equals(object); } @Override public int hashCode() { return fromEntry.hashCode(); } @Override public String toString() { return fromEntry.toString(); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy