Add a typed config getter for integers confined to a range

Use Integer#MIN_VALUE to denote unset option.

Change-Id: I4d65f2434013111f25520c0ed2b9a9dc8123c6cf
This commit is contained in:
Matthias Sohn 2022-02-22 01:20:07 +01:00
parent 9284ed5db7
commit 9244c07d73
5 changed files with 71 additions and 0 deletions

View File

@ -9,6 +9,20 @@
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/jgit/lib/TypedConfigGetter.java" type="org.eclipse.jgit.lib.TypedConfigGetter">
<filter id="403767336">
<message_arguments>
<message_argument value="org.eclipse.jgit.lib.TypedConfigGetter"/>
<message_argument value="UNSET_INT"/>
</message_arguments>
</filter>
<filter id="403804204">
<message_arguments>
<message_argument value="org.eclipse.jgit.lib.TypedConfigGetter"/>
<message_argument value="getIntInRange(Config, String, String, String, int, int, int)"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/jgit/transport/BasePackPushConnection.java" type="org.eclipse.jgit.transport.BasePackPushConnection">
<filter id="338792546">
<message_arguments>

View File

@ -354,6 +354,8 @@ initFailedNonBareRepoSameDirs=When initializing a non-bare repo with directory {
inMemoryBufferLimitExceeded=In-memory buffer limit exceeded
inputDidntMatchLength=Input did not match supplied length. {0} bytes are missing.
inputStreamMustSupportMark=InputStream must support mark()
integerValueNotInRange=Integer value {0}.{1} = {2} not in range {3}..{4}
integerValueNotInRangeSubSection=Integer value {0}.{1}.{2} = {3} not in range {4}..{5}
integerValueOutOfRange=Integer value {0}.{1} out of range
internalRevisionError=internal revision error
internalServerError=internal server error

View File

@ -382,6 +382,8 @@ public static JGitText get() {
/***/ public String inMemoryBufferLimitExceeded;
/***/ public String inputDidntMatchLength;
/***/ public String inputStreamMustSupportMark;
/***/ public String integerValueNotInRange;
/***/ public String integerValueNotInRangeSubSection;
/***/ public String integerValueOutOfRange;
/***/ public String internalRevisionError;
/***/ public String internalServerError;

View File

@ -118,6 +118,26 @@ public int getInt(Config config, String section, String subsection,
.format(JGitText.get().integerValueOutOfRange, section, name));
}
/** {@inheritDoc} */
@Override
public int getIntInRange(Config config, String section, String subsection,
String name, int minValue, int maxValue, int defaultValue) {
int val = getInt(config, section, subsection, name, defaultValue);
if ((val >= minValue && val <= maxValue) || val == UNSET_INT) {
return val;
}
if (subsection == null) {
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().integerValueNotInRange, section, name,
Integer.valueOf(val), Integer.valueOf(minValue),
Integer.valueOf(maxValue)));
}
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().integerValueNotInRangeSubSection, section,
subsection, name, Integer.valueOf(val),
Integer.valueOf(minValue), Integer.valueOf(maxValue)));
}
/** {@inheritDoc} */
@Override
public long getLong(Config config, String section, String subsection,

View File

@ -28,6 +28,13 @@
*/
public interface TypedConfigGetter {
/**
* Use {@code Integer#MIN_VALUE} as unset int value
*
* @since 6.1
*/
public static final int UNSET_INT = Integer.MIN_VALUE;
/**
* Get a boolean value from a git {@link Config}.
*
@ -86,6 +93,32 @@ <T extends Enum<?>> T getEnum(Config config, T[] all, String section,
int getInt(Config config, String section, String subsection, String name,
int defaultValue);
/**
* Obtain an integer value from a git {@link Config} which must be in given
* range.
*
* @param config
* to get the value from
* @param section
* section the key is grouped within.
* @param subsection
* subsection name, such a remote or branch name.
* @param name
* name of the key to get.
* @param minValue
* minimal value
* @param maxValue
* maximum value
* @param defaultValue
* default value to return if no value was present. Use
* {@code #UNSET_INT} to set the default to unset.
* @return an integer value from the configuration, or defaultValue.
* {@code #UNSET_INT} if unset.
* @since 6.1
*/
int getIntInRange(Config config, String section, String subsection,
String name, int minValue, int maxValue, int defaultValue);
/**
* Obtain a long value from a git {@link Config}.
*