|
| 1 | +package ldbc.socialnet.dbgen.serializer; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | + |
| 8 | +import ldbc.socialnet.dbgen.dictionary.LocationDictionary; |
| 9 | + |
| 10 | +import com.google.gson.ExclusionStrategy; |
| 11 | +import com.google.gson.FieldAttributes; |
| 12 | + |
| 13 | +/** |
| 14 | + * Container class used to store all the generator statistics. |
| 15 | + * |
| 16 | + */ |
| 17 | +public class Statistics { |
| 18 | + |
| 19 | + /** |
| 20 | + * This was used in early states to exclude a field of a class in the gson parser library. |
| 21 | + * It is keep for the sake of having a quick example/place to write future field exclusions. |
| 22 | + */ |
| 23 | + public class StatisticsExclude implements ExclusionStrategy { |
| 24 | + |
| 25 | + public boolean shouldSkipClass(Class<?> arg0) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + public boolean shouldSkipField(FieldAttributes f) { |
| 30 | + |
| 31 | + return (f.getDeclaringClass() == CountryPair.class && f.getName().equals("population")); |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Container to keep paired countries and their population. It is used to sort |
| 38 | + * this pairs by population. |
| 39 | + */ |
| 40 | + private class CountryPair implements Comparable<CountryPair> { |
| 41 | + public String[] countries = new String[2]; |
| 42 | + public Long population; |
| 43 | + |
| 44 | + |
| 45 | + public int compareTo(CountryPair pair) { |
| 46 | + return pair.population.compareTo(population); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public Integer minPersonId; |
| 51 | + public Integer maxPersonId; |
| 52 | + public String minWorkFrom; |
| 53 | + public String maxWorkFrom; |
| 54 | + public String minPostCreationDate; |
| 55 | + public String maxPostCreationDate; |
| 56 | + public HashSet<String> firstNames; |
| 57 | + public HashSet<String> tagNames; |
| 58 | + public HashSet<String> countries; |
| 59 | + public HashSet<String> tagClasses; |
| 60 | + private ArrayList<String[]> countryPairs; |
| 61 | + |
| 62 | + public Statistics() { |
| 63 | + minPersonId = Integer.MAX_VALUE; |
| 64 | + maxPersonId = Integer.MIN_VALUE; |
| 65 | + firstNames = new HashSet<String>(); |
| 66 | + tagNames = new HashSet<String>(); |
| 67 | + tagClasses = new HashSet<String>(); |
| 68 | + countries = new HashSet<String>(); |
| 69 | + countryPairs = new ArrayList<String[]>(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Makes all the country pairs of the countries found in the countries statistic field belonging |
| 74 | + * to the same continent. This pairs are sorted by population. |
| 75 | + * |
| 76 | + * @param dicLocation The location dictionary. |
| 77 | + */ |
| 78 | + public void makeCountryPairs(LocationDictionary dicLocation) { |
| 79 | + HashMap<Integer, ArrayList<Integer>> closeCountries = new HashMap<Integer, ArrayList<Integer>>(); |
| 80 | + for (String s : countries) { |
| 81 | + Integer id = dicLocation.getCountryId(s); |
| 82 | + Integer continent = dicLocation.belongsTo(id); |
| 83 | + if (!closeCountries.containsKey(continent)) { |
| 84 | + closeCountries.put(continent, new ArrayList<Integer>()); |
| 85 | + } |
| 86 | + closeCountries.get(continent).add(id); |
| 87 | + } |
| 88 | + |
| 89 | + ArrayList<CountryPair> toSort = new ArrayList<CountryPair>(); |
| 90 | + for (ArrayList<Integer> relatedCountries : closeCountries.values()) { |
| 91 | + for (int i = 0; i < relatedCountries.size(); i++) { |
| 92 | + for (int j = i+1; j < relatedCountries.size(); j++) { |
| 93 | + CountryPair pair = new CountryPair(); |
| 94 | + pair.countries[0] = dicLocation.getLocationName(relatedCountries.get(i)); |
| 95 | + pair.countries[1] = dicLocation.getLocationName(relatedCountries.get(j)); |
| 96 | + pair.population = dicLocation.getPopulation(relatedCountries.get(i)) |
| 97 | + + dicLocation.getPopulation(relatedCountries.get(j)); |
| 98 | + toSort.add(pair); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + Collections.sort(toSort); |
| 103 | + for (CountryPair p : toSort) { |
| 104 | + countryPairs.add(p.countries); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get gson exclusion class for the statistics. |
| 110 | + * |
| 111 | + * @return The exclusion class. |
| 112 | + */ |
| 113 | + public StatisticsExclude getExclusion() { |
| 114 | + return new StatisticsExclude(); |
| 115 | + } |
| 116 | +} |
0 commit comments