/*******************************************************************************
 * Copyright (C) 2013, 2014 Olaf Lessenich.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 *
 * Contributors:
 *     Olaf Lessenich - initial API and implementation
 *******************************************************************************/
package de.fosd.jdime.strategy;

import java.util.HashMap;
import java.util.Set;

import de.fosd.jdime.common.Artifact;
import de.fosd.jdime.merge.MergeInterface;

/**
 * @author Olaf Lessenich
 *
 * @param <T>
 *            type of artifact
 */
public abstract class MergeStrategy<T extends Artifact<T>> implements
		MergeInterface<T>, StatsInterface<T>, DumpInterface<T> {

	/**
	 * Map holding all strategies.
	 */
	private static HashMap<String, MergeStrategy<?>> strategyMap = null;

	/**
	 * Initializes the strategy map.
	 */
	private static void initialize() {
		strategyMap = new HashMap<>();
		LinebasedStrategy linebased = new LinebasedStrategy();
		StructuredStrategy structured = new StructuredStrategy();
		CombinedStrategy combined = new CombinedStrategy();

		strategyMap.put("linebased", linebased);
		strategyMap.put("unstructured", linebased);

		strategyMap.put("structured", structured);

		strategyMap.put("combined", combined);
		strategyMap.put("autotuning", combined);
	}

	/**
	 * Returns a set containing the names of available strategies.
	 *
	 * @return names of available strategies
	 */
	public static Set<String> listStrategies() {
		if (strategyMap == null) {
			initialize();
		}

		assert (strategyMap != null);

		return strategyMap.keySet();
	}

	/**
	 * Parses a String and returns a strategy. Null is returned if no
	 * appropriate Tool is found.
	 *
	 * @param str
	 *            name of the merge tool
	 * @return MergeStrategy merge strategy
	 */
	public static MergeStrategy<?> parse(final String str) {
		assert str != null : "Merge strategy may not be null!";

		String input = str.toLowerCase();

		if (strategyMap == null) {
			initialize();
		}

		assert (strategyMap != null);

		if (strategyMap.containsKey(input)) {
			return strategyMap.get(input);
		} else {
			throw new StrategyNotFoundException("Strategy not found: " + str);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public abstract String toString();
}

@@ -1071,20 +1071,72 @@
 va.util.
-Hash
+Collections;%0Aimport java.util.HashMap;%0Aimport java.util.
 Map;%0Aimp
@@ -1453,50 +1453,8 @@
  %7B%0A%0A
-%09/**%0A%09 * Map holding all strategies.%0A%09 */%0A
 %09pri
@@ -1465,20 +1465,22 @@
  static 
-Hash
+final 
 Map%3CStri
@@ -1516,111 +1516,60 @@
 yMap
- = null;%0A%0A%09/**%0A%09 * Initializes the strategy map.%0A%09 */%0A%09private static void initialize() %7B%0A%09%09strategyMap
+;%0A%0A%09static %7B%0A%09%09Map%3CString, MergeStrategy%3C?%3E%3E entries
  = n
@@ -1604,25 +1604,25 @@
 trategy line
-b
+B
 ased = new L
@@ -1754,35 +1754,31 @@
 ategy();%0A%0A%09%09
-strategyMap
+entries
 .put(%22lineba
@@ -1783,25 +1783,25 @@
 based%22, line
-b
+B
 ased);%0A%09%09str
@@ -1793,35 +1793,31 @@
 neBased);%0A%09%09
-strategyMap
+entries
 .put(%22unstru
@@ -1833,30 +1833,25 @@
 line
-b
+B
 ased);%0A
-%0A
 %09%09
-strategyMap
+entries
 .put
@@ -1878,30 +1878,25 @@
 tured);%0A
-%0A
 %09%09
-strategyMap
+entries
 .put(%22co
@@ -1917,27 +1917,23 @@
 ned);%0A%09%09
-strategyMap
+entries
 .put(%22au
@@ -1954,16 +1954,73 @@
 bined);%0A
+%09%09%0A%09%09strategyMap = Collections.unmodifiableMap(entries);%0A
 %09%7D%0A%0A%09/**
@@ -2185,92 +2185,8 @@
 ) %7B%0A
-%09%09if (strategyMap == null) %7B%0A%09%09%09initialize();%0A%09%09%7D%0A%0A%09%09assert (strategyMap != null);%0A%0A
 %09%09re
@@ -2580,92 +2580,8 @@
 );%0A%0A
-%09%09if (strategyMap == null) %7B%0A%09%09%09initialize();%0A%09%09%7D%0A%0A%09%09assert (strategyMap != null);%0A%0A
 %09%09if
@@ -2735,17 +2735,16 @@
 r);%0A%09%09%7D%0A
-%0A
 %09%7D%0A%0A%09/*%0A

initialize static final map in static block, use unmodifiable Map, remove initializer method and null tests for the Map