X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2Futils%2FLatestEntryRoutingLogic.java;fp=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2Futils%2FLatestEntryRoutingLogic.java;h=f01baf009ba9026c23ab43eed10089e3961e0fdf;hb=b896a5f4bd63df605ecb886deafc19416171b013;hp=0000000000000000000000000000000000000000;hpb=1cced2b2c51d0b8e2c1acbffbe4266d366f0fb14;p=controller.git diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/utils/LatestEntryRoutingLogic.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/utils/LatestEntryRoutingLogic.java new file mode 100644 index 0000000000..f01baf009b --- /dev/null +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/utils/LatestEntryRoutingLogic.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.remote.rpc.utils; + +import akka.actor.ActorRef; +import akka.japi.Pair; +import com.google.common.base.Preconditions; + +import java.util.Collection; +import java.util.Comparator; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * This class will return First Entry + */ +public class LatestEntryRoutingLogic implements RoutingLogic{ + + private SortedSet> actorRefSet; + + public LatestEntryRoutingLogic(Collection> entries) { + Preconditions.checkNotNull(entries, "Entries should not be null"); + Preconditions.checkArgument(!entries.isEmpty(), "Entries collection should not be empty"); + + actorRefSet = new TreeSet<>(new LatestEntryComparator()); + actorRefSet.addAll(entries); + } + + @Override + public ActorRef select() { + return actorRefSet.last().first(); + } + + + private class LatestEntryComparator implements Comparator> { + + @Override + public int compare(Pair o1, Pair o2) { + if(o1 == null && o2 == null) { + return 0; + } + if(o1 == null && o2 != null) { + return -1; + } + if(o1 != null && o2 == null) { + return 1; + } + + return o1.second().compareTo(o2.second()); + + } + + } +} + +