/*
 * Copyright (C) 2014 The Project Lombok Authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package lombok.core.configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lombok.core.configuration.ConfigurationSource.ListModification;
import lombok.core.configuration.ConfigurationSource.Result;

public class BubblingConfigurationResolver implements ConfigurationResolver {
	
	private final Iterable<ConfigurationSource> sources;
	
	public BubblingConfigurationResolver(Iterable<ConfigurationSource> sources) {
		this.sources = sources;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T resolve(ConfigurationKey<T> key) {
		boolean isList = key.getType().isList();
		List<ListModification> listModifications = null;
		for (ConfigurationSource source : sources) {
			Result result = source.resolve(key);
			if (result == null) continue;
			if (isList) {
				if (listModifications == null) {
					listModifications = new ArrayList<ListModification>((List<ListModification>)result.getValue());
				} else {
					listModifications.addAll(0, (List<ListModification>)result.getValue());
				}
			}
			if (result.isAuthoritative()) {
				if (isList) {
					break;
				}
				return (T) result.getValue();
			}
		}
		if (!isList) {
			return null;
		}
		if (listModifications == null) {
			return (T) Collections.emptyList();
		}
		List<Object> listValues = new ArrayList<Object>();
		for (ListModification modification : listModifications) {
			listValues.remove(modification.getValue());
			if (modification.isAdded()) {
				listValues.add(modification.getValue());
			}
		}
		return (T) listValues;
	}
}

@@ -1763,24 +1763,29 @@
 t();%0A%09%09List%3C
+List%3C
 ListModifica
@@ -1789,16 +1789,17 @@
 ication%3E
+%3E
  listMod
@@ -1804,24 +1804,28 @@
 odifications
+List
  = null;%0A%09%09f
@@ -1975,32 +1975,36 @@
 istModifications
+List
  == null) %7B%0A%09%09%09%09
@@ -2021,16 +2021,20 @@
 ications
+List
  = new A
@@ -2046,27 +2046,8 @@
 ist%3C
-ListModification%3E((
 List
@@ -2068,47 +2068,23 @@
 ion%3E
-)result.getValue
+%3E
 ()
-)
 ;%0A%09%09%09%09%7D
- else %7B
 %0A%09%09%09%09
-%09
 list
@@ -2100,19 +2100,17 @@
 ions
-.addAll(0, 
+List.add(
 (Lis
@@ -2149,22 +2149,16 @@
 lue());%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09
@@ -2328,16 +2328,20 @@
 ications
+List
  == null
@@ -2440,16 +2440,138 @@
 ect%3E();%0A
+%09%09Collections.reverse(listModificationsList);%0A%09%09for (List%3CListModification%3E listModifications : listModificationsList) %7B%0A%09
 %09%09for (L
@@ -2622,16 +2622,17 @@
 ions) %7B%0A
+%09
 %09%09%09listV
@@ -2669,24 +2669,25 @@
 alue());%0A%09%09%09
+%09
 if (modifica
@@ -2700,24 +2700,25 @@
 sAdded()) %7B%0A
+%09
 %09%09%09%09listValu
@@ -2746,24 +2746,30 @@
 etValue());%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D%0A%09%09r

Copy less lists during resolving