Skip to content

Commit eed1ce5

Browse files
committed
Implemented IntFromBoolean UDF
1 parent 41c7a59 commit eed1ce5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 Confluent Inc.
3+
*
4+
* Licensed under the Confluent Community License; you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.confluent.io/confluent-community-license
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
*/
14+
15+
package io.confluent.ksql.function.udf.conversions;
16+
17+
import io.confluent.ksql.function.FunctionCategory;
18+
import io.confluent.ksql.function.udf.Udf;
19+
import io.confluent.ksql.function.udf.UdfDescription;
20+
import io.confluent.ksql.function.udf.UdfParameter;
21+
22+
@UdfDescription(
23+
name = "int_from_boolean",
24+
category = FunctionCategory.CONVERSIONS,
25+
description = "Converts a Boolean value to an Integer value"
26+
)
27+
public class IntFromBoolean {
28+
29+
@Udf(description = "Converts a Boolean value to an Integer value")
30+
public Integer intFromBoolean(
31+
@UdfParameter(description = "The Boolean value to convert.")
32+
final Boolean bool
33+
) {
34+
if (bool == null) {
35+
return null;
36+
}
37+
return Boolean.TRUE.equals(bool) ? 1 : 0;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.confluent.ksql.function.udf.conversions;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.is;
7+
import static org.hamcrest.Matchers.nullValue;
8+
9+
public class IntFromBooleanTest {
10+
private final IntFromBoolean udf = new IntFromBoolean();
11+
12+
@Test
13+
public void shouldReturnNullOnNullBoolean() {
14+
assertThat(udf.intFromBoolean(null), is(nullValue()));
15+
}
16+
17+
@Test
18+
public void shouldConvertTrueToInteger() {
19+
assertThat(udf.intFromBoolean(Boolean.TRUE), is(1));
20+
}
21+
22+
@Test
23+
public void shouldConvertFalseToInteger() {
24+
assertThat(udf.intFromBoolean(Boolean.FALSE), is(0));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)