HardwareAssign.java 5.92 KB
package com.example.mina.entity;

import java.util.*;

public class HardwareAssign {

//    private int userId;// all to this user

    private Map<Integer, Integer> outputUserMap;//<output,user>
    private Map<Integer, List<Integer>> userInputListMap;//<user, input list>


    public HardwareAssign() {
        outputUserMap = new HashMap<>();
        userInputListMap = new HashMap<>();
    }


    //-------------------- input -----------------------
    public int getRealInputByUser(int userId, int order) {
        if(order < 1){
            return 0;
        }

        List<Integer> idList = this.getAssignedInputListByUser(userId);
        Collections.sort(idList);
        if (order > idList.size()) {
            return 0;
        }

        return idList.get(order - 1);
    }


    public List<Integer> getAssignedInputListByUser(int userId) {
        return userInputListMap.getOrDefault(userId, new ArrayList<>());
    }



    public void freeInputToUser(int inputPort, int userId) {
        List<Integer> idList = userInputListMap.get(userId);
        if (idList != null) {
            idList.remove(Integer.valueOf(inputPort));
        }
    }

    public void assignInputToUser(int inputPort, int userId) {

        List<Integer> idList = userInputListMap.computeIfAbsent(userId, k -> new ArrayList<>());

        idList.add(inputPort);
    }


    public boolean isAssignedInputToUser(int inputPort, int userId) {
        List<Integer> 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<Integer> idList = this.getAssignedOutputListByUser(userId);
        Collections.sort(idList);
        if (order > idList.size()) {
            return 0;
        }

        return idList.get(order - 1);
    }


    public List<Integer> getAssignedOutputListByUser(int userId) {
        List<Integer> idList = new ArrayList<>();

        for(int id : outputUserMap.keySet()){
            int uid = outputUserMap.get(id);
            if (uid == userId) {
                idList.add(id);
            }
        }

        return idList;
    }

    public List<Integer> getOutputAssignedUserList() {
        List<Integer> 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<Integer> 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<Integer> 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<Integer> inputList = new ArrayList<>();
        for (int i = 1; i <= maxInput; i++) {
            inputList.add(i);
        }
        userInputListMap.put(userId, inputList);

    }

    //-------------------- output ------- End ----------------


    public Map<Integer, Integer> getOutputUserMap() {
        if (outputUserMap == null) {
            outputUserMap = new HashMap<>();
        }
        return outputUserMap;
    }

    public void setOutputUserMap(Map<Integer, Integer> outputUserMap) {
        this.outputUserMap = outputUserMap;
    }

    public Map<Integer, List<Integer>> getUserInputListMap() {
        return userInputListMap;
    }

    public void setUserInputListMap(Map<Integer, List<Integer>> userInputListMap) {
        this.userInputListMap = userInputListMap;
    }
}