All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lombok.core.configuration.SingleConfigurationSource Maven / Gradle / Ivy

Go to download

Spice up your java: Automatic Resource Management, automatic generation of getters, setters, equals, hashCode and toString, and more!

There is a newer version: 1.18.36
Show newest version
/*
 * Copyright (C) 2014-2020 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import lombok.core.configuration.ConfigurationParser.Collector;

public final class SingleConfigurationSource implements ConfigurationSource {
	private final Map, Result> values;
	private final List imports;
	
	public static ConfigurationSource parse(ConfigurationFile context, ConfigurationParser parser) {
		final Map, Result> values = new HashMap, Result>();
		final List imports = new ArrayList();
		Collector collector = new Collector() {
			@Override public void addImport(ConfigurationFile importFile, ConfigurationFile context, int lineNumber) {
				imports.add(importFile);
			}
			
			@Override public void clear(ConfigurationKey key, ConfigurationFile context, int lineNumber) {
				values.put(key, new Result(null, true));
			}
			
			@Override public void set(ConfigurationKey key, Object value, ConfigurationFile context, int lineNumber) {
				values.put(key, new Result(value, true));
			}
			
			@Override public void add(ConfigurationKey key, Object value, ConfigurationFile context, int lineNumber) {
				modifyList(key, value, true);
			}
			
			@Override public void remove(ConfigurationKey key, Object value, ConfigurationFile context, int lineNumber) {
				modifyList(key, value, false);
			}
			
			@SuppressWarnings("unchecked")
			private void modifyList(ConfigurationKey key, Object value, boolean add) {
				Result result = values.get(key);
				List list;
				if (result == null || result.getValue() == null) {
					list = new ArrayList();
					values.put(key, new Result(list, result != null));
				} else {
					list = (List) result.getValue();
				}
				list.add(new ListModification(value, add));
			}
		};
		parser.parse(context, collector);
		return new SingleConfigurationSource(values, imports);
	}
	
	private SingleConfigurationSource(Map, Result> values, List imports) {
		this.values = new HashMap, Result>();
		for (Entry, Result> entry : values.entrySet()) {
			Result result = entry.getValue();
			if (result.getValue() instanceof List) {
				this.values.put(entry.getKey(), new Result(Collections.unmodifiableList((List) result.getValue()), result.isAuthoritative()));
			} else {
				this.values.put(entry.getKey(), result);
			}
		}
		this.imports = Collections.unmodifiableList(imports);
	}
	
	@Override 
	public Result resolve(ConfigurationKey key) {
		return values.get(key);
	}
	
	@Override
	public List imports() {
		return imports;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy