tarilabs commented on code in PR #5645: URL: https://github.com/apache/incubator-kie-drools/pull/5645#discussion_r1450695756
########## 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: > TBH this is a tricky part I did not understood: > integer (0 scale number) means it's not a floating point, just a int ("zero scale" here I believe they refer as: no decimals) > non-zero integer the set of integers, except `0` > in the range [-L..L], where L is the length of the list it's an integer, but cannot be `0`, in the range from -size to size included. For a list of 2 elements such as `[1,2]`, L = 2, so `position` can be `[-2..2]`, For a list of 3 elements such as `[1,2,3]`, L = 3, so `position` can be `[-3..3]`, etc. but in any case `L is never 0`. > the position in the example seems to be 1 based Yes as specified, and consistent with: > 10.3.2.5 Lists and filters > ... The nth element from the beginning can be accessed using L[n], and the nth element from the end can be accessed using L[-n]. taken from the spec, semantic of List > what does negative position means ? i.e. in the following example > list replace( [2, 4, 7, 8], -3, 6) = ... what should be the expected behavior ? `list replace( [2, 4, 7, 8], -3, 6) = [2, 6, 7, 8]` -- 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]
