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