1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-05 20:41:34 +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

@ -21,59 +21,55 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class MobileNumberUtils { public class MobileNumberUtils {
private static final Logger logger = LoggerFactory.getLogger(MobileNumberUtils.class); private static final Logger logger = LoggerFactory.getLogger(MobileNumberUtils.class);
private static Map<String , MobileAddress> mobileAddressMap = new HashMap<String ,MobileAddress>(); private static final Map<String, MobileAddress> mobileAddressMap = new HashMap<>();
private static boolean isInited = false; private static boolean isInited = false;
public static int init() throws IOException{ public static int init() throws IOException {
File file = new File( MobileNumberUtils.class.getResource("/config/mobile.data").getFile()); File file = new File(MobileNumberUtils.class.getResource("/config/mobile.data").getFile());
logger.info("init with file [{}]", file.getAbsolutePath()); logger.info("init with file [{}]", file.getAbsolutePath());
if(file.exists()){ if (file.exists()) {
FileInputStream reader = new FileInputStream(file); FileInputStream reader = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(reader , "UTF-8"); InputStreamReader isr = new InputStreamReader(reader, StandardCharsets.UTF_8);
BufferedReader bf = new BufferedReader(isr); BufferedReader bf = new BufferedReader(isr);
try{ try {
String data = null ; String data;
while((data = bf.readLine()) != null){ while ((data = bf.readLine()) != null) {
String[] group = data.split("[\t ]") ; String[] group = data.split("[\t ]");
MobileAddress address = null ; MobileAddress address = null;
if(group.length == 5){ if (group.length == 5) {
address = new MobileAddress(group[0], group[1], group[2], group[3],group[4]) ; address = new MobileAddress(group[0], group[1], group[2], group[3], group[4]);
}else if(group.length == 4){ } else if (group.length == 4) {
address = new MobileAddress(group[0], group[1], group[2], group[2],group[3]) ; 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) ;
} }
if (address != null) {
mobileAddressMap.putIfAbsent(address.getCode(), address);
mobileAddressMap.putIfAbsent(address.getAreacode(), address);
} }
} }
isInited = true; isInited = true;
logger.info("inited successfully, map size [{}]", mobileAddressMap.size()); logger.info("inited successfully, map size [{}]", mobileAddressMap.size());
}catch(Exception ex){ } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
}finally{ } finally {
bf.close(); bf.close();
isr.close(); isr.close();
reader.close(); reader.close();
} }
} }
return mobileAddressMap.size() ; return mobileAddressMap.size();
} }
/** /**
* 根据呼入号码 找到对应 城市 , 需要传入的号码是 手机号 或者 固话号码位数为 11位 * 根据呼入号码 找到对应 城市 , 需要传入的号码是 手机号 或者 固话号码位数为 11位
* @param phoneNumber
* @return
*/ */
public static MobileAddress getAddress(String phoneNumber){ public static MobileAddress getAddress(String phoneNumber) {
if(!isInited){ if (!isInited) {
try { try {
MobileNumberUtils.init(); MobileNumberUtils.init();
} catch (IOException e) { } catch (IOException e) {
@ -83,13 +79,13 @@ public class MobileNumberUtils {
} }
String code = ""; String code = "";
if(!StringUtils.isBlank(phoneNumber) && phoneNumber.length() > 10){ if (!StringUtils.isBlank(phoneNumber) && phoneNumber.length() > 10) {
if(phoneNumber.startsWith("0")){ if (phoneNumber.startsWith("0")) {
code = phoneNumber.substring(0 , 4) ; code = phoneNumber.substring(0, 4);
}else if(phoneNumber.startsWith("1")){ } else if (phoneNumber.startsWith("1")) {
code = phoneNumber.substring(0 , 7) ; code = phoneNumber.substring(0, 7);
} }
} }
return mobileAddressMap.get(code) ; return mobileAddressMap.get(code);
} }
} }