Skip to content

Commit 3bc3bb0

Browse files
committed
feat: rename mandatory to required
1 parent d70c4ae commit 3bc3bb0

File tree

9 files changed

+44
-47
lines changed

9 files changed

+44
-47
lines changed

spring-shell-core/src/main/java/org/springframework/shell/component/StringInput.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626
import org.jline.utils.AttributedString;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
29-
3029
import org.springframework.shell.component.StringInput.StringInputContext;
3130
import org.springframework.shell.component.context.ComponentContext;
3231
import org.springframework.shell.component.support.AbstractTextComponent;
33-
import org.springframework.shell.component.support.AbstractTextComponent.TextComponentContext;
3432
import org.springframework.util.StringUtils;
3533

3634
/**
@@ -44,7 +42,7 @@ public class StringInput extends AbstractTextComponent<String, StringInputContex
4442
private final String defaultValue;
4543
private StringInputContext currentContext;
4644
private Character maskCharacter;
47-
private boolean mandatory;
45+
private boolean required;
4846

4947
public StringInput(Terminal terminal) {
5048
this(terminal, null, null, null, false);
@@ -60,12 +58,12 @@ public StringInput(Terminal terminal, String name, String defaultValue,
6058
}
6159

6260
public StringInput(Terminal terminal, String name, String defaultValue,
63-
Function<StringInputContext, List<AttributedString>> renderer, boolean mandatory) {
61+
Function<StringInputContext, List<AttributedString>> renderer, boolean required) {
6462
super(terminal, name, null);
6563
setRenderer(renderer != null ? renderer : new DefaultRenderer());
6664
setTemplateLocation("classpath:org/springframework/shell/component/string-input-default.stg");
6765
this.defaultValue = defaultValue;
68-
this.mandatory = mandatory;
66+
this.required = required;
6967
}
7068

7169
/**
@@ -78,20 +76,20 @@ public void setMaskCharacter(Character maskCharacter) {
7876
}
7977

8078
/**
81-
* Sets a mandatory flag to check that the result is not empty
79+
* Sets a required flag to check that the result is not empty
8280
*
83-
* @param mandatory if input is required
81+
* @param required if input is required
8482
*/
85-
public void setMandatory(boolean mandatory) {
86-
this.mandatory = mandatory;
83+
public void setRequired(boolean required) {
84+
this.required = required;
8785
}
8886

8987
@Override
9088
public StringInputContext getThisContext(ComponentContext<?> context) {
9189
if (context != null && currentContext == context) {
9290
return currentContext;
9391
}
94-
currentContext = StringInputContext.of(defaultValue, maskCharacter, mandatory);
92+
currentContext = StringInputContext.of(defaultValue, maskCharacter, required);
9593
currentContext.setName(getName());
9694
if (context != null) {
9795
context.stream().forEach(e -> {
@@ -135,7 +133,7 @@ protected boolean read(BindingReader bindingReader, KeyMap<String> keyMap, Strin
135133
}
136134
else if (context.getDefaultValue() != null) {
137135
context.setResultValue(context.getDefaultValue());
138-
} else if (mandatory) {
136+
} else if (required) {
139137
context.setMessage("This field is mandatory", TextComponentContext.MessageLevel.ERROR);
140138
break;
141139
}
@@ -200,16 +198,16 @@ public interface StringInputContext extends TextComponentContext<String, StringI
200198
/**
201199
* Sets flag for mandatory input.
202200
*
203-
* @param mandatory true if input is mandatory
201+
* @param required true if input is required
204202
*/
205-
void setMandatory(boolean mandatory);
203+
void setRequired(boolean required);
206204

207205
/**
208206
* Returns flag if input is required.
209207
*
210208
* @return true if input is required, false otherwise
211209
*/
212-
boolean isMandatory();
210+
boolean isRequired();
213211

214212
/**
215213
* Gets an empty {@link StringInputContext}.
@@ -234,8 +232,8 @@ public static StringInputContext of(String defaultValue, Character maskCharacter
234232
*
235233
* @return path input context
236234
*/
237-
public static StringInputContext of(String defaultValue, Character maskCharacter, boolean mandatory) {
238-
return new DefaultStringInputContext(defaultValue, maskCharacter, mandatory);
235+
public static StringInputContext of(String defaultValue, Character maskCharacter, boolean required) {
236+
return new DefaultStringInputContext(defaultValue, maskCharacter, required);
239237
}
240238
}
241239

@@ -244,12 +242,12 @@ private static class DefaultStringInputContext extends BaseTextComponentContext<
244242

245243
private String defaultValue;
246244
private Character maskCharacter;
247-
private boolean mandatory;
245+
private boolean required;
248246

249-
public DefaultStringInputContext(String defaultValue, Character maskCharacter, boolean mandatory) {
247+
public DefaultStringInputContext(String defaultValue, Character maskCharacter, boolean required) {
250248
this.defaultValue = defaultValue;
251249
this.maskCharacter = maskCharacter;
252-
this.mandatory = mandatory;
250+
this.required = required;
253251
}
254252

255253
@Override
@@ -268,8 +266,8 @@ public void setMaskCharacter(Character maskCharacter) {
268266
}
269267

270268
@Override
271-
public void setMandatory(boolean mandatory) {
272-
this.mandatory = mandatory;
269+
public void setRequired(boolean required) {
270+
this.required = required;
273271
}
274272

275273
@Override
@@ -293,8 +291,8 @@ public Character getMaskCharacter() {
293291
}
294292

295293
@Override
296-
public boolean isMandatory() {
297-
return mandatory;
294+
public boolean isRequired() {
295+
return required;
298296
}
299297

300298
@Override
@@ -305,7 +303,7 @@ public Map<String, Object> toTemplateModel() {
305303
attributes.put("maskedResultValue", getMaskedResultValue());
306304
attributes.put("maskCharacter", getMaskCharacter());
307305
attributes.put("hasMaskCharacter", hasMaskCharacter());
308-
attributes.put("mandatory", isMandatory());
306+
attributes.put("required", isRequired());
309307
Map<String, Object> model = new HashMap<>();
310308
model.put("model", attributes);
311309
return model;

spring-shell-core/src/main/java/org/springframework/shell/component/flow/BaseStringInput.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.Function;
2222

2323
import org.jline.utils.AttributedString;
24-
2524
import org.springframework.shell.component.StringInput.StringInputContext;
2625
import org.springframework.shell.component.flow.ComponentFlow.BaseBuilder;
2726
import org.springframework.shell.component.flow.ComponentFlow.Builder;
@@ -38,7 +37,7 @@ public abstract class BaseStringInput extends BaseInput<StringInputSpec> impleme
3837
private ResultMode resultMode;
3938
private String defaultValue;
4039
private Character maskCharacter;
41-
private boolean mandatory = false;
40+
private boolean required = false;
4241
private Function<StringInputContext, List<AttributedString>> renderer;
4342
private List<Consumer<StringInputContext>> preHandlers = new ArrayList<>();
4443
private List<Consumer<StringInputContext>> postHandlers = new ArrayList<>();
@@ -81,8 +80,8 @@ public StringInputSpec maskCharacter(Character maskCharacter) {
8180
}
8281

8382
@Override
84-
public StringInputSpec mandatory() {
85-
this.mandatory = true;
83+
public StringInputSpec required() {
84+
this.required = true;
8685
return this;
8786
}
8887

@@ -153,8 +152,8 @@ public Character getMaskCharacter() {
153152
return maskCharacter;
154153
}
155154

156-
public boolean isMandatory() {
157-
return mandatory;
155+
public boolean isRequired() {
156+
return required;
158157
}
159158

160159
public Function<StringInputContext, List<AttributedString>> getRenderer() {

spring-shell-core/src/main/java/org/springframework/shell/component/flow/ComponentFlow.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@
3131
import org.jline.terminal.Terminal;
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
34-
3534
import org.springframework.core.OrderComparator;
3635
import org.springframework.core.Ordered;
3736
import org.springframework.core.io.ResourceLoader;
3837
import org.springframework.shell.component.ConfirmationInput;
38+
import org.springframework.shell.component.ConfirmationInput.ConfirmationInputContext;
3939
import org.springframework.shell.component.MultiItemSelector;
4040
import org.springframework.shell.component.MultiItemSelector.MultiItemSelectorContext;
4141
import org.springframework.shell.component.PathInput;
4242
import org.springframework.shell.component.PathInput.PathInputContext;
4343
import org.springframework.shell.component.SingleItemSelector;
4444
import org.springframework.shell.component.SingleItemSelector.SingleItemSelectorContext;
4545
import org.springframework.shell.component.StringInput;
46-
import org.springframework.shell.component.ConfirmationInput.ConfirmationInputContext;
4746
import org.springframework.shell.component.StringInput.StringInputContext;
4847
import org.springframework.shell.component.context.ComponentContext;
4948
import org.springframework.shell.component.support.SelectorItem;
@@ -445,7 +444,7 @@ else if (n.isPresent()) {
445444

446445
private Stream<OrderedInputOperation> stringInputsStream() {
447446
return stringInputs.stream().map(input -> {
448-
StringInput selector = new StringInput(terminal, input.getName(), input.getDefaultValue(), null, input.isMandatory());
447+
StringInput selector = new StringInput(terminal, input.getName(), input.getDefaultValue(), null, input.isRequired());
449448
Function<ComponentContext<?>, ComponentContext<?>> operation = (context) -> {
450449
if (input.getResultMode() == ResultMode.ACCEPT && input.isStoreResult()
451450
&& StringUtils.hasText(input.getResultValue())) {
@@ -465,7 +464,7 @@ private Stream<OrderedInputOperation> stringInputsStream() {
465464
if (input.getResultMode() == ResultMode.VERIFY && StringUtils.hasText(input.getResultValue())) {
466465
selector.addPreRunHandler(c -> {
467466
c.setDefaultValue(input.getResultValue());
468-
c.setMandatory(input.isMandatory());
467+
c.setRequired(input.isRequired());
469468
});
470469
}
471470
selector.addPostRunHandler(c -> {

spring-shell-core/src/main/java/org/springframework/shell/component/flow/StringInputSpec.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.function.Function;
2121

2222
import org.jline.utils.AttributedString;
23-
2423
import org.springframework.shell.component.StringInput.StringInputContext;
2524
import org.springframework.shell.component.context.ComponentContext;
2625
import org.springframework.shell.component.flow.ComponentFlow.Builder;
@@ -73,11 +72,11 @@ public interface StringInputSpec extends BaseInputSpec<StringInputSpec> {
7372
StringInputSpec maskCharacter(Character maskCharacter);
7473

7574
/**
76-
* Sets input to mandatory
75+
* Sets input to required
7776
*
7877
* @return a builder
7978
*/
80-
StringInputSpec mandatory();
79+
StringInputSpec required();
8180

8281
/**
8382
* Sets a renderer function.

spring-shell-core/src/main/resources/org/springframework/shell/component/string-input-default.stg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ info(model) ::= <%
1515
<if(model.maskedInput)>
1616
<model.maskedInput>
1717
<else>
18-
<if(model.mandatory)>
19-
<("[Mandatory]"); format="style-value">
18+
<if(model.required)>
19+
<("[Required]"); format="style-value">
2020
<endif>
2121
<if(model.defaultValue)>
2222
<("[Default "); format="style-value"><model.defaultValue; format="style-value"><("]"); format="style-value">
@@ -26,8 +26,8 @@ info(model) ::= <%
2626
<if(model.input)>
2727
<model.input>
2828
<else>
29-
<if(model.mandatory)>
30-
<("[Mandatory]"); format="style-value">
29+
<if(model.required)>
30+
<("[Required]"); format="style-value">
3131
<endif>
3232
<if(model.defaultValue)>
3333
<("[Default "); format="style-value"><model.defaultValue; format="style-value"><("]"); format="style-value">

spring-shell-core/src/test/java/org/springframework/shell/component/StringInputTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.shell.component;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
19+
1820
import java.io.ByteArrayInputStream;
1921
import java.io.ByteArrayOutputStream;
2022
import java.nio.charset.StandardCharsets;
@@ -27,7 +29,6 @@
2729
import org.junit.jupiter.api.AfterEach;
2830
import org.junit.jupiter.api.BeforeEach;
2931
import org.junit.jupiter.api.Test;
30-
3132
import org.springframework.core.io.DefaultResourceLoader;
3233
import org.springframework.shell.component.StringInput.StringInputContext;
3334
import org.springframework.shell.component.context.ComponentContext;
@@ -165,7 +166,7 @@ void testResultMandatoryInput() {
165166
StringInput component1 = new StringInput(getTerminal());
166167
component1.setResourceLoader(new DefaultResourceLoader());
167168
component1.setTemplateExecutor(getTemplateExecutor());
168-
component1.setMandatory(true);
169+
component1.setRequired(true);
169170

170171
service.execute(() -> {
171172
StringInputContext run1Context = component1.run(empty);

spring-shell-core/src/test/java/org/springframework/shell/component/flow/ComponentFlowTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.shell.component.flow;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
19+
1820
import java.nio.file.Path;
1921
import java.time.Duration;
2022
import java.util.HashMap;
@@ -56,7 +58,7 @@ void testSimpleFlow() {
5658
.and()
5759
.withStringInput("field3")
5860
.name("Field3")
59-
.mandatory()
61+
.required()
6062
.and()
6163
.withPathInput("path1")
6264
.name("Path1")

spring-shell-docs/modules/ROOT/pages/components/ui/stringinput.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The context object is `StringInputContext`. The following table lists its contex
4141
|`hasMaskCharacter`
4242
|`true` if a mask character is set. Otherwise, false.
4343

44-
|`mandatory`
44+
|`required`
4545
|`true` if the input is required. Otherwise, false.
4646

4747
|`model`

spring-shell-samples/spring-shell-sample-commands/src/main/java/org/springframework/shell/samples/standard/ComponentFlowCommands.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.stream.IntStream;
2525

2626
import org.jline.terminal.impl.DumbTerminal;
27-
2827
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.context.annotation.Bean;
3029
import org.springframework.shell.command.CommandExecution.CommandParserExceptionsException;
@@ -64,7 +63,7 @@ public void showcase1() {
6463
.and()
6564
.withStringInput("field3")
6665
.name("Field3")
67-
.mandatory()
66+
.required()
6867
.and()
6968
.withConfirmationInput("confirmation1")
7069
.name("Confirmation1")

0 commit comments

Comments
 (0)