AdxPeriod { public static Period parse( String periodString ) throws AdxException { String[] tokens = periodString.split( "/" ); if ( tokens.length != 2 ) { throw new AdxException( periodString + " not in valid <date>/<duration> format" ); } try { Period period; PeriodType periodType = null; Date startDate = DateUtils.getMediumDate( tokens[0] ); Calendar cal = Calendar.getInstance(); cal.setTime( startDate ); Duration duration = Duration.valueOf( tokens[1] ); switch ( duration ) { case P1D: periodType = new DailyPeriodType(); break; case P7D: periodType = new WeeklyPeriodType(); break; case P1M: periodType = new MonthlyPeriodType(); break; case P2M: periodType = new BiMonthlyPeriodType(); break; case P3M: periodType = new QuarterlyPeriodType(); break; case P6M: switch ( cal.get( Calendar.MONTH ) ) { case 0: periodType = new SixMonthlyPeriodType(); break; case 6: periodType = new SixMonthlyAprilPeriodType(); break; default: throw new AdxException( periodString + "is invalid sixmonthly type" ); } case P1Y: switch ( cal.get( Calendar.MONTH ) ) { case 0: periodType = new YearlyPeriodType(); break; case 3: periodType = new FinancialAprilPeriodType(); break; case 6: periodType = new FinancialJulyPeriodType(); break; case 9: periodType = new FinancialOctoberPeriodType(); break; default: throw new AdxException( periodString + "is invalid yearly type" ); } } if ( periodType != null ) { period = periodType.createPeriod( startDate ); } else { throw new AdxException( "Failed to create period type from " + duration ); } return period; } catch ( IllegalArgumentException ex ) { throw new AdxException( tokens[1] + " is not a supported duration type" ); } }  static Period parse( String periodString ); static String serialize( Period period );  }
@Test public void testParser() { try { period = AdxPeriod.parse( "2015-01-01/P1Y" ); assertEquals( "2015", period.getIsoDate() ); period = AdxPeriod.parse( "2015-01-01/P1M" ); assertEquals( "201501", period.getIsoDate() ); period = AdxPeriod.parse( "2015-01-01/P1D" ); assertEquals( "20150101", period.getIsoDate() ); period = AdxPeriod.parse( "2015-01-01/P3M" ); assertEquals( "2015Q1", period.getIsoDate() ); period = AdxPeriod.parse( "2015-04-01/P3M" ); assertEquals( "2015Q2", period.getIsoDate() ); period = AdxPeriod.parse( "2015-01-01/P7D" ); assertEquals( "2015W1", period.getIsoDate() ); period = AdxPeriod.parse( "2015-01-05/P7D" ); assertEquals( "2015W2", period.getIsoDate() ); } catch ( AdxException ex) { fail(ex.getMessage()); } }
@Test public void testBadDuration() { try { period = AdxPeriod.parse( "2014-01-01/P1" ); fail( "Should have thrown exception parsing 2015-01-01/P1" ); } catch ( Exception ex ) { assertEquals( AdxException.class, ex.getClass() ); } }
@Test public void testFinancialTypes() { try { period = AdxPeriod.parse( "2015-01-01/P1Y" ); assertEquals( "2015", period.getIsoDate() ); period = AdxPeriod.parse( "2015-04-01/P1Y" ); assertEquals( "2015April", period.getIsoDate() ); period = AdxPeriod.parse( "2015-07-01/P1Y" ); assertEquals( "2015July", period.getIsoDate() ); period = AdxPeriod.parse( "2015-10-01/P1Y" ); assertEquals( "2015Oct", period.getIsoDate() ); } catch ( AdxException ex) { fail(ex.getMessage()); } }
@Test public void testFailFinancialType() { try { period = AdxPeriod.parse( "2014-02-01/P1Y" ); fail( "Should have thrown exception parsing 2014-02-01/P1Y" ); } catch ( Exception ex ) { assertEquals( AdxException.class, ex.getClass() ); } }