Skip to content

Commit 1c442e4

Browse files
committed
IDefaultProvider.sequence(...) produces a sequence of default providers
1 parent 560871c commit 1c442e4

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

docs/index.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,8 @@ JCommander jc = JCommander.newBuilder()
869869

870870
For the most common cases, there is no need to actually implement `IDefaultProvider`, as JCommander contains the classes `PropertyFileDefaultProvider` and `EnvironmentVariableDefaultProvider` for reading defaults from a property file (`jcommander.properties` by default) or from an environment variable (`JCOMMANDER_OPTS` by default).
871871

872+
A *sequence* of multiple default providers can be used with `IDefaultProvider.sequenceOf(...)`, in which case the providers are queried in sequence, and the first non-null default value is applied.
873+
872874

873875
== Help parameter
874876

src/main/java/com/beust/jcommander/IDefaultProvider.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,26 @@ public interface IDefaultProvider {
3232
* @return the default value for this option.
3333
*/
3434
String getDefaultValueFor(String optionName);
35+
36+
/**
37+
* Returns a default provider which attempts to query a default value from a sequence
38+
* of default providers. The first produced non-null value will get finally returned.
39+
*
40+
* @param defaultProviders A sorted sequence of default providers.
41+
* @return The first non-null value provided,
42+
* or {@code null} if all providers returned {@code null}.
43+
*/
44+
static IDefaultProvider sequenceOf(final IDefaultProvider... defaultProviders) {
45+
return new IDefaultProvider() {
46+
@Override
47+
public String getDefaultValueFor(final String optionName) {
48+
for (final var defaultProvider : defaultProviders) {
49+
final var defaultValue = defaultProvider.getDefaultValueFor(optionName);
50+
if (defaultValue != null)
51+
return defaultValue;
52+
}
53+
return null;
54+
}
55+
};
56+
}
3557
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright (C) 2025 the original author or authors.
3+
* See the notice.md file distributed with this work for additional
4+
* information regarding copyright ownership.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.beust.jcommander.defaultprovider;
20+
21+
import static org.testng.Assert.*;
22+
import static org.testng.Assert.assertEquals;
23+
24+
import java.util.function.Function;
25+
26+
import org.testng.annotations.Test;
27+
28+
import com.beust.jcommander.IDefaultProvider;
29+
30+
public final class SequenceDefaultProviderTest {
31+
32+
@Test
33+
public final void shouldProvideFirstNonNullValue() {
34+
// given
35+
final IDefaultProvider defaultProvider = IDefaultProvider.sequenceOf(
36+
always(null),
37+
always("one"),
38+
always("two"));
39+
40+
// when
41+
final var defaultValue = defaultProvider.getDefaultValueFor("test");
42+
43+
// then
44+
assertEquals(defaultValue, "one");
45+
}
46+
47+
@Test
48+
public final void shouldProvideNullWhenNoProviderProducesSomeValue() {
49+
// given
50+
final IDefaultProvider defaultProvider = IDefaultProvider.sequenceOf(
51+
always(null),
52+
always(null),
53+
always(null));
54+
55+
// when
56+
final var defaultValue = defaultProvider.getDefaultValueFor("test");
57+
58+
// then
59+
assertEquals(defaultValue, null);
60+
}
61+
62+
private static final IDefaultProvider always(final String defaultValue) {
63+
return new IDefaultProvider() {
64+
@Override
65+
public String getDefaultValueFor(final String optionName) {
66+
return defaultValue;
67+
}
68+
};
69+
}
70+
71+
}

0 commit comments

Comments
 (0)