<commit_before>/**
 * @license
 * Copyright 2017,2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */
foam.INTERFACE({
  package: 'foam.lib.csv',
  name: 'FromCSVSetter',
  methods: [
    {
      name: 'set',
      args: [
        { type: 'FObject', name: 'obj' },
        { type: 'String',  name: 'str' }
      ]
    }
  ]
});


foam.LIB({
  name: 'foam.java',
  flags: ['java'],
  methods: [
    {
      name: 'asJavaValue',
      code: foam.mmethod({
        String: function asJavaValue(s) {
          return '"' + s.
            replace(/\\/g, "\\\\").
            replace(/"/g, '\\"').
            replace(/\n/g, "\\n") + '"';
        },
        Boolean: function(b) {
          return b ? "true" : "false";
        },
        Number: function(n) {
          return '' + n +
            (n > Math.pow(2, 31) || n < -Math.pow(2,31) ? 'L' : '');
        },
        FObject: function(o) {
          return o.asJavaValue();
        },
        Undefined: function() {
          // TODO: This probably isn't strictly right, but we do it in
          // a number of places.
          return null;
        },
        Array: function(a, prop) {
          return "new " + (prop ? prop.javaType : 'Object[]') + " {" +
            a.map(foam.java.asJavaValue).join(',') +
            '}';
        },
        Null: function(n) { return "null"; },
        Object: function(o) {
          return `foam.util.Arrays.asMap(new Object[] {
${Object.keys(o).map(function(k, i, a) {
  return `  ${foam.java.asJavaValue(k)}, ${foam.java.asJavaValue(o[k])}` + ((i == a.length-1) ? '' : ',')
}).join('\n')}
})`;
        },
        RegExp: function(o) {
          o = o.toString();
          o = o.slice(o.indexOf('/') + 1, o.lastIndexOf('/'))
          o = o.replace(/\\/g, '\\\\')
          return `java.util.regex.Pattern.compile("${o}")`
        },
        Date: function(d) {
          var n = d.getTime();
          return `new java.util.Date(` + n +
            (n > Math.pow(2, 31) || n < -Math.pow(2,31) ? 'L' : '') + `)`
        }
      })
    },
    {
      name: 'toJavaType',
      code: function(type) {
        return foam.core.type.toType(type).toJavaType();
      }
    }<commit_msg>Let Objects specify their own asJavaValue behaviour with method.<commit_after>/**
 * @license
 * Copyright 2017,2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */
foam.INTERFACE({
  package: 'foam.lib.csv',
  name: 'FromCSVSetter',
  methods: [
    {
      name: 'set',
      args: [
        { type: 'FObject', name: 'obj' },
        { type: 'String',  name: 'str' }
      ]
    }
  ]
});


foam.LIB({
  name: 'foam.java',
  flags: ['java'],
  methods: [
    {
      name: 'asJavaValue',
      code: foam.mmethod({
        String: function asJavaValue(s) {
          return '"' + s.
            replace(/\\/g, "\\\\").
            replace(/"/g, '\\"').
            replace(/\n/g, "\\n") + '"';
        },
        Boolean: function(b) {
          return b ? "true" : "false";
        },
        Number: function(n) {
          return '' + n +
            (n > Math.pow(2, 31) || n < -Math.pow(2,31) ? 'L' : '');
        },
        FObject: function(o) {
          return o.asJavaValue();
        },
        Undefined: function() {
          // TODO: This probably isn't strictly right, but we do it in
          // a number of places.
          return null;
        },
        Array: function(a, prop) {
          return "new " + (prop ? prop.javaType : 'Object[]') + " {" +
            a.map(foam.java.asJavaValue).join(',') +
            '}';
        },
        Null: function(n) { return "null"; },
        Object: function(o) {
          if ( o.asJavaValue ) return o.asJavaValue.call(o, o);
          return `foam.util.Arrays.asMap(new Object[] {
${Object.keys(o).map(function(k, i, a) {
  return `  ${foam.java.asJavaValue(k)}, ${foam.java.asJavaValue(o[k])}` + ((i == a.length-1) ? '' : ',')
}).join('\n')}
})`;
        },
        RegExp: function(o) {
          o = o.toString();
          o = o.slice(o.indexOf('/') + 1, o.lastIndexOf('/'))
          o = o.replace(/\\/g, '\\\\')
          return `java.util.regex.Pattern.compile("${o}")`
        },
        Date: function(d) {
          var n = d.getTime();
          return `new java.util.Date(` + n +
            (n > Math.pow(2, 31) || n < -Math.pow(2,31) ? 'L' : '') + `)`
        }
      })
    },
    {
      name: 'toJavaType',
      code: function(type) {
        return foam.core.type.toType(type).toJavaType();
      }
    }
{'fraction_non_alphanumeric': 0.20583717357910905, 'fraction_numerical': 0.01053324555628703, 'mean_word_length': 1.6377314814814814, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7221559', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1574, 'n_words': 305}