Coverage Report - net.sf.jmatchparser.util.NumberExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberExpression
100%
54/54
100%
44/44
5
NumberExpression$NumberRange
100%
9/9
100%
6/6
5
 
 1  
 /* 
 2  
  * NumberExpression.java - a simple number expression parser
 3  
  * 
 4  
  * Copyright (c) 2010 - 2011 Michael Schierl
 5  
  * 
 6  
  * All rights reserved.
 7  
  * 
 8  
  * Redistribution and use in source and binary forms, with or without
 9  
  * modification, are permitted provided that the following conditions
 10  
  * are met:
 11  
  * 
 12  
  * - Redistributions of source code must retain the above copyright notice,
 13  
  *   this list of conditions and the following disclaimer.
 14  
  *   
 15  
  * - Redistributions in binary form must reproduce the above copyright
 16  
  *   notice, this list of conditions and the following disclaimer in the
 17  
  *   documentation and/or other materials provided with the distribution.
 18  
  *   
 19  
  * - Neither name of the copyright holders nor the names of its
 20  
  *   contributors may be used to endorse or promote products derived from
 21  
  *   this software without specific prior written permission.
 22  
  *   
 23  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 24  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 25  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 26  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 27  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 28  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 29  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 30  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 31  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 32  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 33  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 34  
  */
 35  
 package net.sf.jmatchparser.util;
 36  
 
 37  
 /**
 38  
  * An expression that matches nonnegative numbers. This supports cron-like
 39  
  * expressions, like <code>1,3-6,100-200,666,1000-3000/5,400-/7</code>,
 40  
  * <code>-100,102-</code> or <code>*</code>. Odd or even numbers can be matched
 41  
  * either by cron's step syntax, or by suffixing a simple range (without step
 42  
  * values) with <code>e</code> or <code>o</code>.
 43  
  * 
 44  
  * @author Michael Schierl
 45  
  */
 46  
 public class NumberExpression {
 47  
 
 48  
         private final NumberRange[] ranges;
 49  
         private final int min, max;
 50  
 
 51  
         /**
 52  
          * Create a new {@link NumberExpression}.
 53  
          * 
 54  
          * @param pattern
 55  
          *            the expression pattern.
 56  
          * @throws IllegalArgumentException
 57  
          *             if the pattern is malformed
 58  
          */
 59  93
         public NumberExpression(String pattern) {
 60  93
                 String[] parts = pattern.toLowerCase().split(",", -1);
 61  93
                 ranges = new NumberRange[parts.length];
 62  93
                 int min = Integer.MAX_VALUE, max = 0;
 63  214
                 for (int i = 0; i < ranges.length; i++) {
 64  127
                         String part = parts[i];
 65  
                         try {
 66  127
                                 if (part.equals("*")) {
 67  4
                                         ranges[i] = new NumberRange(0, Integer.MAX_VALUE, 0, 1);
 68  123
                                 } else if (part.matches("\\*/\\d+")) {
 69  8
                                         ranges[i] = new NumberRange(0, Integer.MAX_VALUE, 0, Integer.parseInt(part.substring(2)));
 70  115
                                 } else if (part.matches("\\d+")) {
 71  38
                                         int value = Integer.parseInt(part);
 72  37
                                         ranges[i] = new NumberRange(value, value, 0, 1);
 73  37
                                 } else if (part.matches("\\d*-\\d*")) {
 74  48
                                         String[] limits = part.split("-", -1);
 75  48
                                         int from = limits[0].length() == 0 ? 0 : Integer.parseInt(limits[0]);
 76  48
                                         int to = limits[1].length() == 0 ? Integer.MAX_VALUE : Integer.parseInt(limits[1]);
 77  48
                                         if (to < from)
 78  1
                                                 throw new IllegalArgumentException("Invalid pattern: " + part);
 79  47
                                         ranges[i] = new NumberRange(from, to, 0, 1);
 80  47
                                 } else if (part.matches("\\d*-\\d*/\\d+")) {
 81  13
                                         String[] rangeAndModulus = part.split("/", -1);
 82  13
                                         String[] limits = rangeAndModulus[0].split("-", -1);
 83  13
                                         int from = limits[0].length() == 0 ? 0 : Integer.parseInt(limits[0]);
 84  13
                                         int to = limits[1].length() == 0 ? Integer.MAX_VALUE : Integer.parseInt(limits[1]);
 85  13
                                         int modulus = Integer.parseInt(rangeAndModulus[1]);
 86  13
                                         if (to < from || modulus == 0)
 87  2
                                                 throw new IllegalArgumentException("Invalid pattern: " + part);
 88  11
                                         ranges[i] = new NumberRange(from, to, from % modulus, modulus);
 89  11
                                 } else if (part.matches("\\d*-\\d*[eo]")) {
 90  15
                                         String[] limits = part.substring(0, part.length() - 1).split("-", -1);
 91  15
                                         int from = limits[0].length() == 0 ? 0 : Integer.parseInt(limits[0]);
 92  15
                                         int to = limits[1].length() == 0 ? Integer.MAX_VALUE : Integer.parseInt(limits[1]);
 93  15
                                         if (to < from)
 94  1
                                                 throw new IllegalArgumentException("Invalid pattern: " + part);
 95  14
                                         ranges[i] = new NumberRange(from, to, part.charAt(part.length() - 1) == 'o' ? 1 : 0, 2);
 96  14
                                 } else {
 97  1
                                         throw new IllegalArgumentException("Invalid pattern: " + part);
 98  
                                 }
 99  121
                                 max = Math.max(max, ranges[i].getMax());
 100  121
                                 min = Math.min(min, ranges[i].getMin());
 101  1
                         } catch (NumberFormatException ex) {
 102  1
                                 throw new IllegalArgumentException("Invalid pattern: " + part, ex);
 103  121
                         }
 104  
                 }
 105  87
                 this.max = max;
 106  87
                 this.min = min;
 107  87
         }
 108  
 
 109  
         /**
 110  
          * Check whether this number expression matches the given number.
 111  
          * 
 112  
          * @param number
 113  
          *            the number to check against
 114  
          * @return whether the expression matches the number
 115  
          */
 116  
         public boolean matches(int number) {
 117  1461
                 if (number < min || number > max)
 118  4
                         return false;
 119  8138
                 for (int i = 0; i < ranges.length; i++) {
 120  6887
                         if (ranges[i].matches(number))
 121  206
                                 return true;
 122  
                 }
 123  1251
                 return false;
 124  
         }
 125  
 
 126  
         /**
 127  
          * Return the minimum number that can be matched.
 128  
          */
 129  
         public int getMinimum() {
 130  63
                 return min;
 131  
         }
 132  
 
 133  
         /**
 134  
          * Return the maximum number that can be matched.
 135  
          */
 136  
         public int getMaximum() {
 137  184
                 return max;
 138  
         }
 139  
 
 140  
         private static class NumberRange {
 141  
                 private final int min, max, remainder, modulus;
 142  
 
 143  121
                 NumberRange(int min, int max, int remainder, int modulus) {
 144  121
                         this.min = min;
 145  121
                         this.max = max;
 146  121
                         this.remainder = remainder;
 147  121
                         this.modulus = modulus;
 148  121
                 }
 149  
 
 150  
                 boolean matches(int number) {
 151  6887
                         return number >= min && number <= max && number % modulus == remainder;
 152  
                 }
 153  
 
 154  
                 int getMin() {
 155  121
                         return min;
 156  
                 }
 157  
 
 158  
                 int getMax() {
 159  121
                         return max;
 160  
                 }
 161  
         }
 162  
 }