Java tutorial
/******************************************************************************* * Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ package org.summer.dsl.xbase.lib; import java.util.Map; import org.summer.dsl.xbase.lib.Functions.Function1; import org.summer.dsl.xbase.lib.Functions.Function2; import org.summer.dsl.xbase.lib.Procedures.Procedure2; import org.summer.dsl.xbase.lib.Procedures.Procedure3; import org.summer.dsl.xbase.lib.internal.FunctionDelegate; import com.google.common.annotations.GwtCompatible; import com.google.common.base.Predicate; import com.google.common.collect.Maps; /** * This is an extension library for {@link Map maps}. * * @author Sebastian Zarnekow - Initial contribution and API */ @GwtCompatible public class MapExtensions { /** * Applies the given {@code procedure} for each {@link java.util.Map.Entry key value pair} of the given {@code map}. * * @param map * the map. May not be <code>null</code>. * @param procedure * the procedure. May not be <code>null</code>. */ public static <K, V> void forEach(Map<K, V> map, Procedure2<? super K, ? super V> procedure) { if (procedure == null) throw new NullPointerException("procedure"); for (Map.Entry<K, V> entry : map.entrySet()) { procedure.apply(entry.getKey(), entry.getValue()); } } /** * Applies the given {@code procedure} for each {@link java.util.Map.Entry key value pair} of the given {@code map}. * The procedure takes the key, the value and a loop counter. If the counter would overflow, {@link Integer#MAX_VALUE} * is returned for all subsequent pairs. The first pair is at index zero. * * @param map * the map. May not be <code>null</code>. * @param procedure * the procedure. May not be <code>null</code>. */ public static <K, V> void forEach(Map<K, V> map, Procedure3<? super K, ? super V, ? super Integer> procedure) { if (procedure == null) throw new NullPointerException("procedure"); int i = 0; for (Map.Entry<K, V> entry : map.entrySet()) { procedure.apply(entry.getKey(), entry.getValue(), i); if (i != Integer.MAX_VALUE) i++; } } /** * <p> * Returns a filtered live view on top of the original map. Changes to one affect the other. * </p> * * <p> * The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the * transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the * original map are reflected in the result map. The results supports removal if the original map supports removal. * </p> * * @param original * the original map. May not be <code>null</code>. * @param predicate * the predicate. May not be <code>null</code>. * @return a filtered view on top of the original map. Never <code>null</code>. */ public static <K, V> Map<K, V> filter(Map<K, V> original, final Function2<? super K, ? super V, Boolean> predicate) { if (predicate == null) throw new NullPointerException("predicate"); return Maps.filterEntries(original, new Predicate<Map.Entry<K, V>>() { public boolean apply(Map.Entry<K, V> input) { Boolean result = predicate.apply(input.getKey(), input.getValue()); return result.booleanValue(); } }); } /** * <p>Returns a map that performs the given {@code transformation} for each value of {@code original} when requested.</p> * * <p>The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the * transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the * original map are reflected in the result map. The results supports removal if the original map supports removal.</p> * * @param original * the original map. May not be <code>null</code>. * @param transformation * the transformation. May not be <code>null</code>. * @return a map with equal keys but transformed values. Never <code>null</code>. * @since 2.4 */ public static <K, V1, V2> Map<K, V2> mapValues(Map<K, V1> original, Function1<? super V1, ? extends V2> transformation) { return Maps.transformValues(original, new FunctionDelegate<V1, V2>(transformation)); } }