<commit_before>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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 com.facebook.litho;

import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
import androidx.collection.SimpleArrayMap;
import java.util.HashMap;
import java.util.Map;

/** A handler that stores the range status of components with given working range. */
public class WorkingRangeStatusHandler {

  @IntDef({STATUS_UNINITIALIZED, STATUS_IN_RANGE, STATUS_OUT_OF_RANGE})
  public @interface WorkingRangeStatus {}

  static final int STATUS_UNINITIALIZED = 0;
  static final int STATUS_IN_RANGE = 1;
  static final int STATUS_OUT_OF_RANGE = 2;

  /**
   * Use a {@link SimpleArrayMap} to store status of components with given working range. The key of
   * the container is combined with the component's global key and the working range name.
   *
   * <p>The global key guarantees the uniqueness of the component in a ComponentTree, and it's
   * consistent across different {@link LayoutState}s. The working range name is used to find the
   * specific working range since a component can have several working ranges. The value is an
   * integer presenting the status.
   */
  private final Map<String, Integer> mStatus = new HashMap<>();

  boolean isInRange(String name, Component component, String globalKey) {
    return getStatus(name, component, globalKey) == STATUS_IN_RANGE;
  }

  /** Components in the collection share same status, we can only check the first component. */
  @WorkingRangeStatus
  private int getStatus(String name, Component component, String componentGlobalKey) {
    final String globalKey = ComponentUtils.getGlobalKey(component, componentGlobalKey);
    final String key = generateKey(name, globalKey);
    if (mStatus.containsKey(key)) {
      return mStatus.get(key);
    }

    return STATUS_UNINITIALIZED;
  }

  void setEnteredRangeStatus(String name, Component component, String globalKey) {
    setStatus(name, component, globalKey, STATUS_IN_RANGE);
  }

  void setExitedRangeStatus(String name, Component component, String globalKey) {
    setStatus(name, component, globalKey, STATUS_OUT_OF_RANGE);
  }

  void clear() {
    mStatus.clear();
  }

  @VisibleForTesting
  Map<String, Integer> getStatus() {
    return mStatus;
  }

  @VisibleForTesting
  void setStatus(
      String name, Component component, String componentGlobalKey, @WorkingRangeStatus int status) {
    final String globalKey = ComponentUtils.getGlobalKey(component, componentGlobalKey);
    mStatus.put(generateKey(name, globalKey), status);
  }

  private static String generateKey(String name, String globalKey) {
    return name + "_" + globalKey;
  }<commit_msg>Fixes stateless config race condition [17/n]<commit_after>/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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 com.facebook.litho;

import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
import androidx.collection.SimpleArrayMap;
import java.util.HashMap;
import java.util.Map;

/** A handler that stores the range status of components with given working range. */
public class WorkingRangeStatusHandler {

  @IntDef({STATUS_UNINITIALIZED, STATUS_IN_RANGE, STATUS_OUT_OF_RANGE})
  public @interface WorkingRangeStatus {}

  static final int STATUS_UNINITIALIZED = 0;
  static final int STATUS_IN_RANGE = 1;
  static final int STATUS_OUT_OF_RANGE = 2;

  /**
   * Use a {@link SimpleArrayMap} to store status of components with given working range. The key of
   * the container is combined with the component's global key and the working range name.
   *
   * <p>The global key guarantees the uniqueness of the component in a ComponentTree, and it's
   * consistent across different {@link LayoutState}s. The working range name is used to find the
   * specific working range since a component can have several working ranges. The value is an
   * integer presenting the status.
   */
  private final Map<String, Integer> mStatus = new HashMap<>();

  boolean isInRange(String name, Component component, String globalKey) {
    return getStatus(name, component, globalKey) == STATUS_IN_RANGE;
  }

  /** Components in the collection share same status, we can only check the first component. */
  @WorkingRangeStatus
  private int getStatus(String name, Component component, String componentGlobalKey) {
    final String key = generateKey(name, componentGlobalKey);
    if (mStatus.containsKey(key)) {
      return mStatus.get(key);
    }

    return STATUS_UNINITIALIZED;
  }

  void setEnteredRangeStatus(String name, Component component, String globalKey) {
    setStatus(name, component, globalKey, STATUS_IN_RANGE);
  }

  void setExitedRangeStatus(String name, Component component, String globalKey) {
    setStatus(name, component, globalKey, STATUS_OUT_OF_RANGE);
  }

  void clear() {
    mStatus.clear();
  }

  @VisibleForTesting
  Map<String, Integer> getStatus() {
    return mStatus;
  }

  @VisibleForTesting
  void setStatus(
      String name, Component component, String componentGlobalKey, @WorkingRangeStatus int status) {
    mStatus.put(generateKey(name, componentGlobalKey), status);
  }

  private static String generateKey(String name, String globalKey) {
    return name + "_" + globalKey;
  }
{'fraction_non_alphanumeric': 0.07747917648907748, 'fraction_numerical': 0.0025145371680025145, 'mean_word_length': 4.281327800829875, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1795115', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1711, 'n_words': 657}