PageRenderTime 49ms CodeModel.GetById 38ms app.highlight 9ms RepoModel.GetById 0ms 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 */
 22package org.jboss.as.ejb3.timerservice.schedule.value;
 23
 24import java.util.regex.Matcher;
 25import java.util.regex.Pattern;
 26import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
 27/**
 28 * Represents a value for a {@link javax.ejb.ScheduleExpression} which is expressed as a range type. An
 29 * {@link RangeValue} comprises of a start and an end value for the range, separated by a "-"
 30 * <p/>
 31 * <p>
 32 * Each side of the range must be an individual attribute value. Members of a range <b>cannot</b> themselves
 33 * be lists, wild-cards, ranges, or increments. In range ”x-y”, if x is larger than y, the range is equivalent
 34 * to “x-max, min-y”, where max is the largest value of the corresponding attribute and min is the
 35 * smallest. The range “x-x”, where both range values are the same, evaluates to the single value x.
 36 * </p>
 37 *
 38 * @author Jaikiran Pai
 39 * @version $Revision: $
 40 * @see ScheduleExpressionType#RANGE
 41 */
 42public class RangeValue implements ScheduleValue {
 43    /**
 44     * The separator which is used for parsing a {@link String} which
 45     * represents a {@link RangeValue}
 46     */
 47    public static final String RANGE_SEPARATOR = "-";
 48
 49    private static final Pattern RANGE_PATTERN;
 50
 51    static {
 52        final String POSITIVE_OR_NEGATIVE_INTEGER = "\\s*-?\\s*\\d+\\s*";
 53        final String WORD = "\\s*([1-5][a-zA-Z]{2})?\\s*[a-zA-Z]+\\s*[a-zA-Z]*\\s*";
 54        final String OR = "|";
 55        final String OPEN_GROUP = "(";
 56        final String CLOSE_GROUP = ")";
 57
 58        String rangeRegex = OPEN_GROUP + POSITIVE_OR_NEGATIVE_INTEGER + OR + WORD + CLOSE_GROUP + RANGE_SEPARATOR
 59                + OPEN_GROUP + POSITIVE_OR_NEGATIVE_INTEGER + OR + WORD + CLOSE_GROUP;
 60
 61        RANGE_PATTERN = Pattern.compile(rangeRegex);
 62
 63    }
 64
 65    /**
 66     * The start value of the range
 67     */
 68    private String rangeStart;
 69
 70    /**
 71     * The end value of the range
 72     */
 73    private String rangeEnd;
 74
 75    /**
 76     * Creates a {@link RangeValue} by parsing the passed <code>value</code>.
 77     * <p>
 78     * Upon successfully parsing the passed <code>value</code>, this constructor
 79     * sets the start and the end value of this {@link RangeValue}
 80     * </p>
 81     *
 82     * @param range The value to be parsed
 83     * @throws IllegalArgumentException If the passed <code>value</code> cannot be
 84     *                                  represented as an {@link RangeValue}
 85     */
 86    public RangeValue(String range) {
 87        String[] values = getRangeValues(range);
 88        if (values == null || values.length != 2) {
 89            throw MESSAGES.invalidRange(range);
 90        }
 91
 92        this.rangeStart = values[0].trim();
 93        this.rangeEnd = values[1].trim();
 94    }
 95
 96    /**
 97     * Returns the start value of this {@link RangeValue}
 98     *
 99     * @return
100     */
101    public String getStart() {
102        return this.rangeStart;
103    }
104
105    /**
106     * Returns the end value of this {@link RangeValue}
107     *
108     * @return
109     */
110    public String getEnd() {
111        return this.rangeEnd;
112    }
113
114    public static boolean accepts(String value) {
115        if (value == null) {
116            return false;
117        }
118        Matcher matcher = RANGE_PATTERN.matcher(value);
119        return matcher.matches();
120    }
121
122    private String[] getRangeValues(String val) {
123        if (val == null) {
124            return null;
125        }
126        Matcher matcher = RANGE_PATTERN.matcher(val);
127        if (!matcher.matches()) {
128            return null;
129        }
130        String[] rangeVals = new String[2];
131        rangeVals[0] = matcher.group(1);
132        rangeVals[1] = matcher.group(3);
133
134        return rangeVals;
135    }
136
137    public String asString() {
138        return this.rangeStart + RANGE_SEPARATOR + this.rangeStart;
139    }
140}