Java實現類比路由功能

來源:互聯網
上載者:User

標籤:java   junit   dom4j   

本文實現根據路由表適配最優的號段,選擇路由器。選擇路由的規則:

 1、號段num和網元都不能重複

  2、號段num不能重複

  3、選擇號碼適配長度最長的網元為路由目的地

RouterService.java

package com.discover.router;import java.util.HashMap;import java.util.Iterator;import java.util.Map.Entry;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;/** * 簡易路由器 * @author Administrator * */public class RouterService {    HashMap<String, String> routerInfoMap = new HashMap<String, String>();    /**     * 載入路由表規則:     * 1、號段num和網元都不能重複     * 2、號段num不能重複     * @param routerRule 路由規則     * @return     */    public boolean loadRule(String routerRule) {        try {            Document doc = DocumentHelper.parseText(routerRule);            Element routertable = doc.getRootElement();            Iterator routerIterator = routertable.elementIterator("router");            while(routerIterator.hasNext())            {                Element router = (Element) routerIterator.next();                String num = router.elementTextTrim("num");                String net = router.elementTextTrim("net");                if(num.isEmpty()||net.isEmpty())                {                    return false;                }                                if(routerInfoMap.containsKey(num))                {                    return false;                }                else {                    routerInfoMap.put(num, net);                }                            }        } catch (Exception e) {            return false;        }                return true;    }        /**     * 選擇路由     * 1、選擇號碼適配長度最長的網元為路由目的地     * @param phone 電話號碼     * @return     */    public String selRouter(String phone) {        Iterator iterator = routerInfoMap.entrySet().iterator();        String num = "";        String net = "";        while (iterator.hasNext()) {            Entry<String, String> entry = (Entry<String, String>)iterator.next();            String key = entry.getKey();            if(phone.indexOf(key)>-1)            {                if(key.length()>num.length())                {                    net = entry.getValue();                }            }        }                return net;    }}



採用JUnit對路由模擬器進行測試

TestRouterService.java

package com.discover.test;import junit.framework.TestCase;import com.discover.router.RouterService;/** * @author Administrator * */public class TestRouterService extends TestCase {    public void testcase1() {        RouterService routerService = new RouterService();        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"            +"<routertable><router><num></num><net>router1</net></router></routertable>";        assertEquals(false, routerService.loadRule(routerRule));    }        public void testcase2() {        RouterService routerService = new RouterService();        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"            +"<routertable><router><num>13567</num><net></net></router></routertable>";        assertEquals(false, routerService.loadRule(routerRule));    }        public void testcase3() {        RouterService routerService = new RouterService();        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"            +"<routertable><router><num>13567</num><net>router1</net></router>"        +"<router><num>13567</num><net>router2</net></router></routertable>";        assertEquals(false, routerService.loadRule(routerRule));    }        public void testcase4() {        RouterService routerService = new RouterService();        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"            +"<routertable><router><num>13562</num><net>router1</net></router></routertable>";        assertEquals(true, routerService.loadRule(routerRule));        assertEquals("router1", routerService.selRouter("13562779908"));    }            public void testcase5() {        RouterService routerService = new RouterService();        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"            +"<routertable><router><num>13562</num><net>router1</net></router>"            +"<router><num>135627</num><net>router2</net></router></routertable>";        assertEquals(true, routerService.loadRule(routerRule));        assertEquals("router2", routerService.selRouter("13562779908"));    }}


 測試結果:


 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java實現類比路由功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.