From d83b84b14694faffc957540d97f423051a36e466 Mon Sep 17 00:00:00 2001 From: Gary Wu Date: Thu, 13 Apr 2017 14:47:14 -0700 Subject: Simplify RollbackManager Simplify RollbackManager using FunctionalJava and Java 8 features. Change-Id: Icfaa16926b90cb3cfd293c14026731671e007629 Signed-off-by: Gary Wu --- .../common/transaction/mngr/RollbackManager.java | 60 ++++++++-------------- 1 file changed, 20 insertions(+), 40 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java b/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java index 0641524038..99e0da0116 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java @@ -20,68 +20,54 @@ package org.openecomp.sdc.common.transaction.mngr; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - import org.openecomp.sdc.common.transaction.api.RollbackHandler; import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum; import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBTypeEnum; import org.openecomp.sdc.common.util.MethodActivationStatusEnum; +import fj.P; import fj.data.Either; +import fj.data.HashMap; +import fj.data.List; public class RollbackManager { - private Map rollBackHandlersMap; - private Integer transactionId; - private String userId, actionType; + private final HashMap rollbackHandlersMap; + private final Integer transactionId; + private final String userId; + private final String actionType; - RollbackManager(Integer transactionId, String userId, String actionType, List roleBackHandlers) { + RollbackManager(Integer transactionId, String userId, String actionType, Iterable rollbackHandlers) { this.transactionId = transactionId; this.userId = userId; this.actionType = actionType; - rollBackHandlersMap = new HashMap<>(); - for (RollbackHandler handler : roleBackHandlers) { - rollBackHandlersMap.put(handler.getDBType(), handler); - } - + this.rollbackHandlersMap = HashMap.from(List.iterableList(rollbackHandlers).map(i -> P.p(i.getDBType(), i))); } public DBActionCodeEnum transactionRollback() { - DBActionCodeEnum rollbackResult = DBActionCodeEnum.SUCCESS; - Iterator handlersItr = rollBackHandlersMap.values().iterator(); - while (handlersItr.hasNext()) { - RollbackHandler handler = handlersItr.next(); - DBActionCodeEnum rollbackCode = handler.doRollback(); - if (rollbackCode == DBActionCodeEnum.FAIL_GENERAL) { - rollbackResult = DBActionCodeEnum.FAIL_GENERAL; - } - } - - return rollbackResult; + List results = rollbackHandlersMap.values().map(RollbackHandler::doRollback); + boolean failure = results.exists(r -> r == DBActionCodeEnum.FAIL_GENERAL); + return failure ? DBActionCodeEnum.FAIL_GENERAL : DBActionCodeEnum.SUCCESS; } protected Either addRollbackHandler(RollbackHandler rollbackHandler) { Either result; - if (rollBackHandlersMap.containsKey(rollbackHandler.getDBType())) { + if (rollbackHandlersMap.contains(rollbackHandler.getDBType())) { result = Either.right(MethodActivationStatusEnum.NOT_ALLOWED); } else { - rollBackHandlersMap.put(rollbackHandler.getDBType(), rollbackHandler); + rollbackHandlersMap.set(rollbackHandler.getDBType(), rollbackHandler); result = Either.left(rollbackHandler); } return result; } - protected Either createRollbackHandler(DBTypeEnum dbType) { + protected Either createRollbackHandler(final DBTypeEnum dbType) { - final DBTypeEnum dbTypeFinal = dbType; RollbackHandler rollbackHandler = new RollbackHandler(transactionId, userId, actionType) { @Override public DBTypeEnum getDBType() { - return dbTypeFinal; + return dbType; } @Override @@ -89,19 +75,13 @@ public class RollbackManager { return true; } }; - Either result = addRollbackHandler(rollbackHandler); - - return result; + return addRollbackHandler(rollbackHandler); } protected Either getRollbackHandler(DBTypeEnum dbType) { - Either result; - if (rollBackHandlersMap.containsKey(dbType)) { - result = Either.left(rollBackHandlersMap.get(dbType)); - } else { - result = Either.right(MethodActivationStatusEnum.NOT_FOUND); - } - return result; + // need to swap here because the uses of Either in SDC appears to be opposite of convention + // by convention left is failure; in SDC right is failure + return rollbackHandlersMap.get(dbType).toEither(MethodActivationStatusEnum.NOT_FOUND).swap(); } } -- cgit 1.2.3-korg