It is common to encounter some synchronization data, or some crawler data, upstream API from the address format of different problems. Write a simple address resolution start. In order to be compatible with some old systems, using JDK1.7 write.


import com.xxx.Address;

import org.apache.shiro.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AddressResolutionUtil {
    private final static String[][] provinceAndCityAndDistrictsRegexWrapper = {{"Save"."Autonomous region"."District"."The city"}, / / province
            {"The city"."Autonomous prefecture"."Area"."Administrative unit"."Au"."County"}, / / the city
            {"The city"."County"."Area"."Flag"."Sea"."Island"} / / area
    };


    /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *@paramAdd source string *@paramWithSuffix Whether suffixes such as province * are required for Shandong province@returnThe address class *@see AddressMatchIndexAndKey
     */
    public static Address resolveAddress(String add, boolean withSuffix) {
        add = add.replaceAll("".""); // Clear the address format
        String[] proAndCityAndDistrictsFullName = new String[3];
        String[] proAndCityAndDistricts = new String[3];
        for (int i = 0; i < provinceAndCityAndDistrictsRegexWrapper.length; i++) { / / provinces,
            List<AddressMatchIndexAndKey> allMatchKey = new ArrayList<>();
            for (String key : provinceAndCityAndDistrictsRegexWrapper[i]) // Check whether the key is matched
                if (add.contains(key) && (add.indexOf(key) > 1)) // Whether to include a key
                    allMatchKey.add(new AddressMatchIndexAndKey(key, add.indexOf(key))); // All eligible sets
            if(! CollectionUtils.isEmpty(allMatchKey)) { AddressMatchIndexAndKey minIndexAndKey = Collections.min(allMatchKey);// Get the nearest key
                if(! withSuffix)// Compatible with Suffix Condition Indicates Suffix for the first time
                    proAndCityAndDistrictsFullName[i] = add.substring(0,
                            minIndexAndKey.getIndex() + minIndexAndKey.getKey().length());
                proAndCityAndDistricts[i] = add.substring(0,
                        withSuffix ? minIndexAndKey.getIndex() + minIndexAndKey.getKey().length()
                                : minIndexAndKey.getIndex()); // Determine whether suffix is needed based on withSuffix. For example, whether Shandong province needs suffix for the second time
                add = add.substring(minIndexAndKey.getIndex() + minIndexAndKey.getKey().length());
            }
        }
        Address address = Address.of(proAndCityAndDistricts[0], proAndCityAndDistricts[1], proAndCityAndDistricts[2]);
        for (String key : withSuffix ? proAndCityAndDistricts : proAndCityAndDistrictsFullName) { // The third time to judge the suffix
            if(key ! =null)
                add = add.replace(key, "");
        }
        address.setDetail(add);
        return address;
    }

    static class AddressMatchIndexAndKey implements Comparable<AddressMatchIndexAndKey> {
        private String key;
        private Integer index;

        public AddressMatchIndexAndKey(String key, Integer index) {
            this.key = key;
            this.index = index;
        }

        public String getKey(a) {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public Integer getIndex(a) {
            return index;
        }

        public void setIndex(Integer index) {
            this.index = index;
        }

        @Override
        public int compareTo(AddressMatchIndexAndKey o) {
            returnInteger.compare(index, o.index); }}}Copy the code

Address class

public class Address {

    private String province;
    private String city;
    private String region;
    private String detail;


    public static Address of(String province,String city,String region){
        Address address = new Address();
        address.setProvince(province);
        address.setCity(city);
        address.setRegion(region);
        return address;
    }

    public static Address empty(a){
        Address address = new Address();
        address.setProvince("");
        address.setCity("");
        address.setRegion("");
        address.setDetail("");
        return address;
    }

    public String getDetail(a) {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getProvince(a) {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity(a) {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getRegion(a) {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

Copy the code