<commit_before>/*
 * Copyright 2017, Google 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.google.instrumentation.stats;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.instrumentation.common.Clock;
import com.google.instrumentation.common.Function;
import com.google.instrumentation.stats.MutableView.MutableDistributionView;
import com.google.instrumentation.stats.MutableView.MutableIntervalView;
import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor;
import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor;
import java.util.Collection;
import javax.annotation.concurrent.GuardedBy;

/**
 * A class that stores a singleton map from {@link MeasurementDescriptor.Name}s to {@link
 * MutableView}s.
 */
final class MeasurementDescriptorToViewMap {

  /*
   * A synchronized singleton map that stores the one-to-many mapping from MeasurementDescriptors
   * to MutableViews.
   */
  @GuardedBy("this")
  private final Multimap<MeasurementDescriptor.Name, MutableView> mutableMap =
      HashMultimap.<MeasurementDescriptor.Name, MutableView>create();

  /** Returns a {@link View} corresponding to the given {@link ViewDescriptor}. */
  synchronized View getView(ViewDescriptor viewDescriptor, Clock clock) {
    Collection<MutableView> views =
        mutableMap.get(viewDescriptor.getMeasurementDescriptor().getMeasurementDescriptorName());
    for (MutableView view : views) {
      if (view.getViewDescriptor().equals(viewDescriptor)) {
        return view.toView(clock);
      }
    }
    return null;
  }

  private synchronized MutableView getMutableView(ViewDescriptor viewDescriptor) {
    Collection<MutableView> views =
        mutableMap.get(viewDescriptor.getMeasurementDescriptor().getMeasurementDescriptorName());
    for (MutableView view : views) {
      if (view.getViewDescriptor().equals(viewDescriptor)) {
        return view;
      }
    }
    return null;
  }

  /** Enable stats collection for the given {@link ViewDescriptor}. */<commit_msg>Refactor getView to call getMutableView.<commit_after>/*
 * Copyright 2017, Google 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.google.instrumentation.stats;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.instrumentation.common.Clock;
import com.google.instrumentation.common.Function;
import com.google.instrumentation.stats.MutableView.MutableDistributionView;
import com.google.instrumentation.stats.MutableView.MutableIntervalView;
import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor;
import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor;
import java.util.Collection;
import javax.annotation.concurrent.GuardedBy;

/**
 * A class that stores a singleton map from {@link MeasurementDescriptor.Name}s to {@link
 * MutableView}s.
 */
final class MeasurementDescriptorToViewMap {

  /*
   * A synchronized singleton map that stores the one-to-many mapping from MeasurementDescriptors
   * to MutableViews.
   */
  @GuardedBy("this")
  private final Multimap<MeasurementDescriptor.Name, MutableView> mutableMap =
      HashMultimap.<MeasurementDescriptor.Name, MutableView>create();

  /** Returns a {@link View} corresponding to the given {@link ViewDescriptor}. */
  synchronized View getView(ViewDescriptor viewDescriptor, Clock clock) {
    MutableView view = getMutableView(viewDescriptor);
    return view == null ? null : view.toView(clock);
  }

  private synchronized MutableView getMutableView(ViewDescriptor viewDescriptor) {
    Collection<MutableView> views =
        mutableMap.get(viewDescriptor.getMeasurementDescriptor().getMeasurementDescriptorName());
    for (MutableView view : views) {
      if (view.getViewDescriptor().equals(viewDescriptor)) {
        return view;
      }
    }
    return null;
  }

  /** Enable stats collection for the given {@link ViewDescriptor}. */
{'fraction_non_alphanumeric': 0.08890652907322881, 'fraction_numerical': 0.0031752331811867435, 'mean_word_length': 4.6, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268872', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1292, 'n_words': 393}