c0e2973aab53c48e92329d2d96487d277d25531e
[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.util.Collection;
15 import java.util.Comparator;
16 import java.util.SortedSet;
17 import java.util.TreeSet;
18
19 /**
20  * This class will return First Entry.
21  */
22 public class LatestEntryRoutingLogic implements RoutingLogic {
23
24     private final SortedSet<Pair<ActorRef, Long>> actorRefSet;
25
26     public LatestEntryRoutingLogic(Collection<Pair<ActorRef, Long>> entries) {
27         Preconditions.checkNotNull(entries, "Entries should not be null");
28         Preconditions.checkArgument(!entries.isEmpty(), "Entries collection should not be empty");
29
30         actorRefSet = new TreeSet<>(new LatestEntryComparator());
31         actorRefSet.addAll(entries);
32     }
33
34     @Override
35     public ActorRef select() {
36         return actorRefSet.last().first();
37     }
38
39     private class LatestEntryComparator implements Comparator<Pair<ActorRef, Long>> {
40
41         @Override
42         public int compare(Pair<ActorRef, Long> o1, Pair<ActorRef, Long> o2) {
43             if (o1 == null && o2 == null) {
44                 return 0;
45             }
46             if (o1 == null && o2 != null) {
47                 return -1;
48             }
49             if (o1 != null && o2 == null) {
50                 return 1;
51             }
52
53             return o1.second().compareTo(o2.second());
54
55         }
56     }
57 }
58
59