/**
 * Copyright (C) 2013 Michael Mosmann <michael@mosmann.de>
 *
 * 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.
 */
package de.flapdoodle.guava;

import com.google.common.base.Function;
import de.flapdoodle.guava.functions.NoTransformation;
import java.util.Map;

public abstract class MapTransformations {

	private MapTransformations() {
		// no instance
	}

	public static <S, D, V, M> Map<D, M> transform(Map<S, V> map, final Function<? super S, D> keytransformation,
					final Foldleft<? super V, M> valuetransformation) {
		return Transformations.map(map.entrySet(), new Function<Map.Entry<S, V>, D>() {

			@Override
			public D apply(Map.Entry<S, V> input) {
				return keytransformation.apply(input.getKey());
			}
		}, new Foldleft<Map.Entry<S, V>, M>() {

			@Override
			public M apply(M left, Map.Entry<S, V> right) {
				V rightValue = right.getValue();
				try {
					return valuetransformation.apply(left, rightValue);
				} catch (RuntimeException rx) {
					throw new IllegalArgumentException("failed to apply '" + left + "' and '" + rightValue + "'", rx);
				}
			}
		});
	}

	public static <S, D, V, M> Map<D, M> transform(Map<S, V> map, final Function<? super S, D> keytransformation,
					final Function<? super V, M> valuetransformation) {
		return transform(map, keytransformation, new Folds.ValueFromLeftIllegalFold<V, M>(valuetransformation));
	}

	public static <S, D, V> Map<D, V> transformKeys(Map<S, V> map, final Function<? super S, D> keytransformation) {
		return transform(map, keytransformation, new NoTransformation<V>());
	}

	public static <K, V> Map<V, K> swap(Map<K, V> map) {
		return Transformations.map(map.entrySet(), new Function<Map.Entry<K, V>, V>() {

			@Override
			public V apply(Map.Entry<K, V> input) {
				return input.getValue();
			}

		}, new Function<Map.Entry<K, V>, K>() {

			@Override
			public K apply(Map.Entry<K, V> input) {
				return input.getKey();
			}

		});
	}
}

@@ -18,16 +18,21 @@
 (C) 2013
+%0A *  
  Michael
@@ -130,16 +130,19 @@
 cense%22);
+%0A *
  you may
@@ -145,19 +145,16 @@
  may not
-%0A *
  use thi
@@ -198,16 +198,19 @@
 License.
+%0A *
  You may
@@ -226,19 +226,16 @@
  copy of
-%0A *
  the Lic
@@ -247,16 +247,24 @@
 at%0A *%0A *
+        
  http://
@@ -445,16 +445,19 @@
 SIS,
+%0A *
  WITHOUT
 %0A * 
@@ -452,19 +452,16 @@
  WITHOUT
-%0A *
  WARRANT
@@ -521,16 +521,19 @@
 ied.
+%0A *
  See the
 %0A * 
@@ -528,19 +528,16 @@
  See the
-%0A *
  License
@@ -588,16 +588,19 @@
 ions and
+%0A *
  limitat
@@ -609,19 +609,16 @@
 ns under
-%0A *
  the Lic

add lic