1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.surefire.junitcore.pc;
20
21 /**
22 * @author Tibor Digana (tibor17)
23 * @see Balancer
24 * @since 2.16
25 */
26 public class BalancerFactory {
27 private BalancerFactory() {}
28
29 /**
30 * Infinite permits.
31 * @return Balancer wih infinite permits
32 */
33 public static Balancer createInfinitePermitsBalancer() {
34 return balancer(0, false);
35 }
36
37 /**
38 * Balancer without fairness.
39 * Fairness guarantees the waiting schedulers to wake up in order they acquired a permit.
40 *
41 * @param concurrency number of permits to acquire when maintaining concurrency on tests
42 * @return Balancer with given number of permits
43 */
44 public static Balancer createBalancer(int concurrency) {
45 return balancer(concurrency, false);
46 }
47
48 /**
49 * Balancer with fairness.
50 * Fairness guarantees the waiting schedulers to wake up in order they acquired a permit.
51 *
52 * @param concurrency number of permits to acquire when maintaining concurrency on tests
53 * @return Balancer with given number of permits
54 */
55 public static Balancer createBalancerWithFairness(int concurrency) {
56 return balancer(concurrency, true);
57 }
58
59 private static Balancer balancer(int concurrency, boolean fairness) {
60 boolean shouldBalance = concurrency > 0 && concurrency < Integer.MAX_VALUE;
61 return shouldBalance ? new ThreadResourcesBalancer(concurrency, fairness) : new NullBalancer();
62 }
63 }