<commit_before>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.engine.livedata;

import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang.ObjectUtils;

import com.opengamma.engine.security.Security;
import com.opengamma.engine.security.SecurityMaster;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.value.ValueRequirementNames;
import com.opengamma.id.Identifier;
import com.opengamma.id.IdentificationScheme;
import com.opengamma.util.ArgumentChecker;

/**
 * Data provider for live data.
 */
public class DomainLiveDataAvailabilityProvider implements LiveDataAvailabilityProvider {

  /**
   * The security master to resolve against.
   */
  private final SecurityMaster _securityMaster;
  /**
   * The list of acceptable schemes.
   */
  private final Collection<IdentificationScheme> _acceptableSchemes;

  /**
   * Creates a provider.
   * @param secMaster  the security master, not null
   * @param acceptableSchemes  the acceptable schemes, not null
   */
  public DomainLiveDataAvailabilityProvider(SecurityMaster secMaster, IdentificationScheme... acceptableSchemes) {
    ArgumentChecker.notNull(secMaster, "Security master");
    ArgumentChecker.notNull(acceptableSchemes, "Available domains");
    _securityMaster = secMaster;
    _acceptableSchemes = Arrays.asList(acceptableSchemes);
  }

  //-------------------------------------------------------------------------
  @Override
  public boolean isAvailable(ValueRequirement requirement) {
    if (!ObjectUtils.equals(ValueRequirementNames.MARKET_DATA_HEADER, requirement.getValueName())) {
      return false;
    }
    switch (requirement.getTargetSpecification().getType()) {
      case PRIMITIVE: {
        IdentificationScheme scheme = requirement.getTargetSpecification().getIdentifier().getScheme();
        return _acceptableSchemes.contains(scheme);
      }
      case SECURITY: {
        Security security = _securityMaster.getSecurity(requirement.getTargetSpecification().getUniqueIdentifier());
        if (security != null) {
          for (Identifier identifier : security.getIdentifiers()) {
            if (_acceptableSchemes.contains(identifier.getScheme())) {
              return true;
            }
          }
        }
        return false;
      }
      default:<commit_msg>[ENG-33] a list taking constructor allows spring based construction<commit_after>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.engine.livedata;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang.ObjectUtils;

import com.opengamma.engine.security.Security;
import com.opengamma.engine.security.SecurityMaster;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.value.ValueRequirementNames;
import com.opengamma.id.IdentificationScheme;
import com.opengamma.id.Identifier;
import com.opengamma.util.ArgumentChecker;

/**
 * Data provider for live data.
 */
public class DomainLiveDataAvailabilityProvider implements LiveDataAvailabilityProvider {

  /**
   * The security master to resolve against.
   */
  private final SecurityMaster _securityMaster;
  /**
   * The list of acceptable schemes.
   */
  private final Collection<IdentificationScheme> _acceptableSchemes;

  /**
   * Creates a provider.
   * @param secMaster  the security master, not null
   * @param acceptableSchemes  the acceptable schemes, not null
   */
  public DomainLiveDataAvailabilityProvider(SecurityMaster secMaster, IdentificationScheme... acceptableSchemes) {
    this (secMaster, Arrays.asList (acceptableSchemes));
  }
  
  /**
   * Creates a provider.
   * @param secMaster the security master, not null
   * @param acceptableSchemes the acceptable schemes, not null
   */
  public DomainLiveDataAvailabilityProvider (final SecurityMaster secMaster, final Collection<IdentificationScheme> acceptableSchemes) {
    ArgumentChecker.notNull(secMaster, "Security master");
    ArgumentChecker.notNull(acceptableSchemes, "Acceptable schemes");
    _securityMaster = secMaster;
    _acceptableSchemes = new ArrayList<IdentificationScheme> (acceptableSchemes);
  }

  //-------------------------------------------------------------------------
  @Override
  public boolean isAvailable(ValueRequirement requirement) {
    if (!ObjectUtils.equals(ValueRequirementNames.MARKET_DATA_HEADER, requirement.getValueName())) {
      return false;
    }
    switch (requirement.getTargetSpecification().getType()) {
      case PRIMITIVE: {
        IdentificationScheme scheme = requirement.getTargetSpecification().getIdentifier().getScheme();
        return _acceptableSchemes.contains(scheme);
      }
      case SECURITY: {
        Security security = _securityMaster.getSecurity(requirement.getTargetSpecification().getUniqueIdentifier());
        if (security != null) {
          for (Identifier identifier : security.getIdentifiers()) {
            if (_acceptableSchemes.contains(identifier.getScheme())) {
              return true;
            }
          }
        }
        return false;
      }
      default:
{'fraction_non_alphanumeric': 0.11773671175461993, 'fraction_numerical': 0.003429224614212231, 'mean_word_length': 3.419191919191919, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4629077', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1321, 'n_words': 308}