@Test public void test_ExtractDuration_happyPath() { RangerTimeOfDayMatcher matcher = new RangerTimeOfDayMatcher(); Object[][] input = new Object[][] { { "9am-5pm", true, 9*60, (12+5)*60 }, { "1 PM - 10P.M.", true, (12+1)*60, (12+10)*60 }, { "1PM - 9AM", true, (12+1)*60, 9*60 }, { "11am-12pm", true, 11*60, 12*60 }, { "11pm-12am", true, (12+11)*60, 0*60 }, { "12am-12pm", true, 0*60, 12*60 }, { "12pm-12am", true, 12*60, 0*60 }, { "1PM", false, null, null }, }; for (Object[] data: input) { int[] duration = matcher.extractDuration((String)data[0]); boolean expectedToMatch = (boolean)data[1]; if (expectedToMatch) { int start = (Integer)data[2]; int end = (Integer)data[3]; Assert.assertArrayEquals(new int[] { start, end }, duration); } else { Assert.assertNull(duration); } } }
int[] extractDuration(String value) { if(LOG.isDebugEnabled()) { LOG.debug("==> RangerTimeOfDayMatcher.extractDuration(" + value + ")"); } int[] result = null; if (value == null) { LOG.warn("extractDuration: null input value!"); } else { Matcher m = _Pattern.matcher(value); if (!m.matches()) { LOG.warn("extractDuration: input[" + value + "] did not match pattern!"); } else { int startHour = Integer.parseInt(m.group(1)); int startMin = 0; if (m.group(3) != null) { startMin = Integer.parseInt(m.group(3)); } String startType = m.group(4).toUpperCase(); int endHour = Integer.parseInt(m.group(5)); int endMinute = 0; if (m.group(7) != null) { endMinute = Integer.parseInt(m.group(7)); } String endType = m.group(8).toUpperCase(); if(startHour == 12) { if("A".equals(startType)) { startHour = 0; } } else if ("P".equals(startType)) { startHour += 12; } if(endHour == 12) { if("A".equals(endType)) { endHour = 0; } } else if ("P".equals(endType)) { endHour += 12; } result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute }; } } if(LOG.isDebugEnabled()) { LOG.debug("<== RangerTimeOfDayMatcher.extractDuration(" + value + "): duration:" + Arrays.toString(result)); } return result; }
RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { int[] extractDuration(String value) { if(LOG.isDebugEnabled()) { LOG.debug("==> RangerTimeOfDayMatcher.extractDuration(" + value + ")"); } int[] result = null; if (value == null) { LOG.warn("extractDuration: null input value!"); } else { Matcher m = _Pattern.matcher(value); if (!m.matches()) { LOG.warn("extractDuration: input[" + value + "] did not match pattern!"); } else { int startHour = Integer.parseInt(m.group(1)); int startMin = 0; if (m.group(3) != null) { startMin = Integer.parseInt(m.group(3)); } String startType = m.group(4).toUpperCase(); int endHour = Integer.parseInt(m.group(5)); int endMinute = 0; if (m.group(7) != null) { endMinute = Integer.parseInt(m.group(7)); } String endType = m.group(8).toUpperCase(); if(startHour == 12) { if("A".equals(startType)) { startHour = 0; } } else if ("P".equals(startType)) { startHour += 12; } if(endHour == 12) { if("A".equals(endType)) { endHour = 0; } } else if ("P".equals(endType)) { endHour += 12; } result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute }; } } if(LOG.isDebugEnabled()) { LOG.debug("<== RangerTimeOfDayMatcher.extractDuration(" + value + "): duration:" + Arrays.toString(result)); } return result; } }
RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { int[] extractDuration(String value) { if(LOG.isDebugEnabled()) { LOG.debug("==> RangerTimeOfDayMatcher.extractDuration(" + value + ")"); } int[] result = null; if (value == null) { LOG.warn("extractDuration: null input value!"); } else { Matcher m = _Pattern.matcher(value); if (!m.matches()) { LOG.warn("extractDuration: input[" + value + "] did not match pattern!"); } else { int startHour = Integer.parseInt(m.group(1)); int startMin = 0; if (m.group(3) != null) { startMin = Integer.parseInt(m.group(3)); } String startType = m.group(4).toUpperCase(); int endHour = Integer.parseInt(m.group(5)); int endMinute = 0; if (m.group(7) != null) { endMinute = Integer.parseInt(m.group(7)); } String endType = m.group(8).toUpperCase(); if(startHour == 12) { if("A".equals(startType)) { startHour = 0; } } else if ("P".equals(startType)) { startHour += 12; } if(endHour == 12) { if("A".equals(endType)) { endHour = 0; } } else if ("P".equals(endType)) { endHour += 12; } result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute }; } } if(LOG.isDebugEnabled()) { LOG.debug("<== RangerTimeOfDayMatcher.extractDuration(" + value + "): duration:" + Arrays.toString(result)); } return result; }  }
RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { int[] extractDuration(String value) { if(LOG.isDebugEnabled()) { LOG.debug("==> RangerTimeOfDayMatcher.extractDuration(" + value + ")"); } int[] result = null; if (value == null) { LOG.warn("extractDuration: null input value!"); } else { Matcher m = _Pattern.matcher(value); if (!m.matches()) { LOG.warn("extractDuration: input[" + value + "] did not match pattern!"); } else { int startHour = Integer.parseInt(m.group(1)); int startMin = 0; if (m.group(3) != null) { startMin = Integer.parseInt(m.group(3)); } String startType = m.group(4).toUpperCase(); int endHour = Integer.parseInt(m.group(5)); int endMinute = 0; if (m.group(7) != null) { endMinute = Integer.parseInt(m.group(7)); } String endType = m.group(8).toUpperCase(); if(startHour == 12) { if("A".equals(startType)) { startHour = 0; } } else if ("P".equals(startType)) { startHour += 12; } if(endHour == 12) { if("A".equals(endType)) { endHour = 0; } } else if ("P".equals(endType)) { endHour += 12; } result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute }; } } if(LOG.isDebugEnabled()) { LOG.debug("<== RangerTimeOfDayMatcher.extractDuration(" + value + "): duration:" + Arrays.toString(result)); } return result; }  @Override void init(); @Override boolean isMatched(RangerAccessRequest request); @Override String toString(); }
RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { int[] extractDuration(String value) { if(LOG.isDebugEnabled()) { LOG.debug("==> RangerTimeOfDayMatcher.extractDuration(" + value + ")"); } int[] result = null; if (value == null) { LOG.warn("extractDuration: null input value!"); } else { Matcher m = _Pattern.matcher(value); if (!m.matches()) { LOG.warn("extractDuration: input[" + value + "] did not match pattern!"); } else { int startHour = Integer.parseInt(m.group(1)); int startMin = 0; if (m.group(3) != null) { startMin = Integer.parseInt(m.group(3)); } String startType = m.group(4).toUpperCase(); int endHour = Integer.parseInt(m.group(5)); int endMinute = 0; if (m.group(7) != null) { endMinute = Integer.parseInt(m.group(7)); } String endType = m.group(8).toUpperCase(); if(startHour == 12) { if("A".equals(startType)) { startHour = 0; } } else if ("P".equals(startType)) { startHour += 12; } if(endHour == 12) { if("A".equals(endType)) { endHour = 0; } } else if ("P".equals(endType)) { endHour += 12; } result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute }; } } if(LOG.isDebugEnabled()) { LOG.debug("<== RangerTimeOfDayMatcher.extractDuration(" + value + "): duration:" + Arrays.toString(result)); } return result; }  @Override void init(); @Override boolean isMatched(RangerAccessRequest request); @Override String toString();  }