org.instancio.settings.Settings Maven / Gradle / Ivy
/*
* Copyright 2022-2024 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
*
* https://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.instancio.settings;
import org.instancio.internal.settings.InternalSettings;
import org.instancio.internal.util.Constants;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
/**
* This class provides an API for updating settings programmatically.
* An instance of this class can be created using one of the static methods
* below. Instances of this class can be shared when creating different objects.
*
*
* - {@link #create()} - returns a new instance of blank settings
* - {@link #defaults()} - returns a new instance containing default settings
*
*
* Out of the box, Instancio uses default settings as returned by {@link #defaults()}.
* Defaults can be overridden either globally using a configuration file, or per-object
* using the API, for example:
*
*
{@code
* // Create a blank instance of settings and set the overrides
* Settings settings = Settings.create()
* .set(Keys.COLLECTION_MIN_SIZE, 50)
* .set(Keys.COLLECTION_MAX_SIZE, 100);
*
* // Pass the overrides when creating an object
* Person person = Instancio.of(Person.class)
* .withSettings(settings)
* .create();
* }
*
* For information on how to override settings globally using a configuration file, please refer
* to the user guide.
*
* @see Keys
* @see SettingKey
* @since 1.0.1
*/
public interface Settings {
/**
* Creates a new instance of empty settings.
*
* @return a new instance of empty settings
*/
static Settings create() {
return InternalSettings.create();
}
/**
* Creates a new instance containing default settings.
*
* @return a new instance of settings containing the defaults
*/
static Settings defaults() {
return InternalSettings.defaults();
}
/**
* Create settings from the given map.
*
* @param map to create settings from
* @return a new instance of settings created from the given map
*/
static Settings from(@NotNull Map