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;
14 import java.util.Collection;
15 import java.util.Comparator;
16 import java.util.SortedSet;
17 import java.util.TreeSet;
20 * This class will return First Entry.
22 public class LatestEntryRoutingLogic implements RoutingLogic {
24 private final SortedSet<Pair<ActorRef, Long>> actorRefSet;
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");
30 actorRefSet = new TreeSet<>(new LatestEntryComparator());
31 actorRefSet.addAll(entries);
35 public ActorRef select() {
36 return actorRefSet.last().first();
39 private class LatestEntryComparator implements Comparator<Pair<ActorRef, Long>> {
42 public int compare(Pair<ActorRef, Long> o1, Pair<ActorRef, Long> o2) {
43 if (o1 == null && o2 == null) {
46 if (o1 == null && o2 != null) {
49 if (o1 != null && o2 == null) {
53 return o1.second().compareTo(o2.second());