File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\scheduler\BaseParser.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.scheduler;
25
26 import antlr.ANTLRException;
27 import antlr.LLkParser;
28 import antlr.ParserSharedInputState;
29 import antlr.SemanticException;
30 import antlr.Token;
31 import antlr.TokenBuffer;
32 import antlr.TokenStream;
33 import antlr.TokenStreamException;
34
35 /**
36 * @author Kohsuke Kawaguchi
37 */
38 abstract class BaseParser extends LLkParser {
39 // lower/uppser bounds of fields (inclusive)
40 static final int[] LOWER_BOUNDS = new int[] {0,0,1,1,0};
41 static final int[] UPPER_BOUNDS = new int[] {59,23,31,12,7};
42
43 /**
44 * Used to pick a value from within the range
45 */
46 protected Hash hash = Hash.zero();
47
48 protected BaseParser(int i) {
49 super(i);
50 }
51
52 protected BaseParser(ParserSharedInputState parserSharedInputState, int i) {
53 super(parserSharedInputState, i);
54 }
55
56 protected BaseParser(TokenBuffer tokenBuffer, int i) {
57 super(tokenBuffer, i);
58 }
59
60 protected BaseParser(TokenStream tokenStream, int i) {
61 super(tokenStream, i);
62 }
63
64 public void setHash(Hash hash) {
65 if (hash==null) hash = Hash.zero();
66 this.hash = hash;
67 }
68
69 protected long doRange(int start, int end, int step, int field) throws ANTLRException {
70 rangeCheck(start, field);
71 rangeCheck(end, field);
72 if (step <= 0)
73 error(Messages.BaseParser_MustBePositive(step));
74 if (start>end)
75 error(Messages.BaseParser_StartEndReversed(end,start));
76
77 long bits = 0;
78 for (int i = start; i <= end; i += step) {
79 bits |= 1L << i;
80 }
81 return bits;
82 }
83
84 protected long doRange( int step, int field ) throws ANTLRException {
85 return doRange( LOWER_BOUNDS[field], UPPER_BOUNDS[field], step, field );
86 }
87
88 /**
89 * Uses {@link Hash} to choose a random (but stable) value from within this field.
90 */
91 protected long doHash( int field ) {
92 int u = UPPER_BOUNDS[field];
93 if (field==2) u = 28; // day of month can vary depending on month, so to make life simpler, just use [1,28] that's always safe
94 if (field==4) u = 6; // Both 0 and 7 of day of week are Sunday. For better distribution, limit upper bound to 6
95 int h = hash.next(u+1 - LOWER_BOUNDS[field]); // upper bound is inclusive
96 return 1L << (h+LOWER_BOUNDS[field]);
97 }
98
99 protected long doHash( int s, int e ) {
100 return 1L << (s+hash.next(e+1-s));
101 }
102
103 protected void rangeCheck(int value, int field) throws ANTLRException {
104 if( value<LOWER_BOUNDS[field] || UPPER_BOUNDS[field]<value ) {
105 error(Messages.BaseParser_OutOfRange(value,LOWER_BOUNDS[field],UPPER_BOUNDS[field]));
106 }
107 }
108
109 private void error(String msg) throws TokenStreamException, SemanticException {
110 Token token = LT(0);
111 throw new SemanticException(
112 msg,
113 token.getFilename(),
114 token.getLine(),
115 token.getColumn()
116 );
117 }
118
119 protected Hash getHashForTokens() {
120 return HASH_TOKENS ? hash : Hash.zero();
121 }
122
123 /**
124 * This property hashes tokens in the cron tab tokens like @daily so that they spread evenly.
125 * This is more aggressive optimization that changes the semantics, so not on by default.
126 */
127 public static boolean HASH_TOKENS = Boolean.getBoolean(BaseParser.class.getName()+".hash");
128 }
129