File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\Resource.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.model;
25
26 /**
27 * Represents things that {@link Queue.Executable} uses while running.
28 *
29 * <p>
30 * This is used in {@link Queue} to support basic mutual exclusion/locks. If two
31 * {@link Queue.Task}s require the same {@link Resource}, they will not
32 * be run at the same time.
33 *
34 * <p>
35 * Resources are compared by using their {@link #displayName names}, and
36 * need not have the "same object" semantics.
37 *
38 * @since 1.121
39 */
40 public final class Resource {
41 /**
42 * Human-readable name of this resource.
43 * Used for rendering HTML.
44 */
45 public final String displayName;
46
47 /**
48 * Parent resource.
49 *
50 * <p>
51 * A child resource is considered a part of the parent resource,
52 * so acquiring the parent resource always imply acquiring all
53 * the child resources.
54 */
55 public final Resource parent;
56
57 /**
58 * Maximum number of concurrent write.
59 */
60 public final int numConcurrentWrite;
61
62 public Resource(Resource parent, String displayName) {
63 this(parent,displayName,1);
64 }
65
66 /**
67 * @since 1.155
68 */
69 public Resource(Resource parent, String displayName, int numConcurrentWrite) {
70 if(numConcurrentWrite<1)
71 throw new IllegalArgumentException();
72
73 this.parent = parent;
74 this.displayName = displayName;
75 this.numConcurrentWrite = numConcurrentWrite;
76 }
77
78 public Resource(String displayName) {
79 this(null,displayName);
80 }
81
82 /**
83 * Checks the resource collision.
84 *
85 * @param count
86 * If we are testing W/W conflict, total # of write counts.
87 * For R/W conflict test, this value should be set to {@link Integer#MAX_VALUE}.
88 */
89 public boolean isCollidingWith(Resource that, int count) {
90 assert that!=null;
91 for(Resource r=that; r!=null; r=r.parent)
92 if(this.equals(r) && r.numConcurrentWrite<count)
93 return true;
94 for(Resource r=this; r!=null; r=r.parent)
95 if(that.equals(r) && r.numConcurrentWrite<count)
96 return true;
97 return false;
98 }
99
100 @Override
101 public boolean equals(Object o) {
102 if (this == o) return true;
103 if (o == null || getClass() != o.getClass()) return false;
104 Resource that = (Resource) o;
105
106 return displayName.equals(that.displayName) && eq(this.parent,that.parent);
107 }
108
109 private static boolean eq(Object lhs,Object rhs) {
110 if(lhs==rhs) return true;
111 if(lhs==null || rhs==null) return false;
112 return lhs.equals(rhs);
113 }
114
115 @Override
116 public int hashCode() {
117 return displayName.hashCode();
118 }
119
120 @Override
121 public String toString() {
122 StringBuilder buf = new StringBuilder();
123 if(parent!=null)
124 buf.append(parent).append('/');
125 buf.append(displayName).append('(').append(numConcurrentWrite).append(')');
126 return buf.toString();
127 }
128 }
129