
infra.beans.factory.support.ManagedProperties Maven / Gradle / Ivy
/*
* Copyright 2017 - 2024 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [https://www.gnu.org/licenses/]
*/
package infra.beans.factory.support;
import java.util.Properties;
import infra.beans.BeanMetadataElement;
import infra.beans.Mergeable;
import infra.lang.Nullable;
/**
* Tag class which represents a Framework-managed {@link Properties} instance
* that supports merging of parent/child definitions.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @author Harry Yang
* @since 4.0
*/
@SuppressWarnings("serial")
public class ManagedProperties extends Properties implements Mergeable, BeanMetadataElement {
@Nullable
private Object source;
private boolean mergeEnabled;
/**
* Set the configuration source {@code Object} for this metadata element.
* The exact type of the object will depend on the configuration mechanism used.
*/
public void setSource(@Nullable Object source) {
this.source = source;
}
@Override
@Nullable
public Object getSource() {
return this.source;
}
/**
* Set whether merging should be enabled for this collection,
* in case of a 'parent' collection value being present.
*/
public void setMergeEnabled(boolean mergeEnabled) {
this.mergeEnabled = mergeEnabled;
}
@Override
public boolean isMergeEnabled() {
return this.mergeEnabled;
}
@Override
public Object merge(@Nullable Object parent) {
if (!this.mergeEnabled) {
throw new IllegalStateException("Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
}
if (parent == null) {
return this;
}
if (!(parent instanceof Properties)) {
throw new IllegalArgumentException("Cannot merge with object of type [%s]".formatted(parent.getClass()));
}
Properties merged = new ManagedProperties();
merged.putAll((Properties) parent);
merged.putAll(this);
return merged;
}
}