PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/timerservice/schedule/value/RangeValue.java

#
Java | 140 lines | 57 code | 16 blank | 67 comment | 9 complexity | 59fca4892fda52bc77a1d915dded58d3 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2009, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.ejb3.timerservice.schedule.value;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
  26. /**
  27. * Represents a value for a {@link javax.ejb.ScheduleExpression} which is expressed as a range type. An
  28. * {@link RangeValue} comprises of a start and an end value for the range, separated by a "-"
  29. * <p/>
  30. * <p>
  31. * Each side of the range must be an individual attribute value. Members of a range <b>cannot</b> themselves
  32. * be lists, wild-cards, ranges, or increments. In range ”x-y”, if x is larger than y, the range is equivalent
  33. * to “x-max, min-y”, where max is the largest value of the corresponding attribute and min is the
  34. * smallest. The range “x-x”, where both range values are the same, evaluates to the single value x.
  35. * </p>
  36. *
  37. * @author Jaikiran Pai
  38. * @version $Revision: $
  39. * @see ScheduleExpressionType#RANGE
  40. */
  41. public class RangeValue implements ScheduleValue {
  42. /**
  43. * The separator which is used for parsing a {@link String} which
  44. * represents a {@link RangeValue}
  45. */
  46. public static final String RANGE_SEPARATOR = "-";
  47. private static final Pattern RANGE_PATTERN;
  48. static {
  49. final String POSITIVE_OR_NEGATIVE_INTEGER = "\\s*-?\\s*\\d+\\s*";
  50. final String WORD = "\\s*([1-5][a-zA-Z]{2})?\\s*[a-zA-Z]+\\s*[a-zA-Z]*\\s*";
  51. final String OR = "|";
  52. final String OPEN_GROUP = "(";
  53. final String CLOSE_GROUP = ")";
  54. String rangeRegex = OPEN_GROUP + POSITIVE_OR_NEGATIVE_INTEGER + OR + WORD + CLOSE_GROUP + RANGE_SEPARATOR
  55. + OPEN_GROUP + POSITIVE_OR_NEGATIVE_INTEGER + OR + WORD + CLOSE_GROUP;
  56. RANGE_PATTERN = Pattern.compile(rangeRegex);
  57. }
  58. /**
  59. * The start value of the range
  60. */
  61. private String rangeStart;
  62. /**
  63. * The end value of the range
  64. */
  65. private String rangeEnd;
  66. /**
  67. * Creates a {@link RangeValue} by parsing the passed <code>value</code>.
  68. * <p>
  69. * Upon successfully parsing the passed <code>value</code>, this constructor
  70. * sets the start and the end value of this {@link RangeValue}
  71. * </p>
  72. *
  73. * @param range The value to be parsed
  74. * @throws IllegalArgumentException If the passed <code>value</code> cannot be
  75. * represented as an {@link RangeValue}
  76. */
  77. public RangeValue(String range) {
  78. String[] values = getRangeValues(range);
  79. if (values == null || values.length != 2) {
  80. throw MESSAGES.invalidRange(range);
  81. }
  82. this.rangeStart = values[0].trim();
  83. this.rangeEnd = values[1].trim();
  84. }
  85. /**
  86. * Returns the start value of this {@link RangeValue}
  87. *
  88. * @return
  89. */
  90. public String getStart() {
  91. return this.rangeStart;
  92. }
  93. /**
  94. * Returns the end value of this {@link RangeValue}
  95. *
  96. * @return
  97. */
  98. public String getEnd() {
  99. return this.rangeEnd;
  100. }
  101. public static boolean accepts(String value) {
  102. if (value == null) {
  103. return false;
  104. }
  105. Matcher matcher = RANGE_PATTERN.matcher(value);
  106. return matcher.matches();
  107. }
  108. private String[] getRangeValues(String val) {
  109. if (val == null) {
  110. return null;
  111. }
  112. Matcher matcher = RANGE_PATTERN.matcher(val);
  113. if (!matcher.matches()) {
  114. return null;
  115. }
  116. String[] rangeVals = new String[2];
  117. rangeVals[0] = matcher.group(1);
  118. rangeVals[1] = matcher.group(3);
  119. return rangeVals;
  120. }
  121. public String asString() {
  122. return this.rangeStart + RANGE_SEPARATOR + this.rangeStart;
  123. }
  124. }