Without going into details, there are two instances of the method:

public static Map<CustomerServiceType, Double> calculateLossPerService(ServiceNodeAgent<?, ?, ?> scope) { Map<CustomerServiceType, Double> resultEnumKeys = Maps.newHashMap(); Map<CustomerServiceType, Long> serviceTypesToDroppedPackets = scope.getDroppedPacketsByServiceType().reduceRows(); Map<CustomerServiceType, Long> serviceTypesToReceivedPackets = scope.getReceivedPacketsByServiceType().reduceRows(); for (Map.Entry<CustomerServiceType, Long> customerServiceTypeDoubleEntry : serviceTypesToReceivedPackets.entrySet()) { Long droppedPackets = serviceTypesToDroppedPackets.get(customerServiceTypeDoubleEntry.getKey()); Long receivedPackets = Math.max(customerServiceTypeDoubleEntry.getValue(), 1); double result = droppedPackets / (double) receivedPackets; resultEnumKeys.put(customerServiceTypeDoubleEntry.getKey(), CommonUtils.clip(0.0, result, 1.0)); } return resultEnumKeys; } 

// ------------------------------------------------ -----------

  public static Map<CustomerServiceType, Double> calculateLossPerService(ServiceNodeAgent scope) { Map<CustomerServiceType, Double> resultEnumKeys = Maps.newHashMap(); Map serviceTypesToDroppedPackets = scope.getDroppedPacketsByServiceType().reduceRows(); Map serviceTypesToReceivedPackets = scope.getReceivedPacketsByServiceType().reduceRows(); for (Object customerServiceTypeDoubleEntry : serviceTypesToReceivedPackets.entrySet()) { Map.Entry<CustomerServiceType, Long> customerServiceTypeDoubleEntry1 = (Map.Entry<CustomerServiceType, Long>) customerServiceTypeDoubleEntry;//<---тут Long droppedPackets = (Long) serviceTypesToDroppedPackets.get(customerServiceTypeDoubleEntry1.getKey()); Long receivedPackets = Math.max(customerServiceTypeDoubleEntry1.getValue(), 1); double result = droppedPackets / (double) receivedPackets; resultEnumKeys.put(customerServiceTypeDoubleEntry1.getKey(), CommonUtils.clip(0.0, result, 1.0)); } return resultEnumKeys; } 

In the second one, the compiler cursed that Unchecked cast in the string // <--- here After corrections, it acquired the form of 1 fragment. The question is why now the compiler stopped swearing?

    1 answer 1

    In the second fragment, the customerServiceTypeDoubleEntry variable is of type Object . Obviously, any type can actually be hidden behind Object . Therefore any casting will be unsafe.

    In the first fragment, the customerServiceTypeDoubleEntry variable has a well-defined type corresponding to the type of elements in the serviceTypesToReceivedPackets . The compiler sees this and has no complaints.

    • I did not quite correctly ask the question. I’m wondering why changing the type of the function argument to ServiceNodeAgent <?,?,?> Helped the compiler navigate? - voipp
    • @voipp Your fragments differ not only in the type of the function argument, but also in almost all the specified types. I do not have enough information to answer the question. - a_gura
    • If in the second piece of code to change the argument in a given way, then all the varings will disappear. The first piece of code is already refactored. In the end, the question boils down, why did replacing the argument help so much? - voipp