From 27ec5d9f05197aedb3a7cd8d5b6e763f361685db Mon Sep 17 00:00:00 2001 From: ramoliyaYug Date: Wed, 22 Jan 2025 09:52:36 +0530 Subject: [PATCH] CATROID-1576:migrate ReplaceItemInUserListAction to Kotlin - Refactored ReplaceItemInUserListAction from Java to Kotlin. - Verified test coverage by checking existing tests to ensure appropriate fault detection capabilities. - Maintained the original behavior of the code while improving readability and robustness. Signed-off-by: ramoliyaYug --- .../actions/ReplaceItemInUserListAction.java | 83 ------------------- .../actions/ReplaceItemInUserListAction.kt | 77 +++++++++++++++++ 2 files changed, 77 insertions(+), 83 deletions(-) delete mode 100644 catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.java create mode 100644 catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.kt diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.java b/catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.java deleted file mode 100644 index 28934103ee0..00000000000 --- a/catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team - * () - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * An additional term exception under section 7 of the GNU Affero - * General Public License, version 3, is available at - * http://developer.catrobat.org/license_additional_term - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.catrobat.catroid.content.actions; - -import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction; - -import org.catrobat.catroid.content.Scope; -import org.catrobat.catroid.formulaeditor.Formula; -import org.catrobat.catroid.formulaeditor.InterpretationException; -import org.catrobat.catroid.formulaeditor.UserList; - -import java.util.ArrayList; - -public class ReplaceItemInUserListAction extends TemporalAction { - - private Scope scope; - private Formula formulaIndexToReplace; - private Formula formulaItemToInsert; - - private UserList userList; - - @Override - protected void update(float percent) { - if (userList == null) { - return; - } - - Object value = formulaItemToInsert == null ? Double.valueOf(0d) - : formulaItemToInsert.interpretObject(scope); - int indexToReplace; - - try { - indexToReplace = formulaIndexToReplace == null ? 1 - : formulaIndexToReplace.interpretInteger(scope); - } catch (InterpretationException interpretationException) { - indexToReplace = 1; - } - - indexToReplace--; - - if (indexToReplace >= userList.getValue().size() || indexToReplace < 0) { - return; - } - - ((ArrayList) userList.getValue()).set(indexToReplace, value); - } - - public void setUserList(UserList userVariable) { - this.userList = userVariable; - } - - public void setFormulaIndexToReplace(Formula formulaIndexToReplace) { - this.formulaIndexToReplace = formulaIndexToReplace; - } - - public void setFormulaItemToInsert(Formula formulaItemToInsert) { - this.formulaItemToInsert = formulaItemToInsert; - } - - public void setScope(Scope scope) { - this.scope = scope; - } -} diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.kt b/catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.kt new file mode 100644 index 00000000000..940553bc033 --- /dev/null +++ b/catroid/src/main/java/org/catrobat/catroid/content/actions/ReplaceItemInUserListAction.kt @@ -0,0 +1,77 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2022 The Catrobat Team + * () + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * An additional term exception under section 7 of the GNU Affero + * General Public License, version 3, is available at + * http://developer.catrobat.org/license_additional_term + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.catrobat.catroid.content.actions + +import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction +import org.catrobat.catroid.content.Scope +import org.catrobat.catroid.formulaeditor.Formula +import org.catrobat.catroid.formulaeditor.InterpretationException +import org.catrobat.catroid.formulaeditor.UserList + +class ReplaceItemInUserListAction : TemporalAction() { + private var scope: Scope? = null + private var formulaIndexToReplace: Formula? = null + private var formulaItemToInsert: Formula? = null + + private var userList: UserList? = null + + override fun update(percent: Float) { + if (userList == null) { + return + } + + val value = + if (formulaItemToInsert == null) 0.0 else formulaItemToInsert!!.interpretObject(scope) + + var indexToReplace = try { + if (formulaIndexToReplace == null) 1 + else formulaIndexToReplace!!.interpretInteger(scope) + } catch (interpretationException: InterpretationException) { + 1 + } + + indexToReplace-- + + if (indexToReplace >= userList!!.value.size || indexToReplace < 0) { + return + } + + (userList!!.value as ArrayList)[indexToReplace] = value + } + + fun setUserList(userVariable: UserList?) { + this.userList = userVariable + } + + fun setFormulaIndexToReplace(formulaIndexToReplace: Formula?) { + this.formulaIndexToReplace = formulaIndexToReplace + } + + fun setFormulaItemToInsert(formulaItemToInsert: Formula?) { + this.formulaItemToInsert = formulaItemToInsert + } + + fun setScope(scope: Scope?) { + this.scope = scope + } +}