StatisticsHelper { public static StatisticsGroupingKey buildGroupingKey( Object attributeValue, StatisticsCaseAttribute attribute, StatisticsCaseSubAttribute subAttribute, Function<Integer, RegionReferenceDto> regionProvider, Function<Integer, DistrictReferenceDto> districtProvider, Function<Integer, CommunityReferenceDto> communityProvider, Function<Integer, FacilityReferenceDto> facilityProvider) { if (isNullOrUnknown(attributeValue)) { return null; } if (subAttribute != null) { switch (subAttribute) { case YEAR: return new Year((int) attributeValue); case QUARTER: return new Quarter((int) attributeValue); case MONTH: return Month.values()[(int) attributeValue - 1]; case EPI_WEEK: return new EpiWeek(null, (int) attributeValue); case QUARTER_OF_YEAR: String entryAsString = String.valueOf(attributeValue); return new QuarterOfYear( new Quarter(Integer.valueOf(entryAsString.substring(entryAsString.length() - 1))), new Year(Integer.valueOf(entryAsString.substring(0, entryAsString.length() - 1)))); case MONTH_OF_YEAR: entryAsString = String.valueOf(attributeValue); return new MonthOfYear( Month.values()[Integer.valueOf(entryAsString.substring(entryAsString.length() - 2)) - 1], new Year(Integer.valueOf(entryAsString.substring(0, entryAsString.length() - 2)))); case EPI_WEEK_OF_YEAR: entryAsString = String.valueOf(attributeValue); return new EpiWeek( Integer.valueOf(entryAsString.substring(0, entryAsString.length() - 2)), Integer.valueOf(entryAsString.substring(entryAsString.length() - 2))); case REGION: return regionProvider.apply(((Number) attributeValue).intValue()); case DISTRICT: return districtProvider.apply(((Number) attributeValue).intValue()); case COMMUNITY: return communityProvider.apply(((Number) attributeValue).intValue()); case FACILITY: return facilityProvider.apply(((Number) attributeValue).intValue()); default: throw new IllegalArgumentException(subAttribute.toString()); } } else { switch (attribute) { case DISEASE: return Disease.valueOf(attributeValue.toString()); case SEX: return Sex.valueOf(attributeValue.toString()); case CLASSIFICATION: return CaseClassification.valueOf(attributeValue.toString()); case OUTCOME: return CaseOutcome.valueOf(attributeValue.toString()); case AGE_INTERVAL_1_YEAR: case AGE_INTERVAL_5_YEARS: case AGE_INTERVAL_CHILDREN_COARSE: case AGE_INTERVAL_CHILDREN_FINE: case AGE_INTERVAL_CHILDREN_MEDIUM: case AGE_INTERVAL_BASIC: String entryAsString = attributeValue.toString(); if (attribute == StatisticsCaseAttribute.AGE_INTERVAL_5_YEARS) { try { AgeGroup ageGroup = AgeGroup.valueOf(entryAsString); return ageGroup.toIntegerRange(); } catch (IllegalArgumentException e) { } } if (entryAsString.contains("-")) { return new IntegerRange( Integer.valueOf(entryAsString.substring(0, entryAsString.indexOf("-"))), Integer.valueOf(entryAsString.substring(entryAsString.indexOf("-") + 1))); } else if (entryAsString.contains("+")) { return new IntegerRange(Integer.valueOf(entryAsString.substring(0, entryAsString.indexOf("+"))), null); } else { return new IntegerRange(Integer.valueOf(entryAsString), Integer.valueOf(entryAsString)); } case REPORTING_USER_ROLE: return UserRole.valueOf(attributeValue.toString()); default: throw new IllegalArgumentException(attribute.toString()); } } } private  StatisticsHelper(); static StatisticsGroupingKey buildGroupingKey(
		Object attributeValue,
		StatisticsCaseAttribute attribute,
		StatisticsCaseSubAttribute subAttribute,
		Function<Integer, RegionReferenceDto> regionProvider,
		Function<Integer, DistrictReferenceDto> districtProvider,
		Function<Integer, CommunityReferenceDto> communityProvider,
		Function<Integer, FacilityReferenceDto> facilityProvider); static List<StatisticsGroupingKey> getAgeIntervalGroupingKeys(StatisticsCaseAttribute attribute); static List<StatisticsGroupingKey> getTimeGroupingKeys(StatisticsCaseAttribute attribute, StatisticsCaseSubAttribute subAttribute); @SuppressWarnings("unchecked") static List<StatisticsGroupingKey> getAttributeGroupingKeys(StatisticsCaseAttribute attribute, StatisticsCaseSubAttribute subAttribute); static boolean isNullOrUnknown(Object value); static boolean isUnknown(Object value); static final String VALUE_UNKNOWN; static final String TOTAL; static final String NOT_SPECIFIED; }
@Test public void testBuildGroupingKey() { EpiWeek epiWeek = (EpiWeek) StatisticsHelper .buildGroupingKey(1803, StatisticsCaseAttribute.REPORT_TIME, StatisticsCaseSubAttribute.EPI_WEEK_OF_YEAR, null, null, null, null); assertThat(epiWeek.getYear(), equalTo(18)); assertThat(epiWeek.getWeek(), equalTo(3)); MonthOfYear monthOfYear = (MonthOfYear) StatisticsHelper .buildGroupingKey(1811, StatisticsCaseAttribute.REPORT_TIME, StatisticsCaseSubAttribute.MONTH_OF_YEAR, null, null, null, null); assertThat(monthOfYear.getYear().getValue(), equalTo(18)); assertThat(monthOfYear.getMonth(), equalTo(Month.NOVEMBER)); QuarterOfYear quarterOfYear = (QuarterOfYear) StatisticsHelper .buildGroupingKey(182, StatisticsCaseAttribute.REPORT_TIME, StatisticsCaseSubAttribute.QUARTER_OF_YEAR, null, null, null, null); assertThat(quarterOfYear.getYear().getValue(), equalTo(18)); assertThat(quarterOfYear.getQuarter().getValue(), equalTo(2)); }