From 6b96cef40897a9ba3c36e7021598bb3a85b3721e Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Fri, 3 Apr 2020 11:11:53 +0800 Subject: [PATCH] Reformat class MobileNumberUtils --- .../cc/util/mobile/MobileNumberUtils.java | 186 +++++++++--------- 1 file changed, 91 insertions(+), 95 deletions(-) diff --git a/contact-center/app/src/main/java/com/chatopera/cc/util/mobile/MobileNumberUtils.java b/contact-center/app/src/main/java/com/chatopera/cc/util/mobile/MobileNumberUtils.java index 828a7dad..d81a5899 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/util/mobile/MobileNumberUtils.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/util/mobile/MobileNumberUtils.java @@ -1,95 +1,91 @@ -/* - * Copyright (C) 2017 优客服-多渠道客服系统 - * Modifications copyright (C) 2018-2019 Chatopera Inc, - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.chatopera.cc.util.mobile; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.util.HashMap; -import java.util.Map; - -public class MobileNumberUtils { - private static final Logger logger = LoggerFactory.getLogger(MobileNumberUtils.class); - private static Map mobileAddressMap = new HashMap(); - private static boolean isInited = false; - - public static int init() throws IOException{ - File file = new File( MobileNumberUtils.class.getResource("/config/mobile.data").getFile()); - logger.info("init with file [{}]", file.getAbsolutePath()); - if(file.exists()){ - FileInputStream reader = new FileInputStream(file); - InputStreamReader isr = new InputStreamReader(reader , "UTF-8"); - BufferedReader bf = new BufferedReader(isr); - try{ - String data = null ; - while((data = bf.readLine()) != null){ - String[] group = data.split("[\t ]") ; - MobileAddress address = null ; - if(group.length == 5){ - address = new MobileAddress(group[0], group[1], group[2], group[3],group[4]) ; - }else if(group.length == 4){ - address = new MobileAddress(group[0], group[1], group[2], group[2],group[3]) ; - } - if(address!=null){ - if(mobileAddressMap.get(address.getCode()) == null){ - mobileAddressMap.put(address.getCode(), address) ; - } - if(mobileAddressMap.get(address.getAreacode()) == null){ - mobileAddressMap.put(address.getAreacode(), address) ; - } - } - } - isInited = true; - logger.info("inited successfully, map size [{}]", mobileAddressMap.size()); - }catch(Exception ex){ - ex.printStackTrace(); - }finally{ - bf.close(); - isr.close(); - reader.close(); - } - } - return mobileAddressMap.size() ; - } - /** - * 根据呼入号码 找到对应 城市 , 需要传入的号码是 手机号 或者 固话号码,位数为 11位 - * @param phoneNumber - * @return - */ - public static MobileAddress getAddress(String phoneNumber){ - if(!isInited){ - try { - MobileNumberUtils.init(); - } catch (IOException e) { - logger.error("getAddress error: ", e); - e.printStackTrace(); - } - } - - String code = ""; - if(!StringUtils.isBlank(phoneNumber) && phoneNumber.length() > 10){ - if(phoneNumber.startsWith("0")){ - code = phoneNumber.substring(0 , 4) ; - }else if(phoneNumber.startsWith("1")){ - code = phoneNumber.substring(0 , 7) ; - } - } - return mobileAddressMap.get(code) ; - } -} +/* + * Copyright (C) 2017 优客服-多渠道客服系统 + * Modifications copyright (C) 2018-2019 Chatopera Inc, + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.chatopera.cc.util.mobile; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +public class MobileNumberUtils { + private static final Logger logger = LoggerFactory.getLogger(MobileNumberUtils.class); + private static final Map mobileAddressMap = new HashMap<>(); + private static boolean isInited = false; + + public static int init() throws IOException { + File file = new File(MobileNumberUtils.class.getResource("/config/mobile.data").getFile()); + logger.info("init with file [{}]", file.getAbsolutePath()); + if (file.exists()) { + FileInputStream reader = new FileInputStream(file); + InputStreamReader isr = new InputStreamReader(reader, StandardCharsets.UTF_8); + BufferedReader bf = new BufferedReader(isr); + try { + String data; + while ((data = bf.readLine()) != null) { + String[] group = data.split("[\t ]"); + MobileAddress address = null; + if (group.length == 5) { + address = new MobileAddress(group[0], group[1], group[2], group[3], group[4]); + } else if (group.length == 4) { + address = new MobileAddress(group[0], group[1], group[2], group[2], group[3]); + } + if (address != null) { + mobileAddressMap.putIfAbsent(address.getCode(), address); + mobileAddressMap.putIfAbsent(address.getAreacode(), address); + } + } + isInited = true; + logger.info("inited successfully, map size [{}]", mobileAddressMap.size()); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + bf.close(); + isr.close(); + reader.close(); + } + } + return mobileAddressMap.size(); + } + + /** + * 根据呼入号码 找到对应 城市 , 需要传入的号码是 手机号 或者 固话号码,位数为 11位 + */ + public static MobileAddress getAddress(String phoneNumber) { + if (!isInited) { + try { + MobileNumberUtils.init(); + } catch (IOException e) { + logger.error("getAddress error: ", e); + e.printStackTrace(); + } + } + + String code = ""; + if (!StringUtils.isBlank(phoneNumber) && phoneNumber.length() > 10) { + if (phoneNumber.startsWith("0")) { + code = phoneNumber.substring(0, 4); + } else if (phoneNumber.startsWith("1")) { + code = phoneNumber.substring(0, 7); + } + } + return mobileAddressMap.get(code); + } +}