All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feilong.tools.emailaddress.EmailAddressUtil Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.0.8
Show newest version
/*
 * Copyright (C) 2008 feilong
 *
 * 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.feilong.tools.emailaddress;

import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.core.bean.ConvertUtil.toList;
import static com.feilong.core.util.ResourceBundleUtil.getResourceBundle;
import static com.feilong.core.util.ResourceBundleUtil.toMap;
import static com.feilong.core.util.SortUtil.sortListByPropertyNamesValue;
import static com.feilong.formatter.FormatterUtil.formatToSimpleTable;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.lang.StringUtil;
import com.feilong.lib.lang3.StringUtils;
import com.feilong.core.Validate;

/**
 * 解析邮箱地址工具类.
 *
 * @author feilong
 * @see Email_address
 * @since 1.0.1
 */
@SuppressWarnings("squid:S1192") //String literals should not be duplicated
public final class EmailAddressUtil{

    /** The Constant log. */
    private static final Logger               LOGGER                    = LoggerFactory.getLogger(EmailAddressUtil.class);

    //---------------------------------------------------------------

    /** The Constant AT. */
    private static final String               AT                        = "@";

    /** 值得分隔符. */
    private static final String               DELIMITERS                = ",;";

    /** 配置文件地址. */
    static final Map          EMAIL_PROVIDER_MAP        = toMap(getResourceBundle("config/feilong-emailProvider"));

    //---------------------------------------------------------------

    /** key 为 domain. */
    private static Map domainAndEmailProviderMap = new ConcurrentHashMap<>();

    //---------------------------------------------------------------

    static{
        init();
    }

    //---------------------------------------------------------------

    /** Don't let anyone instantiate this class. */
    private EmailAddressUtil(){
        //AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
        //see 《Effective Java》 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    //---------------------------------------------------------------

    /**
     * 基于 emailAddress 获得 {@link EmailProvider} .
     * 
     * 

原理:

*
*

* 先基于 emailAddress,解析获得 domain,然后转成小写,从 {@link #domainAndEmailProviderMap}中取到对应的value *

*
* *

示例:

*
* *
     * EmailProvider emailProvider = EmailAddressUtil.getEmailProvider("[email protected]");
     * LOGGER.debug(JsonUtil.format(emailProvider));
     * 
* * 返回: * *
     * {
     * "webSite": "http://mail.163.com/",
     * "name": "网易163邮箱",
     * "domain": "163.com"
     * }
     * 
* *
* * @param emailAddress * 邮箱地址 * @return 如果 emailAddress 是null,抛出 {@link NullPointerException}
* 如果 emailAddress 是blank,抛出 {@link IllegalArgumentException}
* @see #getDomain(String) * @since 1.5.3 */ public static EmailProvider getEmailProvider(String emailAddress){ Validate.notBlank(emailAddress, "emailAddress can't be blank!"); String domain = getDomain(emailAddress); return isNullOrEmpty(domain) ? null : domainAndEmailProviderMap.get(domain.toLowerCase()); } /** * 获得邮件地址的后缀名. * *

示例:

* *
* *
     * EmailAddressUtil.getDomain("[email protected]") = 163.com
     * 
* *
* * @param emailAddress * 邮件地址 * @return 如果 emailAddress 是null,抛出 {@link NullPointerException}
* 如果 emailAddress 是blank,抛出 {@link IllegalArgumentException}
* @since 1.5.3 */ public static String getDomain(String emailAddress){ Validate.notBlank(emailAddress, "emailAddress can't be blank!"); return StringUtils.substringAfterLast(emailAddress, AT); } /** * 获得邮件地址的前缀. * *
     * EmailAddressUtil.getUserName("[email protected]") = feilong
     * 
* * @param emailAddress * 邮箱地址 * @return 如果 emailAddress 是null,抛出 {@link NullPointerException}
* 如果 emailAddress 是blank,抛出 {@link IllegalArgumentException}
* @since 1.5.3 */ public static String getUser(String emailAddress){ Validate.notBlank(emailAddress, "emailAddress can't be blank!"); return StringUtil.split(emailAddress, AT)[0]; } //--------------------------------------------------------------- /** * 初始化. * * @since 1.5.3 */ private static void init(){ Validate.notEmpty(EMAIL_PROVIDER_MAP, "EmailServiceProvider properties can't be null/empty!"); //--------------------------------------------------------------- for (Map.Entry entry : EMAIL_PROVIDER_MAP.entrySet()){ String key = entry.getKey(); //统统转成小写 domainAndEmailProviderMap.put(key.toLowerCase(), buildEmailProvider(key, entry.getValue())); } //--------------------------------------------------------------- if (LOGGER.isDebugEnabled()){ //Caused by: java.lang.ClassCastException: java.util.concurrent.ConcurrentHashMap$Values cannot be cast to java.util.List //ConcurrentHashMap$Values 是自定义的类 List iterable = sortListByPropertyNamesValue(toList(domainAndEmailProviderMap.values()), "domain"); LOGGER.debug("init [data] over,{}", formatToSimpleTable(iterable, "domain", "webSite", "name")); } } //--------------------------------------------------------------- /** * Builds the email provider. * * @param key * the key * @param value * the value * @return the email provider * @since 1.10.4 */ private static EmailProvider buildEmailProvider(String key,String value){ String[] tokenizeToStringArray = StringUtil.tokenizeToStringArray(value, DELIMITERS); return new EmailProvider(key, tokenizeToStringArray[0], tokenizeToStringArray[1]); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy