<commit_before>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.engine.view.client;

import java.util.Timer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

import javax.time.Instant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opengamma.engine.marketdata.MarketDataInjector;
import com.opengamma.engine.view.ViewComputationResultModel;
import com.opengamma.engine.view.ViewDefinition;
import com.opengamma.engine.view.ViewDeltaResultModel;
import com.opengamma.engine.view.ViewProcessorImpl;
import com.opengamma.engine.view.ViewResultModel;
import com.opengamma.engine.view.calc.EngineResourceReference;
import com.opengamma.engine.view.calc.EngineResourceRetainer;
import com.opengamma.engine.view.calc.ViewCycle;
import com.opengamma.engine.view.client.merging.RateLimitingMergingViewProcessListener;
import com.opengamma.engine.view.compilation.CompiledViewDefinition;
import com.opengamma.engine.view.execution.ViewCycleExecutionOptions;
import com.opengamma.engine.view.execution.ViewExecutionOptions;
import com.opengamma.engine.view.listener.ViewResultListener;
import com.opengamma.engine.view.permission.ViewPermissionProvider;
import com.opengamma.id.UniqueId;
import com.opengamma.id.VersionCorrection;
import com.opengamma.livedata.UserPrincipal;
import com.opengamma.util.ArgumentChecker;

/**
 * Default implementation of {@link ViewClient}.
 */
public class ViewClientImpl implements ViewClient {

  private static final Logger s_logger = LoggerFactory.getLogger(ViewClientImpl.class);

  private final ReentrantLock _clientLock = new ReentrantLock();

  private final UniqueId _id;
  private final ViewProcessorImpl _viewProcessor;
  private final UserPrincipal _user;
  private final EngineResourceRetainer _latestCycleRetainer;

  private final AtomicReference<ViewResultMode> _resultMode = new AtomicReference<ViewResultMode>(ViewResultMode.FULL_ONLY);
  private final AtomicReference<ViewResultMode> _jobResultMode = new AtomicReference<ViewResultMode>(ViewResultMode.FULL_ONLY);

  private final AtomicBoolean _isViewCycleAccessSupported = new AtomicBoolean(false);
  private final AtomicBoolean _isAttached = new AtomicBoolean(false);
  private ViewClientState _state = ViewClientState.STARTED;

  // Per-process state
  private volatile ViewPermissionProvider _permissionProvider;

  @SuppressWarnings("unused")
  private volatile boolean _canAccessCompiledViewDefinition;
  @SuppressWarnings("unused")
  private volatile boolean _canAccessComputationResults;

  private volatile CountDownLatch _completionLatch = new CountDownLatch(0);
  private final AtomicReference<ViewComputationResultModel> _latestResult = new AtomicReference<ViewComputationResultModel>();
  private final AtomicReference<CompiledViewDefinition> _latestCompiledViewDefinition = new AtomicReference<CompiledViewDefinition>();

  private final ViewResultListener _mergedViewProcessListener;<commit_msg>Don't send streaming results by default<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.engine.view.client;

import java.util.Timer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

import javax.time.Instant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opengamma.engine.marketdata.MarketDataInjector;
import com.opengamma.engine.view.ViewComputationResultModel;
import com.opengamma.engine.view.ViewDefinition;
import com.opengamma.engine.view.ViewDeltaResultModel;
import com.opengamma.engine.view.ViewProcessorImpl;
import com.opengamma.engine.view.ViewResultModel;
import com.opengamma.engine.view.calc.EngineResourceReference;
import com.opengamma.engine.view.calc.EngineResourceRetainer;
import com.opengamma.engine.view.calc.ViewCycle;
import com.opengamma.engine.view.client.merging.RateLimitingMergingViewProcessListener;
import com.opengamma.engine.view.compilation.CompiledViewDefinition;
import com.opengamma.engine.view.execution.ViewCycleExecutionOptions;
import com.opengamma.engine.view.execution.ViewExecutionOptions;
import com.opengamma.engine.view.listener.ViewResultListener;
import com.opengamma.engine.view.permission.ViewPermissionProvider;
import com.opengamma.id.UniqueId;
import com.opengamma.id.VersionCorrection;
import com.opengamma.livedata.UserPrincipal;
import com.opengamma.util.ArgumentChecker;

/**
 * Default implementation of {@link ViewClient}.
 */
public class ViewClientImpl implements ViewClient {

  private static final Logger s_logger = LoggerFactory.getLogger(ViewClientImpl.class);

  private final ReentrantLock _clientLock = new ReentrantLock();

  private final UniqueId _id;
  private final ViewProcessorImpl _viewProcessor;
  private final UserPrincipal _user;
  private final EngineResourceRetainer _latestCycleRetainer;

  private final AtomicReference<ViewResultMode> _resultMode = new AtomicReference<ViewResultMode>(ViewResultMode.FULL_ONLY);
  private final AtomicReference<ViewResultMode> _jobResultMode = new AtomicReference<ViewResultMode>(ViewResultMode.NONE);

  private final AtomicBoolean _isViewCycleAccessSupported = new AtomicBoolean(false);
  private final AtomicBoolean _isAttached = new AtomicBoolean(false);
  private ViewClientState _state = ViewClientState.STARTED;

  // Per-process state
  private volatile ViewPermissionProvider _permissionProvider;

  @SuppressWarnings("unused")
  private volatile boolean _canAccessCompiledViewDefinition;
  @SuppressWarnings("unused")
  private volatile boolean _canAccessComputationResults;

  private volatile CountDownLatch _completionLatch = new CountDownLatch(0);
  private final AtomicReference<ViewComputationResultModel> _latestResult = new AtomicReference<ViewComputationResultModel>();
  private final AtomicReference<CompiledViewDefinition> _latestCompiledViewDefinition = new AtomicReference<CompiledViewDefinition>();

  private final ViewResultListener _mergedViewProcessListener;
{'fraction_non_alphanumeric': 0.07653779934262013, 'fraction_numerical': 0.0021912662388480202, 'mean_word_length': 7.234536082474227, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4765487', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1737, 'n_words': 362}