PeriodOffsetUtils { public static ListMap<String, DimensionalItemObject> getPeriodTypePeriodMap( DataQueryParams params ) { if ( params == null || params.getPeriods().isEmpty() ) { return new ListMap<>(); } ListMap<String, DimensionalItemObject> periodTypePeriodMap = PartitionUtils.getPeriodTypePeriodMap( params.getPeriods() ); DimensionalObject dimension = params.getDimension( DATA_X_DIM_ID ); if ( dimension == null ) { return periodTypePeriodMap; } List<DimensionalItemObject> items = dimension.getItems(); ListMap<String, DimensionalItemObject> shiftedMap = new ListMap<>(); for ( DimensionalItemObject item : items ) { if ( item.getPeriodOffset() != 0 ) { shiftedMap.putAll( addPeriodOffset( periodTypePeriodMap, item.getPeriodOffset() ) ); } } Set<DimensionalItemObject> dimensionalItemObjects = shiftedMap.uniqueValues(); for ( DimensionalItemObject dimensionalItemObject : dimensionalItemObjects ) { Period period = (Period) dimensionalItemObject; if ( !periodTypePeriodMap.containsValue( period.getPeriodType().getName(), dimensionalItemObject ) ) { periodTypePeriodMap.putValue( period.getPeriodType().getName(), dimensionalItemObject ); } } return periodTypePeriodMap; }  static ListMap<String, DimensionalItemObject> getPeriodTypePeriodMap( DataQueryParams params ); static Period shiftPeriod( Period period, int periodOffset ); static DataQueryParams removeOffsetPeriodsIfNotNeeded( DataQueryParams params ); static List<Object> getPeriodOffsetRow( Grid grid, DimensionalItemObject dimItem, String isoPeriod,
        int offset );  }
@Test public void verifyGetPeriodTypePeriodMapExitOnNull() { ListMap<String, DimensionalItemObject> periodTypePeriodMap = PeriodOffsetUtils .getPeriodTypePeriodMap( null ); assertThat( periodTypePeriodMap.size(), is( 0 ) ); PeriodOffsetUtils.getPeriodTypePeriodMap( DataQueryParams.newBuilder().build() ); assertThat( periodTypePeriodMap.size(), is( 0 ) ); }
@Test public void verifyMappingIsCreatedWithNoOffset() { Period month1 = createMonthlyPeriod( 2020, 1 ); Period month2 = createMonthlyPeriod( 2020, 2 ); Period month3 = createMonthlyPeriod( 2020, 3 ); Period q1 = createQuarterPeriod( 2020, 1 ); DataQueryParams queryParams = DataQueryParams.newBuilder() .withPeriods( Lists.newArrayList( month1, month2, month3, q1 ) ) .build(); final ListMap<String, DimensionalItemObject> periodMap = PeriodOffsetUtils .getPeriodTypePeriodMap( queryParams ); assertThat( periodMap.size(), is( 2 ) ); assertThat( periodMap.get( MonthlyPeriodType.NAME ), hasSize( 3 ) ); assertThat( periodMap.get( QuarterlyPeriodType.NAME ), hasSize( 1 ) ); }
@Test public void verifyMappingIsCreatedWitOffset() { Period month1 = createMonthlyPeriod( 2020, 1 ); Period q1 = createQuarterPeriod( 2020, 1 ); DataQueryParams queryParams = DataQueryParams.newBuilder() .withPeriods( Lists.newArrayList( month1, q1 ) ) .withDataElements( Lists.newArrayList( createDataElement( 1 ), createDataElement( 5 ) ) ) .build(); final ListMap<String, DimensionalItemObject> periodMap = PeriodOffsetUtils .getPeriodTypePeriodMap( queryParams ); assertThat( periodMap.size(), is( 2 ) ); assertThat( periodMap.get( MonthlyPeriodType.NAME ), hasSize( 3 ) ); assertIsoPeriods( periodMap.get( MonthlyPeriodType.NAME ), "202001", "202002", "202006" ); assertThat( periodMap.get( QuarterlyPeriodType.NAME ), hasSize( 3 ) ); assertIsoPeriods( periodMap.get( QuarterlyPeriodType.NAME ), "2020Q1", "2020Q2", "2021Q2" ); }