<commit_before>/**
 * @license
 * Copyright 2019 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.dao',
  name: 'OptionDAO',
  extends: 'foam.dao.ProxyDAO',

  documentation: `
    OptionDAO allows for two different dao branches based on a predicate evaluation.
    Currently only supports put_ and  remove_.
  `,

  javaImports: [
    'foam.dao.DAO',
    'foam.dao.NullDAO'
  ],

  properties: [
    {
      class: 'Proxy',
      of: 'foam.dao.DAO',
      name: 'optionDelegate',
      forwards: [ 'put_', 'remove_', 'find_', 'select_', 'removeAll_', 'cmd_', 'listen_' ],
      topics: [ 'on' ], // TODO: Remove this when all users of it are updated.
      factory: function() { return this.NullDAO.create() },
      postSet: function(old, nu) {
        if ( old ) this.on.reset.pub();
      },
      swiftFactory: 'return NullDAO_create()',
      swiftPostSet: `
        if let oldValue = oldValue as? foam_dao_AbstractDAO {
          _ = oldValue.on["reset"].pub()
        }
      `
      ,
    },
    {
      class: 'foam.mlang.predicate.PredicateProperty',
      name: 'optionPredicate',
      factory: function () {
        return foam.mlang.predicate.True.create();
      },
      javaFactory: `
      return foam.mlang.MLang.TRUE;
      `,
      documentation: 'OptionDAO.optionPredicate is checked against an object; if returns true, OptionDAO.optionDelegate is used.'+
      'Default uses OptionDAO.delegate.'
    },
  ],

  methods: [
    {
      name: 'put_',
      javaCode: `
        if  ( getOptionPredicate().f(obj) ) {
          DAO optionDelegate = (DAO) getOptionDelegate();

          return optionDelegate.put_(x, obj);
        }

        return getDelegate().put_(x, obj);
      `
    },
    {
      name: 'remove_',
      javaCode: `
        if  ( getOptionPredicate().f(obj) ) {
          DAO optionDelegate = (DAO) getOptionDelegate();

          return optionDelegate.remove_(x, obj);
        }

        return getDelegate().remove_(x, obj);
      `
    },
  ],
});<commit_msg>Refactored optionDAO<commit_after>/**
 * @license
 * Copyright 2020 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.dao',
  name: 'OptionDAO',
  extends: 'foam.dao.ProxyDAO',

  documentation: `
    OptionDAO allows the user to pass a delegate to either the default delegate (OptionDAO.delegate) or
    the option Delegate (OptionDAO.optionDelegate). Passing to the option delegate will occur if the
    OptionDAO.optionPredicate evaluates to true.

    To use, add optionDAO as the delegate of the proxyDAO you want to start branching from.

    Currently only supports put_ and remove_.
  `,

  javaImports: [
    'foam.dao.DAO',
    'foam.dao.NullDAO'
  ],

  properties: [
    {
      class: 'Proxy',
      of: 'foam.dao.DAO',
      name: 'optionDelegate',
      forwards: [ 'put_', 'remove_', 'find_', 'select_', 'removeAll_', 'cmd_', 'listen_' ],
      topics: [ 'on' ], // TODO: Remove this when all users of it are updated.
      factory: function() { return this.NullDAO.create() },
      postSet: function(old, nu) {
        if ( old ) this.on.reset.pub();
      },
      swiftFactory: 'return NullDAO_create()',
      swiftPostSet: `
        if let oldValue = oldValue as? foam_dao_AbstractDAO {
          _ = oldValue.on["reset"].pub()
        }
      `
      ,
    },
    {
      class: 'foam.mlang.predicate.PredicateProperty',
      name: 'optionPredicate',
      factory: function () {
        return foam.mlang.predicate.True.create();
      },
      javaFactory: `
      return foam.mlang.MLang.TRUE;
      `,
      documentation: 'OptionDAO.optionPredicate is checked against an object; if returns true, OptionDAO.optionDelegate is used.'+
      'Default uses OptionDAO.delegate.'
    },
  ],

  methods: [
    {
      name: 'put_',
      javaCode: `
        return getDelegateFor(obj).put_(x, obj);
      `
    },
    {
      name: 'remove_',
      javaCode: `
        return getDelegateFor(obj).remove_(x, obj);
      `
    },
    {
      name: 'getDelegateFor',
      type: 'foam.dao.DAO',
      args: [
        { name: 'obj', type: 'foam.core.FObject' },
      ],
      javaCode: `
        if ( getOptionPredicate().f(obj) ) {
          return getOptionDelegate();
        }

        return getDelegate();
      `
    }
  ],
});
{'fraction_non_alphanumeric': 0.1364566750056857, 'fraction_numerical': 0.002729133500113714, 'mean_word_length': 2.282089552238806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6169120', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1415, 'n_words': 327}