tarilabs commented on code in PR #5645: URL: https://github.com/apache/incubator-kie-drools/pull/5645#discussion_r1450163732
########## kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/ListReplaceFunction.java: ########## @@ -0,0 +1,83 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.kie.dmn.feel.runtime.functions; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity; +import org.kie.dmn.feel.runtime.events.InvalidParametersEvent; +import org.kie.dmn.feel.util.EvalHelper; + +public class ListReplaceFunction + extends BaseFEELFunction { + + public static final ListReplaceFunction INSTANCE = new ListReplaceFunction(); + + private ListReplaceFunction() { + super("list replace"); + } + + public FEELFnResult<List> invoke(@ParameterName("list") List list, @ParameterName("position") BigDecimal position, + @ParameterName("newItem") Object newItem) { + if (list == null) { + return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null")); + } + if (position == null) { + return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "position", "cannot be null")); + } + int intPosition = position.intValue(); + if (intPosition < 1 || intPosition > list.size()) { Review Comment: iirc in FEEL you can access by negative index (a-la python so to speak). I don't have access to the spec, maybe you want to check if you need to support negative indexing here. ########## kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELFunctionsTest.java: ########## @@ -237,6 +237,12 @@ public static Collection<Object[]> data() { { "{a: -2, r: -sum( 1, -abs(a), 3 )}.r", BigDecimal.valueOf( -2 ) , null}, { "if list contains ([2.2, 3.0, 4.0], 3) then \"OK\" else \"NOT_OK\"", "OK" , null}, { "if list contains ([2.2, 3, 4], 3.000) then \"OK\" else \"NOT_OK\"", "OK" , null}, + {"list replace ( null, 3, 6)", null , FEELEvent.Severity.ERROR}, + {"list replace ( [2, 4, 7, 8], null, 6)", null , FEELEvent.Severity.ERROR}, + {"list replace ( [2, 4, 7, 8], 3, 6)", Arrays.asList(BigDecimal.valueOf(2), BigDecimal.valueOf(4), BigDecimal.valueOf(6), BigDecimal.valueOf(8)), null}, + {"list replace ( [2, 4, 7, 8], function(item, newItem) item + newItem, 6)", null , FEELEvent.Severity.ERROR}, + {"list replace ( [\"El-1\", \"El-2\", \"El-3\", \"El-4\"], function(item, newItem) item = \"El-2\", null)", Arrays.asList("El-1", null, "El-3", "El-4"), null}, + {"list replace ( [2, 4, 7, 8], function(item, newItem) item < newItem, 5)", Arrays.asList(BigDecimal.valueOf(5), BigDecimal.valueOf(5), BigDecimal.valueOf(7), BigDecimal.valueOf(8)), null} Review Comment: so my understanding the semantic of the `list replace` to take: - as first argument, the list subject of replacements - as second argument, a FEEL:number for the position (or a predicate function) - as third argument, the to-replace value so either replace-at-position or replace-when-predicate semantic in case the 2nd arg is a predicate, is a predicate in 2 arguments, the "current" item being processed and the third to-replace value. There is no doubt for the second argument (a FEEL:number for the position or a predicate function) in which variant of the built-in function you are -- ie it's _not_ like [Collections.replaceAll](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#replaceAll-java.util.List-T-T-) this is my understanding 👍 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
