Do not duplicate candidate list 45/35545/3
authorRobert Varga <rovarga@cisco.com>
Mon, 29 Feb 2016 11:45:52 +0000 (12:45 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 7 Mar 2016 16:29:38 +0000 (17:29 +0100)
Iterables.getLast() provides an efficient way of getting the last item
from a collection, without the need to duplicate it.

Change-Id: I49b0bc827bffc0adb616cf516438bad7526ad141
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LastCandidateSelectionStrategy.java

index ea7deb5..73236c2 100644 (file)
@@ -8,19 +8,17 @@
 
 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
 
-import java.util.ArrayList;
+import com.google.common.collect.Iterables;
 import java.util.Collection;
-import java.util.List;
 import java.util.Map;
 
 public class LastCandidateSelectionStrategy extends AbstractEntityOwnerSelectionStrategy {
-    public LastCandidateSelectionStrategy(long selectionDelayInMillis) {
+    public LastCandidateSelectionStrategy(final long selectionDelayInMillis) {
         super(selectionDelayInMillis);
     }
 
     @Override
-    public String newOwner(Collection<String> viableCandidates, Map<String, Long> statistics) {
-        List<String> candidates = new ArrayList<>(viableCandidates);
-        return candidates.get(candidates.size()-1);
+    public String newOwner(final Collection<String> viableCandidates, final Map<String, Long> statistics) {
+        return Iterables.getLast(viableCandidates);
     }
 }