Skip to content

Commit eded713

Browse files
committed
Change Booleans.parseBooleanLenient to wrap Boolean.parseBoolean
1 parent 834b2d0 commit eded713

File tree

2 files changed

+6
-138
lines changed

2 files changed

+6
-138
lines changed

libs/core/src/main/java/org/elasticsearch/core/Booleans.java

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,16 @@ public static Boolean parseBoolean(String value, Boolean defaultValue) {
9797
}
9898

9999
/**
100-
* Returns {@code false} if text is in "false", "0", "off", "no"; else, {@code true}.
100+
* Wrapper around Boolean.parseBoolean for lenient parsing of booleans.
101101
*
102-
* @deprecated Only kept to provide automatic upgrades for pre 6.0 indices. Use {@link #parseBoolean(String, Boolean)} instead.
102+
* Note: Lenient parsing is highly discouraged and should only be used if absolutely necessary.
103103
*/
104-
@Deprecated
105-
public static Boolean parseBooleanLenient(String value, Boolean defaultValue) {
106-
if (value == null) { // only for the null case we do that here!
107-
return defaultValue;
108-
}
109-
return parseBooleanLenient(value, false);
110-
}
111-
112-
/**
113-
* Returns {@code false} if text is in "false", "0", "off", "no"; else, {@code true}.
114-
*
115-
* @deprecated Only kept to provide automatic upgrades for pre 6.0 indices. Use {@link #parseBoolean(String, boolean)} instead.
116-
*/
117-
@Deprecated
104+
@SuppressForbidden(reason = "allow lenient parsing of booleans")
118105
public static boolean parseBooleanLenient(String value, boolean defaultValue) {
119106
if (value == null) {
120107
return defaultValue;
121108
}
122-
return switch (value) {
123-
case "false", "0", "off", "no" -> false;
124-
default -> true;
125-
};
109+
return Boolean.parseBoolean(value);
126110
}
127111

128112
/**
@@ -138,71 +122,4 @@ public static boolean isFalse(String value) {
138122
public static boolean isTrue(String value) {
139123
return "true".equals(value);
140124
}
141-
142-
/**
143-
* Returns {@code false} if text is in "false", "0", "off", "no"; else, {@code true}.
144-
*
145-
* @deprecated Only kept to provide automatic upgrades for pre 6.0 indices. Use {@link #parseBoolean(char[], int, int, boolean)} instead
146-
*/
147-
@Deprecated
148-
public static boolean parseBooleanLenient(char[] text, int offset, int length, boolean defaultValue) {
149-
if (text == null || length == 0) {
150-
return defaultValue;
151-
}
152-
if (length == 1) {
153-
return text[offset] != '0';
154-
}
155-
if (length == 2) {
156-
return (text[offset] == 'n' && text[offset + 1] == 'o') == false;
157-
}
158-
if (length == 3) {
159-
return (text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f') == false;
160-
}
161-
if (length == 5) {
162-
return (text[offset] == 'f'
163-
&& text[offset + 1] == 'a'
164-
&& text[offset + 2] == 'l'
165-
&& text[offset + 3] == 's'
166-
&& text[offset + 4] == 'e') == false;
167-
}
168-
return true;
169-
}
170-
171-
/**
172-
* returns true if the a sequence of chars is one of "true","false","on","off","yes","no","0","1"
173-
*
174-
* @param text sequence to check
175-
* @param offset offset to start
176-
* @param length length to check
177-
*
178-
* @deprecated Only kept to provide automatic upgrades for pre 6.0 indices. Use {@link #isBoolean(char[], int, int)} instead.
179-
*/
180-
@Deprecated
181-
public static boolean isBooleanLenient(char[] text, int offset, int length) {
182-
if (text == null || length == 0) {
183-
return false;
184-
}
185-
if (length == 1) {
186-
return text[offset] == '0' || text[offset] == '1';
187-
}
188-
if (length == 2) {
189-
return (text[offset] == 'n' && text[offset + 1] == 'o') || (text[offset] == 'o' && text[offset + 1] == 'n');
190-
}
191-
if (length == 3) {
192-
return (text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f')
193-
|| (text[offset] == 'y' && text[offset + 1] == 'e' && text[offset + 2] == 's');
194-
}
195-
if (length == 4) {
196-
return (text[offset] == 't' && text[offset + 1] == 'r' && text[offset + 2] == 'u' && text[offset + 3] == 'e');
197-
}
198-
if (length == 5) {
199-
return (text[offset] == 'f'
200-
&& text[offset + 1] == 'a'
201-
&& text[offset + 2] == 'l'
202-
&& text[offset + 3] == 's'
203-
&& text[offset + 4] == 'e');
204-
}
205-
return false;
206-
}
207-
208125
}

server/src/test/java/org/elasticsearch/common/BooleansTests.java

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import org.elasticsearch.core.Booleans;
1313
import org.elasticsearch.test.ESTestCase;
1414

15-
import java.util.Locale;
16-
1715
import static org.hamcrest.Matchers.is;
18-
import static org.hamcrest.Matchers.nullValue;
1916

2017
public class BooleansTests extends ESTestCase {
2118
private static final String[] NON_BOOLEANS = new String[] {
@@ -87,56 +84,10 @@ public void testParseNonBoolean() {
8784
}
8885
}
8986

90-
public void testIsBooleanLenient() {
91-
String[] booleans = new String[] { "true", "false", "on", "off", "yes", "no", "0", "1" };
92-
String[] notBooleans = new String[] { "11", "00", "sdfsdfsf", "F", "T" };
93-
assertThat(Booleans.isBooleanLenient(null, 0, 1), is(false));
94-
95-
for (String b : booleans) {
96-
String t = "prefix" + b + "suffix";
97-
assertTrue(
98-
"failed to recognize [" + b + "] as boolean",
99-
Booleans.isBooleanLenient(t.toCharArray(), "prefix".length(), b.length())
100-
);
101-
}
102-
103-
for (String nb : notBooleans) {
104-
String t = "prefix" + nb + "suffix";
105-
assertFalse("recognized [" + nb + "] as boolean", Booleans.isBooleanLenient(t.toCharArray(), "prefix".length(), nb.length()));
106-
}
107-
}
108-
10987
public void testParseBooleanLenient() {
110-
assertThat(Booleans.parseBooleanLenient(randomFrom("true", "on", "yes", "1"), randomBoolean()), is(true));
111-
assertThat(Booleans.parseBooleanLenient(randomFrom("false", "off", "no", "0"), randomBoolean()), is(false));
112-
assertThat(Booleans.parseBooleanLenient(randomFrom("true", "on", "yes").toUpperCase(Locale.ROOT), randomBoolean()), is(true));
88+
assertThat(Booleans.parseBooleanLenient(randomFrom("true", "TRUE", "True"), randomBoolean()), is(true));
89+
assertThat(Booleans.parseBooleanLenient(randomFrom("false", "FALSE", "anything"), randomBoolean()), is(false));
11390
assertThat(Booleans.parseBooleanLenient(null, false), is(false));
11491
assertThat(Booleans.parseBooleanLenient(null, true), is(true));
115-
116-
assertThat(
117-
Booleans.parseBooleanLenient(randomFrom("true", "on", "yes", "1"), randomFrom(Boolean.TRUE, Boolean.FALSE, null)),
118-
is(true)
119-
);
120-
assertThat(
121-
Booleans.parseBooleanLenient(randomFrom("false", "off", "no", "0"), randomFrom(Boolean.TRUE, Boolean.FALSE, null)),
122-
is(false)
123-
);
124-
assertThat(
125-
Booleans.parseBooleanLenient(
126-
randomFrom("true", "on", "yes").toUpperCase(Locale.ROOT),
127-
randomFrom(Boolean.TRUE, Boolean.FALSE, null)
128-
),
129-
is(true)
130-
);
131-
assertThat(Booleans.parseBooleanLenient(null, Boolean.FALSE), is(false));
132-
assertThat(Booleans.parseBooleanLenient(null, Boolean.TRUE), is(true));
133-
assertThat(Booleans.parseBooleanLenient(null, null), nullValue());
134-
135-
char[] chars = randomFrom("true", "on", "yes", "1").toCharArray();
136-
assertThat(Booleans.parseBooleanLenient(chars, 0, chars.length, randomBoolean()), is(true));
137-
chars = randomFrom("false", "off", "no", "0").toCharArray();
138-
assertThat(Booleans.parseBooleanLenient(chars, 0, chars.length, randomBoolean()), is(false));
139-
chars = randomFrom("true", "on", "yes").toUpperCase(Locale.ROOT).toCharArray();
140-
assertThat(Booleans.parseBooleanLenient(chars, 0, chars.length, randomBoolean()), is(true));
14192
}
14293
}

0 commit comments

Comments
 (0)