lundi 19 novembre 2018

Running if condition after all three items are updated

I have a list which is supposed to receive values from several hosts periodically and do some operations on these values.

List<Integer> loadList = new ArrayList<>(3);
if ((dstIp.toString().equals("10.0.0.1")
                        && udp.getPayload() != null) {

                    loadList.add(0, intLoad1);
                }
                else if ((dstIp.toString().equals("10.0.0.2") 
                        && udp.getPayload() != null) {

                    loadList.add(1, intLoad2);
                }
                else if ((dstIp.toString().equals("10.0.0.3") 
                        && udp.getPayload() != null) {

                    loadList.add(2, intLoad3);
                }

                if (!loadList.isEmpty() && !loadList.contains(null)) {
                    int sum = loadList.stream().mapToInt(Integer::intValue).sum();
                    System.out.println("---- Sum: "+sum);
                    double averageLoad = ((double) sum) / loadList.size();
                }

In my code the 4th if condition (I mean if (!loadList.isEmpty() && !loadList.contains(null))) will run every time it receives a new value, but I'm trying to change it so that it will run after the end of each period (when all the three elements in the list are updated). I mean I want it to run only when all the three load value are received (so that I will find the average value of the newly received values).

I searched for a solution but I didn't find anything. Is it possible?

Aucun commentaire:

Enregistrer un commentaire