public class

ThreadFactoryBuilder

extends java.lang.Object

 java.lang.Object

↳androidx.test.espresso.util.concurrent.ThreadFactoryBuilder

Gradle dependencies

compile group: 'androidx.test.espresso', name: 'espresso-core', version: '3.6.1'

  • groupId: androidx.test.espresso
  • artifactId: espresso-core
  • version: 3.6.1

Artifact androidx.test.espresso:espresso-core:3.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso:espresso-core com.android.support.test.espresso:espresso-core

Overview

A minimal implementation of Guava's ThreadFactoryBuilder

Summary

Constructors
publicThreadFactoryBuilder()

Methods
public java.util.concurrent.ThreadFactorybuild()

public ThreadFactoryBuildersetNameFormat(java.lang.String nameFormat)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public ThreadFactoryBuilder()

Methods

public ThreadFactoryBuilder setNameFormat(java.lang.String nameFormat)

public java.util.concurrent.ThreadFactory build()

Source

/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.test.espresso.util.concurrent;

import static androidx.test.internal.util.Checks.checkNotNull;

import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

/**
 * A minimal implementation of Guava's ThreadFactoryBuilder
 *
 * @hide
 */
@RestrictTo(Scope.LIBRARY)
public class ThreadFactoryBuilder {

  private String nameFormat;

  public ThreadFactoryBuilder setNameFormat(String nameFormat) {
    String unused = format(nameFormat, 0); // fail fast if the format is bad or null
    this.nameFormat = nameFormat;
    return this;
  }

  public ThreadFactory build() {
    // espresso usages currently always set nameFormat, so lets just assert its nonnull to
    // simplify logic
    checkNotNull(nameFormat);
    ThreadFactory backingFactory = Executors.defaultThreadFactory();
    AtomicLong count = new AtomicLong(0);
    return r -> {
      Thread thread = backingFactory.newThread(r);
      thread.setName(format(nameFormat, count.getAndIncrement()));
      return thread;
    };
  }

  private static String format(String format, Object... args) {
    return String.format(Locale.ROOT, format, args);
  }
}