Filter by region codes
This commit is contained in:
@@ -6,6 +6,7 @@ import me.bearns.fias.exceptions.DownloadException;
|
||||
import me.bearns.fias.exceptions.UnzipException;
|
||||
import me.bearns.fias.repository.FiasVersionRepository;
|
||||
import me.bearns.fias.util.Catalog;
|
||||
import me.bearns.fias.util.RegionFilter;
|
||||
import me.bearns.fias.util.UnmarshallerParameters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -122,11 +123,13 @@ public class FiasUpdater implements Updater {
|
||||
|
||||
final ZipEntry entry = entries.nextElement();
|
||||
|
||||
final RegionFilter filter = regions != null ? new RegionFilter(regions) : null;
|
||||
|
||||
final UnmarshallerParameters config = catalog.getByPrefix(entry.getName());
|
||||
|
||||
if(config!=null) { //todo
|
||||
try (InputStream is = zipFile.getInputStream(entry)) {
|
||||
unmarshaller.process(is, config, null);
|
||||
unmarshaller.process(is, config, filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package me.bearns.fias.util;
|
||||
|
||||
import me.bearns.fias.domain.Addrobj;
|
||||
import me.bearns.fias.domain.House;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class RegionFilter implements Predicate {
|
||||
|
||||
private final Set<String> set = new HashSet<>();
|
||||
|
||||
public RegionFilter(Long[] codes) {
|
||||
for (Long code : codes){
|
||||
if(code>0L && code<100L) {
|
||||
set.add(String.format("%1$02d", code));
|
||||
} else {
|
||||
//todo
|
||||
//log.warn("Element out of range: {}", code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Object o) {
|
||||
|
||||
String regioncode = null;
|
||||
|
||||
if(o instanceof Addrobj){
|
||||
regioncode = ((Addrobj) o).regioncode;
|
||||
} else if(o instanceof House){
|
||||
regioncode = ((House) o).regioncode;
|
||||
}
|
||||
|
||||
return regioncode != null && set.contains(regioncode);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user