package com.example.mina.entity; import java.util.*; public class HardwareAssign { // private int userId;// all to this user private Map outputUserMap;// private Map> userInputListMap;// public HardwareAssign() { outputUserMap = new HashMap<>(); userInputListMap = new HashMap<>(); } //-------------------- input ----------------------- public int getRealInputByUser(int userId, int order) { if(order < 1){ return 0; } List idList = this.getAssignedInputListByUser(userId); Collections.sort(idList); if (order > idList.size()) { return 0; } return idList.get(order - 1); } public List getAssignedInputListByUser(int userId) { return userInputListMap.getOrDefault(userId, new ArrayList<>()); } public void freeInputToUser(int inputPort, int userId) { List idList = userInputListMap.get(userId); if (idList != null) { idList.remove(Integer.valueOf(inputPort)); } } public void assignInputToUser(int inputPort, int userId) { List idList = userInputListMap.computeIfAbsent(userId, k -> new ArrayList<>()); idList.add(inputPort); } public boolean isAssignedInputToUser(int inputPort, int userId) { List idList = userInputListMap.get(userId); if (idList == null) { return false; } return idList.contains(inputPort); } //-------------------- input ------- End ---------------- //-------------------- output ----------------------- public int getRealOutputByUser(int userId, int order) { if(order < 1){ return 0; } List idList = this.getAssignedOutputListByUser(userId); Collections.sort(idList); if (order > idList.size()) { return 0; } return idList.get(order - 1); } public List getAssignedOutputListByUser(int userId) { List idList = new ArrayList<>(); for(int id : outputUserMap.keySet()){ int uid = outputUserMap.get(id); if (uid == userId) { idList.add(id); } } return idList; } public List getOutputAssignedUserList() { List idList = new ArrayList<>(); for (int id : outputUserMap.values()) { boolean find = false; for (int uid : idList) { if (id == uid) { find = true; break; } } if (!find) { idList.add(id); } } return idList; } public int checkOutputAssigned(int outputPort) { Integer id = outputUserMap.get(outputPort); return id == null ? 0 : id; } public void freeAssignOutput(int outputPort, int fromUserId) { Integer id = outputUserMap.get(outputPort); if (id != null) { if (id == fromUserId) { outputUserMap.remove(outputPort); } } } public void assignOutputToUser(String colsStr, int toUserId) { if ((colsStr!=null) && !colsStr.isEmpty() && !colsStr.startsWith("null")) { String[] tokens = colsStr.split(","); for (String t : tokens) { int port = Integer.valueOf(t); if (port > 0) { outputUserMap.put(port, toUserId); } } } } public void reAssignOutput(int outputPort, int fromUserId, int toUserId) { if (outputPort > 0 && fromUserId > 0 && toUserId > 0) { outputUserMap.put(outputPort, toUserId); } } public boolean isAllAssignedToUser(int userId, int maxInput, int maxOutput) { Integer id; for (int i = 1; i <= maxOutput; i++) { id = outputUserMap.get(i); if (id == null || id != userId) { return false; } } List inList = userInputListMap.get(userId); if(inList == null){ for (int i = 1; i <= maxInput; i++) { if(!inList.contains(i)){ return false; } } } return true; } public void removeAllAssignToUser(int userId) { List removeList = new ArrayList<>(); for (int key : outputUserMap.keySet()) { if (outputUserMap.get(key) == userId) { removeList.add(key); } } for (int key : removeList) { outputUserMap.remove(key); } if (userInputListMap.containsKey(userId)) { userInputListMap.remove(userId); } } public void assignAllToUser(int userId, int maxInput, int maxOutput) { outputUserMap = new HashMap<>(); userInputListMap = new HashMap<>(); for (int i = 1; i <= maxOutput; i++) { outputUserMap.put(i, userId); } List inputList = new ArrayList<>(); for (int i = 1; i <= maxInput; i++) { inputList.add(i); } userInputListMap.put(userId, inputList); } //-------------------- output ------- End ---------------- public Map getOutputUserMap() { if (outputUserMap == null) { outputUserMap = new HashMap<>(); } return outputUserMap; } public void setOutputUserMap(Map outputUserMap) { this.outputUserMap = outputUserMap; } public Map> getUserInputListMap() { return userInputListMap; } public void setUserInputListMap(Map> userInputListMap) { this.userInputListMap = userInputListMap; } }