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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2012, CloudBees, Inc.
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 java.io.UnsupportedEncodingException;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.Random;
30
31 /**
32 * Generates a pseudo-random sequence of integers in the specified range.
33 *
34 * <p>
35 * {@link CronTab} supports tokens like '@daily', which means "do it once a day".
36 * Exactly which time of the day this gets scheduled is randomized --- randomized
37 * in the sense that it's spread out when many jobs choose @daily, but it's at
38 * the same time stable so that every job sticks to a specific time of the day
39 * even after the configuration is updated.
40 *
41 * <p>
42 *
43 *
44 * @author Kohsuke Kawaguchi
45 * @since 1.448
46 */
47 public abstract class Hash {
48 /*package*/ Hash() {}
49
50 /**
51 * Produces an integer in [0,n)
52 */
53 public abstract int next(int n);
54
55 public static Hash from(String seed) {
56 try {
57 MessageDigest md5 = MessageDigest.getInstance("MD5");
58 md5.update(seed.getBytes("UTF-8"));
59 byte[] digest = md5.digest();
60
61 for (int i=8; i<digest.length; i++)
62 digest[i%8] ^= digest[i];
63
64 long l = 0;
65 for (int i=0; i<8; i++)
66 l = (l<<8)+(digest[i]&0xFF);
67
68 final Random rnd = new Random(l);
69 return new Hash() {
70 @Override
71 public int next(int n) {
72 return rnd.nextInt(n);
73 }
74 };
75 } catch (NoSuchAlgorithmException e) {
76 throw new AssertionError(e); // MD5 is a part of JRE
77 } catch (UnsupportedEncodingException e) {
78 throw new AssertionError(e); // UTF-8 is mandatory
79 }
80 }
81
82 /**
83 * Creates a hash that always return 0.
84 */
85 public static Hash zero() {
86 return ZERO;
87 }
88
89 private static final Hash ZERO = new Hash() {
90 @Override
91 public int next(int n) {
92 return 0;
93 }
94 };
95 }
96