Add Amenity and PoiFilter comparators

This commit is contained in:
Roman Inflianskas 2016-05-30 15:06:53 +03:00
parent e7897826ca
commit e0fc6d64c5
4 changed files with 60 additions and 13 deletions

View file

@ -10,6 +10,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@ -19,6 +20,7 @@ import java.util.zip.GZIPInputStream;
public class Amenity extends MapObject {
public static final AmenityByIdComparator BY_ID_COMPARATOR = new AmenityByIdComparator();
public static final String WEBSITE = "website";
public static final String PHONE = "phone";
@ -249,5 +251,15 @@ public class Amenity extends MapObject {
setAdditionalInfo(OPENING_HOURS, openingHours);
}
public static class AmenityByIdComparator implements Comparator<Amenity> {
@Override
public int compare(Amenity a1, Amenity a2) {
int result = MapObject.BY_ID_COMPARATOR.compare(a1, a2);
return result != 0 ? result : a1.type.compareTo(a2.type);
}
public boolean areEqual(Amenity a1, Amenity a2) {
return MapObject.BY_ID_COMPARATOR.areEqual(a1, a2) && a1.type.equals(a2.type);
}
}
}

View file

@ -234,10 +234,8 @@ public abstract class MapObject implements Comparable<MapObject> {
}
public boolean areEqual(MapObject o1, MapObject o2) {
if (o1 == null ^ o2 == null) {
return false;
} else if (o1 == o2) {
return true;
if (o1 == null) {
return o2 == null;
} else {
return collator.equals(o1.getName(l), o2.getName(l));
}
@ -257,10 +255,8 @@ public abstract class MapObject implements Comparable<MapObject> {
}
public boolean areEqual(MapObject o1, MapObject o2) {
if (o1 == null ^ o2 == null) {
return false;
} else if (o1 == o2) {
return true;
if (o1 == null) {
return o2 == null;
} else {
return o1.id.equals(o2.id);
}

View file

@ -8,7 +8,7 @@ import java.util.Map;
import java.util.Set;
public class PoiCategory extends PoiFilter {
public class PoiCategory extends PoiFilter implements Comparable<PoiCategory> {
private List<PoiFilter> poiFilters = new ArrayList<PoiFilter>();
private Set<PoiType> basemapPoi = null;
@ -30,8 +30,7 @@ public class PoiCategory extends PoiFilter {
public String getDefaultTag() {
if(defaultTag == null) {
return keyName;
}
return keyName; }
return defaultTag;
}
@ -70,4 +69,29 @@ public class PoiCategory extends PoiFilter {
}
return basemapPoi.contains(pt);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof PoiCategory)) {
return false;
}
PoiCategory other = (PoiCategory) o;
return regId == other.regId;
}
@Override
public int hashCode() {
return regId;
}
@Override
public int compareTo(PoiCategory poiCategory) {
return Double.compare(regId, poiCategory.regId);
}
@Override
public String toString() {
return keyName + " (" + regId + ")";
}
}

View file

@ -16,13 +16,17 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
@ -595,4 +599,15 @@ public class Algorithms {
return 0xFFFF00FF;
}
}
public static <T> Set<T> findDuplicates(Collection<T> list) {
Set<T> duplicates = new LinkedHashSet<T>();
Set<T> uniques = new HashSet<T>();
for (T t : list) {
if (!uniques.add(t)) {
duplicates.add(t);
}
}
return duplicates;
}
}