1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

Reformat class MobileNumberUtils

This commit is contained in:
dengchao@xgtl 2020-04-03 11:11:53 +08:00
parent 27a6666f14
commit 6b96cef408

View File

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