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

import java.util.Collection;
import java.util.HashSet;

import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeField;
import org.fudgemsg.MutableFudgeMsg;

import com.google.common.collect.Sets;
import com.opengamma.livedata.server.FieldHistoryStore;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.fudgemsg.OpenGammaFudgeContext;

/**
 * Strips all fields out of the message except the ones you want to explicitly accept.
 * <p>
 * If no field is accepted, the message is extinguished. 
 */
public class FieldFilter implements NormalizationRule {
  
  private final Collection<String> _fieldsToAccept;
  private final FudgeContext _context;
  
  public FieldFilter(String... fieldsToAccept) {
    this(OpenGammaFudgeContext.getInstance(), fieldsToAccept);
  }
  
  public FieldFilter(FudgeContext context, String... fieldsToAccept) {
    this(Sets.newHashSet(fieldsToAccept), context);
  }
  
  public FieldFilter(Collection<String> fieldsToAccept) {
    this(fieldsToAccept, OpenGammaFudgeContext.getInstance());
  }

  public FieldFilter(Collection<String> fieldsToAccept, FudgeContext fudgeContext) {
    ArgumentChecker.notNull(fieldsToAccept, "fieldsToAccept");
    ArgumentChecker.notNull(fudgeContext, "fudgeContext");
    _fieldsToAccept = new HashSet<String>(fieldsToAccept);
    _context = fudgeContext;
  }

  /**
   * @return the context
   */
  public FudgeContext getContext() {
    return _context;
  }

  @Override
  public MutableFudgeMsg apply(
      MutableFudgeMsg msg,
      FieldHistoryStore fieldHistory) {
    
    MutableFudgeMsg normalizedMsg = getContext().newMessage();
    // REVIEW kirk 2010-04-15 -- Run through the fields in the order of the
    // original message and check for containment in _fieldsToAccept as it's
    // faster for large messages.
    // It also supports multiple values with the same name.
    for (FudgeField field : msg.getAllFields()) {
      if (field.getName() == null) {
        // Don't allow non-named fields.
        continue;
      }
      if (!_fieldsToAccept.contains(field.getName())) {
        continue;
      }
      normalizedMsg.add(field);
    }
    
    if (normalizedMsg.getAllFields().isEmpty()) {
      return null; // extinguish message
    }
    
    return normalizedMsg;<commit_msg>Iterate directly on the message, rather than wrapping it with an unnecesary call<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.livedata.normalization;

import java.util.Collection;
import java.util.HashSet;

import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeField;
import org.fudgemsg.MutableFudgeMsg;

import com.google.common.collect.Sets;
import com.opengamma.livedata.server.FieldHistoryStore;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.fudgemsg.OpenGammaFudgeContext;

/**
 * Strips all fields out of the message except the ones you want to explicitly accept.
 * <p>
 * If no field is accepted, the message is extinguished. 
 */
public class FieldFilter implements NormalizationRule {
  
  private final Collection<String> _fieldsToAccept;
  private final FudgeContext _context;
  
  public FieldFilter(String... fieldsToAccept) {
    this(OpenGammaFudgeContext.getInstance(), fieldsToAccept);
  }
  
  public FieldFilter(FudgeContext context, String... fieldsToAccept) {
    this(Sets.newHashSet(fieldsToAccept), context);
  }
  
  public FieldFilter(Collection<String> fieldsToAccept) {
    this(fieldsToAccept, OpenGammaFudgeContext.getInstance());
  }

  public FieldFilter(Collection<String> fieldsToAccept, FudgeContext fudgeContext) {
    ArgumentChecker.notNull(fieldsToAccept, "fieldsToAccept");
    ArgumentChecker.notNull(fudgeContext, "fudgeContext");
    _fieldsToAccept = new HashSet<String>(fieldsToAccept);
    _context = fudgeContext;
  }

  /**
   * @return the context
   */
  public FudgeContext getContext() {
    return _context;
  }

  @Override
  public MutableFudgeMsg apply(
      MutableFudgeMsg msg,
      FieldHistoryStore fieldHistory) {
    
    MutableFudgeMsg normalizedMsg = getContext().newMessage();
    // REVIEW kirk 2010-04-15 -- Run through the fields in the order of the
    // original message and check for containment in _fieldsToAccept as it's
    // faster for large messages.
    // It also supports multiple values with the same name.
    for (FudgeField field : msg) {
      if (field.getName() == null) {
        // Don't allow non-named fields.
        continue;
      }
      if (!_fieldsToAccept.contains(field.getName())) {
        continue;
      }
      normalizedMsg.add(field);
    }
    
    if (normalizedMsg.getAllFields().isEmpty()) {
      return null; // extinguish message
    }
    
    return normalizedMsg;
{'fraction_non_alphanumeric': 0.09304174950298211, 'fraction_numerical': 0.004771371769383698, 'mean_word_length': 3.684357541899441, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4233178', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1438, 'n_words': 383}