PeriodOffsetUtils { public static DataQueryParams removeOffsetPeriodsIfNotNeeded( DataQueryParams params ) { final List<DimensionalItemObject> dimensionalItemObjects = params.getDataElements(); final boolean hasOffset = dimensionalItemObjects.stream().filter( dio -> dio.getDimensionItemType() != null ) .filter( dio -> dio.getDimensionItemType().equals( DimensionItemType.DATA_ELEMENT ) ) .anyMatch( dio -> dio.getPeriodOffset() != 0 ); if ( !hasOffset ) { final List<DimensionalItemObject> nonShiftedPeriods = params.getPeriods().stream() .filter( dio -> ( !( (Period) dio).isShifted() ) ) .collect( Collectors.toList() ); return DataQueryParams.newBuilder( params ) .withPeriods( params.getDimension( PERIOD_DIM_ID ).getDimensionName(), nonShiftedPeriods ) .build(); } return params; }  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 verifyRemoveOffsetPeriodsIfNotNeeded() { Period month1 = createMonthlyPeriod( 2020, 1 ); Period month2 = createMonthlyPeriod( 2020, 2 ); Period month3 = createMonthlyPeriod( 2020, 3 ); month3.setShifted( true ); Period month4 = createMonthlyPeriod( 2020, 4 ); month4.setShifted( true ); DataQueryParams queryParams = DataQueryParams.newBuilder() .withPeriods( Lists.newArrayList( month1, month2, month3, month4 ) ) .withDataElements( Lists.newArrayList( createDataElement( 0 ), createDataElement( 0 ) ) ) .build(); final DataQueryParams dataQueryParams = PeriodOffsetUtils .removeOffsetPeriodsIfNotNeeded( queryParams ); assertThat( dataQueryParams.getPeriods(), hasSize( 2 ) ); assertIsoPeriods( dataQueryParams.getPeriods(), "202001", "202002" ); }
@Test public void verifyRemoveOffsetPeriodsIfNotNeededNoPeriodsRemoved() { Period month1 = createMonthlyPeriod( 2020, 1 ); Period month2 = createMonthlyPeriod( 2020, 2 ); Period month3 = createMonthlyPeriod( 2020, 3 ); month3.setShifted( true ); Period month4 = createMonthlyPeriod( 2020, 4 ); month4.setShifted( true ); DataQueryParams queryParams = DataQueryParams.newBuilder() .withPeriods( Lists.newArrayList( month1, month2, month3, month4 ) ) .withDataElements( Lists.newArrayList( createDataElement( 0 ), createDataElement( 1 ) ) ) .build(); final DataQueryParams dataQueryParams = PeriodOffsetUtils .removeOffsetPeriodsIfNotNeeded( queryParams ); assertThat( dataQueryParams.getPeriods(), hasSize( 4 ) ); }