View Javadoc
1   package org.apache.maven.plugin.surefire.booterclient;
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  import java.util.Queue;
23  import java.util.concurrent.ConcurrentLinkedQueue;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  /**
27   * A bucket from which fork numbers can be drawn. Any drawn number needs to be returned to the bucket, in order to keep
28   * the range of provided values delivered as small as possible.
29   *
30   * @author Andreas Gudian
31   */
32  public class ForkNumberBucket
33  {
34  
35      private static final ForkNumberBucket INSTANCE = new ForkNumberBucket();
36  
37      private Queue<Integer> qFree = new ConcurrentLinkedQueue<Integer>();
38  
39      private AtomicInteger highWaterMark = new AtomicInteger( 1 );
40  
41      /**
42       * Non-public constructor
43       */
44      protected ForkNumberBucket()
45      {
46      }
47  
48      /**
49       * @return a fork number that is not currently in use. The value must be returned to the bucket using
50       *         {@link #returnNumber(int)}.
51       */
52      public static int drawNumber()
53      {
54          return getInstance().drawNumberInternal();
55      }
56  
57      /**
58       * @param number the number to return to the bucket so that it can be reused.
59       */
60      public static void returnNumber( int number )
61      {
62          getInstance().returnNumberInternal( number );
63      }
64  
65      /**
66       * @return a singleton instance
67       */
68      private static ForkNumberBucket getInstance()
69      {
70          return INSTANCE;
71      }
72  
73      /**
74       * @return a fork number that is not currently in use. The value must be returned to the bucket using
75       *         {@link #returnNumber(int)}.
76       */
77      protected int drawNumberInternal()
78      {
79          Integer nextFree = qFree.poll();
80  
81          if ( null == nextFree )
82          {
83              return highWaterMark.getAndIncrement();
84          }
85          else
86          {
87              return nextFree.intValue();
88          }
89      }
90  
91      /**
92       * @return the highest number that has been drawn
93       */
94      protected int getHighestDrawnNumber()
95      {
96          return highWaterMark.get() - 1;
97      }
98  
99      /**
100      * @param number the number to return to the bucket so that it can be reused.
101      */
102     protected void returnNumberInternal( int number )
103     {
104         qFree.add( Integer.valueOf( number ) );
105     }
106 }