Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
EtherConfig.java
Go to the documentation of this file.
1package dev.rafex.ether.config;
2
3/*-
4 * #%L
5 * ether-config
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.util.ArrayList;
30import java.util.LinkedHashMap;
31import java.util.List;
32import java.util.Map;
33import java.util.Objects;
34import java.util.Optional;
35
36import dev.rafex.ether.config.binding.ConfigBinder;
37import dev.rafex.ether.config.sources.ConfigSource;
38
39public final class EtherConfig {
40
41 private final List<ConfigSource> sources;
42
43 private EtherConfig(final List<ConfigSource> sources) {
44 this.sources = List.copyOf(sources);
45 }
46
47 public static EtherConfig of(final ConfigSource... sources) {
48 return of(List.of(sources));
49 }
50
51 public static EtherConfig of(final List<ConfigSource> sources) {
52 Objects.requireNonNull(sources, "sources");
53 return new EtherConfig(new ArrayList<>(sources));
54 }
55
56 public List<ConfigSource> sources() {
57 return sources;
58 }
59
60 public Optional<String> get(final String key) {
61 for (final var source : sources) {
62 final var value = source.get(key);
63 if (value.isPresent()) {
64 return value;
65 }
66 }
67 return Optional.empty();
68 }
69
70 public String require(final String key) {
71 return get(key).orElseThrow(() -> new IllegalArgumentException("Missing config key: " + key));
72 }
73
74 public Map<String, String> snapshot() {
75 final var merged = new LinkedHashMap<String, String>();
76 for (final var source : sources) {
77 for (final var entry : source.entries().entrySet()) {
78 merged.putIfAbsent(entry.getKey(), entry.getValue());
79 }
80 }
81 return Map.copyOf(merged);
82 }
83
84 public <T extends Record> T bind(final Class<T> recordType) {
85 return ConfigBinder.bind(this, recordType);
86 }
87
88 public <T extends Record> T bind(final String prefix, final Class<T> recordType) {
89 return ConfigBinder.bind(this, prefix, recordType);
90 }
91
92 public <T extends Record> T bindValidated(final Class<T> recordType) {
93 return ConfigBinder.bindValidated(this, recordType);
94 }
95
96 public <T extends Record> T bindValidated(final String prefix, final Class<T> recordType) {
97 return ConfigBinder.bindValidated(this, prefix, recordType);
98 }
99}
String require(final String key)
List< ConfigSource > sources()
static EtherConfig of(final List< ConfigSource > sources)
Map< String, String > snapshot()
static EtherConfig of(final ConfigSource... sources)
static< T extends Record > T bind(final EtherConfig config, final Class< T > recordType)