/*
 * Copyright 2012-2014 the original author or authors.
 *
 * 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 org.springframework.configurationmetadata;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * The default {@link ConfigurationMetadataRepository} implementation.
 *
 * @author Stephane Nicoll
 * @since 1.2.0
 */
public class SimpleConfigurationMetadataRepository implements ConfigurationMetadataRepository {

	private final Map<String, ConfigurationMetadataGroup> allGroups = new HashMap<String, ConfigurationMetadataGroup>();

	@Override
	public Map<String, ConfigurationMetadataGroup> getAllGroups() {
		return Collections.unmodifiableMap(this.allGroups);
	}

	@Override
	public Map<String, ConfigurationMetadataProperty> getAllProperties() {
		Map<String,ConfigurationMetadataProperty> properties = new HashMap<String, ConfigurationMetadataProperty>();
		for (ConfigurationMetadataGroup group : this.allGroups.values()) {
			properties.putAll(group.getProperties());
		}
		return properties;
	}

	/**
	 * Register the specified {@link ConfigurationMetadataSource sources}.
	 */
	public void add(Collection<ConfigurationMetadataSource> sources) {
		for (ConfigurationMetadataSource source : sources) {
			String groupId = source.getGroupId();
			ConfigurationMetadataGroup group = this.allGroups.get(groupId);
			if (group == null) {
				group = new ConfigurationMetadataGroup(groupId);
				this.allGroups.put(groupId, group);
			}
			String sourceType = source.getType();
			if (sourceType != null) {
				group.getSources().putIfAbsent(sourceType, source);
			}
		}
	}

	/**
	 * Add a {@link ConfigurationMetadataProperty} with the {@link ConfigurationMetadataSource source}
	 * that defines it, if any.
	 */
	public void add(ConfigurationMetadataProperty property, ConfigurationMetadataSource source) {
		if (source != null) {
			source.getProperties().putIfAbsent(property.getId(), property);
		}
		getGroup(source).getProperties().putIfAbsent(property.getId(), property);
	}


	/**
	 * Merge the content of the specified repository to this repository.
	 */
	public void include(ConfigurationMetadataRepository repository) {
		for (ConfigurationMetadataGroup group : repository.getAllGroups().values()) {
			ConfigurationMetadataGroup existingGroup = this.allGroups.get(group.getId());
			if (existingGroup == null)  {
				this.allGroups.put(group.getId(), group);
			}
			else {
				// Merge properties
				for (Map.Entry<String, ConfigurationMetadataProperty> entry : group.getProperties().entrySet()) {
					existingGroup.getProperties().putIfAbsent(entry.getKey(), entry.getValue());
				}
				// Merge sources
				for (Map.Entry<String, ConfigurationMetadataSource> entry : group.getSources().entrySet()) {
					existingGroup.getSources().putIfAbsent(entry.getKey(), entry.getValue());
				}
			}
		}

	}

	private ConfigurationMetadataGroup getGroup(ConfigurationMetadataSource source) {
		if (source == null) {
			ConfigurationMetadataGroup rootGroup = this.allGroups.get(ROOT_GROUP);
			if (rootGroup == null) {
				rootGroup = new ConfigurationMetadataGroup(ROOT_GROUP);
				this.allGroups.put(ROOT_GROUP, rootGroup);
			}
			return rootGroup;
		}
		else {
			return this.allGroups.get(source.getGroupId());
		}
	}

}

@@ -2092,24 +2092,36 @@
 null) %7B%0A%09%09%09%09
+putIfAbsent(
 group.getSou
@@ -2126,29 +2126,18 @@
 ources()
-.putIfAbsent(
+, 
 sourceTy
@@ -2423,16 +2423,28 @@
 l) %7B%0A%09%09%09
+putIfAbsent(
 source.g
@@ -2453,37 +2453,25 @@
 Properties()
-.putIfAbsent(
+,
 property.get
@@ -2493,16 +2493,28 @@
 ;%0A%09%09%7D%0A%09%09
+putIfAbsent(
 getGroup
@@ -2537,29 +2537,18 @@
 erties()
-.putIfAbsent(
+, 
 property
@@ -3098,32 +3098,44 @@
 rySet()) %7B%0A%09%09%09%09%09
+putIfAbsent(
 existingGroup.ge
@@ -3143,37 +3143,26 @@
 Properties()
-.putIfAbsent(
+, 
 entry.getKey
@@ -3313,16 +3313,28 @@
  %7B%0A%09%09%09%09%09
+putIfAbsent(
 existing
@@ -3355,21 +3355,10 @@
 es()
-.putIfAbsent(
+, 
 entr
@@ -3822,10 +3822,146 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private %3CV%3E void putIfAbsent(Map%3CString, V%3E map, String key, V value) %7B%0A%09%09if (!map.containsKey(key)) %7B%0A%09%09%09map.put(key, value);%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

Fix usage of putIfAbsent