Fix FindBugs warnings in sal-remoterpc-connector and enable enforcement
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / utils / LatestEntryRoutingLogic.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.remote.rpc.utils;
10
11 import akka.actor.ActorRef;
12 import akka.japi.Pair;
13 import com.google.common.base.Preconditions;
14 import java.io.Serializable;
15 import java.util.Collection;
16 import java.util.Comparator;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19
20 /**
21  * This class will return First Entry.
22  */
23 public class LatestEntryRoutingLogic implements RoutingLogic {
24
25     private final SortedSet<Pair<ActorRef, Long>> actorRefSet;
26
27     public LatestEntryRoutingLogic(Collection<Pair<ActorRef, Long>> entries) {
28         Preconditions.checkNotNull(entries, "Entries should not be null");
29         Preconditions.checkArgument(!entries.isEmpty(), "Entries collection should not be empty");
30
31         actorRefSet = new TreeSet<>(new LatestEntryComparator());
32         actorRefSet.addAll(entries);
33     }
34
35     @Override
36     public ActorRef select() {
37         return actorRefSet.last().first();
38     }
39
40     private static class LatestEntryComparator implements Comparator<Pair<ActorRef, Long>>, Serializable {
41         private static final long serialVersionUID = 1L;
42
43         @Override
44         public int compare(Pair<ActorRef, Long> o1, Pair<ActorRef, Long> o2) {
45             if (o1 == null && o2 == null) {
46                 return 0;
47             }
48
49             if (o1 != null && o2 != null && o1.second() == null && o2.second() == null) {
50                 return 0;
51             }
52
53             if ((o1 == null || o1.second() == null) && o2 != null) {
54                 return -1;
55             }
56
57             if (o2 == null || o2.second() == null) {
58                 return 1;
59             }
60
61             return o1.second().compareTo(o2.second());
62         }
63     }
64 }
65
66