<commit_before>/*
 * Copyright 2013-present Facebook, Inc.
 *
 * 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.buck.model;

import java.lang.ref.WeakReference;
import java.util.Objects;
import javax.annotation.Nullable;

/**
 * Simple type representing a pair of differently typed values.
 *
 * <p>Used to represent pair-like structures in coerced values.
 */
public class Pair<FIRST, SECOND> {

  private FIRST first;
  private SECOND second;

  @Nullable private WeakReference<Integer> hashCache = null;
  @Nullable private WeakReference<String> stringCache = null;

  public Pair(FIRST first, SECOND second) {
    this.first = first;
    this.second = second;
  }

  public FIRST getFirst() {
    return first;
  }

  public SECOND getSecond() {
    return second;
  }

  @Override
  public boolean equals(Object other) {
    if (!(other instanceof Pair)) {
      return false;
    }

    Pair<?, ?> that = (Pair<?, ?>) other;
    return Objects.equals(this.first, that.first) && Objects.equals(this.second, that.second);
  }

  @Override
  public int hashCode() {
    synchronized (this) {
      if (hashCache == null) {
        return calculateHashAndCache();
      }
      Integer hash = hashCache.get();
      if (hash == null) {
        return calculateHashAndCache();
      }
      return hash;
    }
  }

  private int calculateHashAndCache() {
    int hash = Objects.hash(first, second);
    hashCache = new WeakReference<>(hash);
    return hash;
  }

  @Override
  public String toString() {
    synchronized (this) {
      if (stringCache == null) {
        return createStringAndCache();
      }
      String string = stringCache.get();<commit_msg>Better hashCode caching in Pair<commit_after>/*
 * Copyright 2013-present Facebook, Inc.
 *
 * 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.buck.model;

import java.lang.ref.WeakReference;
import java.util.Objects;
import javax.annotation.Nullable;

/**
 * Simple type representing a pair of differently typed values.
 *
 * <p>Used to represent pair-like structures in coerced values.
 */
public class Pair<FIRST, SECOND> {

  private FIRST first;
  private SECOND second;

  private volatile boolean hashCodeComputed = false;
  private int hashCode;
  @Nullable private WeakReference<String> stringCache = null;

  public Pair(FIRST first, SECOND second) {
    this.first = first;
    this.second = second;
  }

  public FIRST getFirst() {
    return first;
  }

  public SECOND getSecond() {
    return second;
  }

  @Override
  public boolean equals(Object other) {
    if (!(other instanceof Pair)) {
      return false;
    }

    Pair<?, ?> that = (Pair<?, ?>) other;
    return Objects.equals(this.first, that.first) && Objects.equals(this.second, that.second);
  }

  @Override
  public int hashCode() {
    if (!hashCodeComputed) {
      synchronized (this) {
        if (!hashCodeComputed) {
          hashCode = Objects.hash(first, second);
          hashCodeComputed = true;
        }
      }
    }

    return hashCode;
  }

  @Override
  public String toString() {
    synchronized (this) {
      if (stringCache == null) {
        return createStringAndCache();
      }
      String string = stringCache.get();
{'fraction_non_alphanumeric': 0.0996708979783733, 'fraction_numerical': 0.0037611659614480487, 'mean_word_length': 3.1310679611650487, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3853453', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1170, 'n_words': 418}