View Javadoc
1   package com.gisgraphy.street;
2   
3   import static com.gisgraphy.street.HouseNumberDeserializer.HOUSENUMBER_AND_LOCATION_SEPARATOR;
4   import static com.gisgraphy.street.HouseNumberDeserializer.HOUSE_NUMBERS_SEPARATOR;
5   import static com.gisgraphy.street.HouseNumberDeserializer.LAT_LON_SEPARATOR;
6   
7   import java.util.List;
8   import java.util.Locale;
9   import java.util.regex.Pattern;
10  
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  import com.gisgraphy.domain.geoloc.entity.HouseNumber;
15  
16  import edu.emory.mathcs.backport.java.util.Collections;
17  
18  public class HouseNumberSerializer {
19  	
20  	private Pattern clean_pattern = Pattern.compile(HouseNumberDeserializer.HOUSENUMBER_AND_LOCATION_SEPARATOR);
21  	private HouseNumberComparator comparator = new HouseNumberComparator();
22  	
23  	 /**
24       * The logger
25       */
26      protected static final Logger logger = LoggerFactory
27  	    .getLogger(HouseNumberSerializer.class);
28  	
29  
30  	public String serializeList(List<HouseNumber> houseNumberList){
31  		if (houseNumberList==null || houseNumberList.size()==0){
32  			return null;
33  		}
34  		Collections.sort(houseNumberList,comparator);
35  		StringBuffer sb = new StringBuffer();
36  		for (HouseNumber houseNumber:houseNumberList){
37  			if (houseNumber==null || houseNumber.getNumber()==null || houseNumber.getLocation()==null){
38  				continue;
39  			}
40  			String serialized = serialize(houseNumber);
41  			if (serialized!=null){
42  			sb.append(serialized)
43  			.append(HOUSE_NUMBERS_SEPARATOR);
44  			}
45  		}
46  		return sb.toString().trim();
47  	}
48  	
49  	public String serialize(HouseNumber houseNumber){
50  		if (houseNumber==null ){
51  			return null;
52  		}
53  		StringBuffer sb = new StringBuffer();
54  			if (houseNumber==null || houseNumber.getNumber()==null || houseNumber.getLocation()==null){
55  				return null;
56  			}
57  			String cleaned = clean_pattern.matcher(houseNumber.getNumber()).replaceAll("");
58  			sb.append(cleaned)
59  			.append(HOUSENUMBER_AND_LOCATION_SEPARATOR)
60  			.append(String.format(Locale.US, "%s", houseNumber.getLongitude().doubleValue()))
61  			.append(LAT_LON_SEPARATOR)
62  			.append(String.format(Locale.US, "%s", houseNumber.getLatitude().doubleValue()));
63  		return sb.toString().trim();
64  	}
65  	
66  	
67  }