2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.remote.rpc.utils;
11 import akka.actor.ActorRef;
12 import akka.japi.Pair;
13 import com.google.common.base.Preconditions;
15 import java.util.Collection;
16 import java.util.Comparator;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
21 * This class will return First Entry
23 public class LatestEntryRoutingLogic implements RoutingLogic{
25 private SortedSet<Pair<ActorRef, Long>> actorRefSet;
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");
31 actorRefSet = new TreeSet<>(new LatestEntryComparator());
32 actorRefSet.addAll(entries);
36 public ActorRef select() {
37 return actorRefSet.last().first();
41 private class LatestEntryComparator implements Comparator<Pair<ActorRef, Long>> {
44 public int compare(Pair<ActorRef, Long> o1, Pair<ActorRef, Long> o2) {
45 if(o1 == null && o2 == null) {
48 if(o1 == null && o2 != null) {
51 if(o1 != null && o2 == null) {
55 return o1.second().compareTo(o2.second());