f01baf009ba9026c23ab43eed10089e3961e0fdf
[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
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 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
41   private class LatestEntryComparator implements Comparator<Pair<ActorRef, Long>> {
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       if(o1 == null && o2 != null) {
49         return -1;
50       }
51       if(o1 != null && o2 == null) {
52         return 1;
53       }
54
55       return o1.second().compareTo(o2.second());
56
57     }
58
59   }
60 }
61
62