View Javadoc
1   package org.apache.maven.surefire.report;
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 org.apache.maven.surefire.api.util.internal.ClassMethod;
23  
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.atomic.AtomicInteger;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Creates an index for class/method.
32   * Returns ThreadLocal index if created before.
33   */
34  public final class ClassMethodIndexer
35  {
36      private final AtomicInteger classIndex = new AtomicInteger( 1 );
37      private final AtomicInteger methodIndex = new AtomicInteger( 1 );
38      private final Map<ClassMethod, Long> testIdMapping = new ConcurrentHashMap<>();
39      private final ThreadLocal<Long> testLocalMapping = new ThreadLocal<>();
40  
41      public long indexClassMethod( String clazz, String method )
42      {
43          ClassMethod key = new ClassMethod( requireNonNull( clazz ), method );
44          return testIdMapping.computeIfAbsent( key, cm ->
45          {
46              Long classId = testIdMapping.get( new ClassMethod( requireNonNull( clazz ), null ) );
47              long c = classId == null ? ( ( (long) classIndex.getAndIncrement() ) << 32 ) : classId;
48              int m = method == null ? 0 : methodIndex.getAndIncrement();
49              long id = c | m;
50              testLocalMapping.set( id );
51              return id;
52          } );
53      }
54  
55      public long indexClass( String clazz )
56      {
57          return indexClassMethod( clazz, null );
58      }
59  
60      public Long getLocalIndex()
61      {
62          return testLocalMapping.get();
63      }
64  }