<commit_before>package org.swellrt.beta.model.remote;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.swellrt.beta.client.PlatformBasedFactory;
import org.swellrt.beta.client.WaveStatus;
import org.swellrt.beta.common.SException;
import org.swellrt.beta.model.SHandler;
import org.swellrt.beta.model.SMap;
import org.swellrt.beta.model.SNode;
import org.swellrt.beta.model.SObject;
import org.swellrt.beta.model.SObservable;
import org.swellrt.beta.model.SPrimitive;
import org.swellrt.beta.model.SStatusEvent;
import org.swellrt.beta.model.SText;
import org.swellrt.beta.model.js.Proxy;
import org.swellrt.beta.model.js.SMapProxyHandler;
import org.swellrt.beta.model.remote.wave.DocumentBasedBasicRMap;
import org.waveprotocol.wave.model.adt.ObservableBasicMap;
import org.waveprotocol.wave.model.document.Doc;
import org.waveprotocol.wave.model.document.Doc.E;
import org.waveprotocol.wave.model.document.Document;
import org.waveprotocol.wave.model.document.ObservableDocument;
import org.waveprotocol.wave.model.document.operation.DocInitialization;
import org.waveprotocol.wave.model.document.util.DefaultDocEventRouter;
import org.waveprotocol.wave.model.document.util.DocHelper;
import org.waveprotocol.wave.model.id.IdGenerator;
import org.waveprotocol.wave.model.id.ModernIdSerialiser;
import org.waveprotocol.wave.model.id.WaveletId;
import org.waveprotocol.wave.model.util.Preconditions;
import org.waveprotocol.wave.model.util.Serializer;
import org.waveprotocol.wave.model.wave.Blip;
import org.waveprotocol.wave.model.wave.InvalidParticipantAddress;
import org.waveprotocol.wave.model.wave.ObservableWavelet;
import org.waveprotocol.wave.model.wave.ParticipantId;
import org.waveprotocol.wave.model.wave.opbased.ObservableWaveView;


/**
 * 
 * Main class providing object-based data model built on top of Wave data model.
 * <p>
 * A {@link SObject} represents a JSON-like data structure containing a nested structure of {@link SMap}, {@link SList} and {@link SPrimitive} values. 
 * <p>
 * The underlying implementation based on the Wave data model provides:
 * <ul>
 * <li>Real-time sync of SObject state from changes of different users, even between different servers (see Wave Federation)</li>
 * <li>In-flight persistence of changes with eventual consistency (see Wave Operational Transformations)</li>
 * </ul>
 * <p>
 * The SObject data model is mapped with Wave data model as follows:
 * <p>
 * Wave => SObject instance
 * <br>
 * Wavelet => Container
 * <br>
 * Blip/Document => Substrate
 * <br>
 * <p>
 * Implementation details based on the Wave data model follows:
 * <p>
 * Each node of a CObject is stored in a substrate Wave blip/document. Blips could be stored in different container Wavelets for
 * performance reasons if it is necessary.
 * <br>
 * A map (see example below) is represented as a following XML structure within a blip. Primitive values are embedded in their container structure,
 * and nested containers {@link SMap} or {@link SList} are represented as pointers to its substrate blip. 
 * <pre>
 * {@code
 *  
 *    prop1 : "value1",
 *    prop2 : 12345,
 *    prop3 : 
 *              prop31: "value31"
 *              prop32: "value32"
 *            
 *     prop4 : [ "a", "b", "c" ]
 *  
 * 
 * 
 *  <object>
 *    <prop1 t='s'>value1</prop1>
 *    <prop2 t='i'>12345</prop2>
 *    <prop3 t='o'>
 *          <prop31 t='s'>value31</prop31>
 *          <prop32 t='s'>value32</prop32>
 *    </prop3>
 *    <prop4 t='a'>
 *          <item t='s'>a</item>
 *          <item t='s'>b</item>
 *          <item t='s'>c</item>
 *    </prop4>
 *  </object>
 * 
 * }
 * </pre>
 * @author <EMAIL> (<NAME>)
 *
 */
public class SObjectRemote extends SNodeRemoteContainer implements SObject, SObservable {
   
  /**
   * A serializer/deserializer of CNode objects to/from a Wave concurrent map
   */
  public class SubstrateMapSerializer implements org.waveprotocol.wave.model.util.Serializer<SNodeRemote> {

    
    @Override
    public String toString(SNodeRemote x) {
    
      if (x instanceof SNodeRemote) {
        SNodeRemote r = (SNodeRemote) x;  
        return r.getSubstrateId().serialize();
      }
      
      if (x instanceof SPrimitive) {        
        SPrimitive p = (SPrimitive) x;
        return p.serialize();
      }
      
      return null;
    }

    @Override
    public SNodeRemote fromString(String s) {            
      Preconditions.checkNotNull(s, "Unable to deserialize a null value");    
     
      SubstrateId substrateId = SubstrateId.deserialize(s);
      if (substrateId != null) {
      
        if (substrateId.isMap())
          return loadMap(substrateId);
        
        if (substrateId.isText())
          return loadText(substrateId, null);
        
        return null;
      
      } else {      
        return SPrimitive.deserialize(s);      
      }
      
    }

    @Override
    public SNodeRemote fromString(String s, SNodeRemote defaultValue) {<commit_msg>Fix bug in internal remote map logic for primitives<commit_after>package org.swellrt.beta.model.remote;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.swellrt.beta.client.PlatformBasedFactory;
import org.swellrt.beta.client.WaveStatus;
import org.swellrt.beta.common.SException;
import org.swellrt.beta.model.SHandler;
import org.swellrt.beta.model.SMap;
import org.swellrt.beta.model.SNode;
import org.swellrt.beta.model.SObject;
import org.swellrt.beta.model.SObservable;
import org.swellrt.beta.model.SPrimitive;
import org.swellrt.beta.model.SStatusEvent;
import org.swellrt.beta.model.SText;
import org.swellrt.beta.model.js.Proxy;
import org.swellrt.beta.model.js.SMapProxyHandler;
import org.swellrt.beta.model.remote.wave.DocumentBasedBasicRMap;
import org.waveprotocol.wave.model.adt.ObservableBasicMap;
import org.waveprotocol.wave.model.document.Doc;
import org.waveprotocol.wave.model.document.Doc.E;
import org.waveprotocol.wave.model.document.Document;
import org.waveprotocol.wave.model.document.ObservableDocument;
import org.waveprotocol.wave.model.document.operation.DocInitialization;
import org.waveprotocol.wave.model.document.util.DefaultDocEventRouter;
import org.waveprotocol.wave.model.document.util.DocHelper;
import org.waveprotocol.wave.model.id.IdGenerator;
import org.waveprotocol.wave.model.id.ModernIdSerialiser;
import org.waveprotocol.wave.model.id.WaveletId;
import org.waveprotocol.wave.model.util.Preconditions;
import org.waveprotocol.wave.model.util.Serializer;
import org.waveprotocol.wave.model.wave.Blip;
import org.waveprotocol.wave.model.wave.InvalidParticipantAddress;
import org.waveprotocol.wave.model.wave.ObservableWavelet;
import org.waveprotocol.wave.model.wave.ParticipantId;
import org.waveprotocol.wave.model.wave.opbased.ObservableWaveView;


/**
 * 
 * Main class providing object-based data model built on top of Wave data model.
 * <p>
 * A {@link SObject} represents a JSON-like data structure containing a nested structure of {@link SMap}, {@link SList} and {@link SPrimitive} values. 
 * <p>
 * The underlying implementation based on the Wave data model provides:
 * <ul>
 * <li>Real-time sync of SObject state from changes of different users, even between different servers (see Wave Federation)</li>
 * <li>In-flight persistence of changes with eventual consistency (see Wave Operational Transformations)</li>
 * </ul>
 * <p>
 * The SObject data model is mapped with Wave data model as follows:
 * <p>
 * Wave => SObject instance
 * <br>
 * Wavelet => Container
 * <br>
 * Blip/Document => Substrate
 * <br>
 * <p>
 * Implementation details based on the Wave data model follows:
 * <p>
 * Each node of a CObject is stored in a substrate Wave blip/document. Blips could be stored in different container Wavelets for
 * performance reasons if it is necessary.
 * <br>
 * A map (see example below) is represented as a following XML structure within a blip. Primitive values are embedded in their container structure,
 * and nested containers {@link SMap} or {@link SList} are represented as pointers to its substrate blip. 
 * <pre>
 * {@code
 *  
 *    prop1 : "value1",
 *    prop2 : 12345,
 *    prop3 : 
 *              prop31: "value31"
 *              prop32: "value32"
 *            
 *     prop4 : [ "a", "b", "c" ]
 *  
 * 
 * 
 *  <object>
 *    <prop1 t='s'>value1</prop1>
 *    <prop2 t='i'>12345</prop2>
 *    <prop3 t='o'>
 *          <prop31 t='s'>value31</prop31>
 *          <prop32 t='s'>value32</prop32>
 *    </prop3>
 *    <prop4 t='a'>
 *          <item t='s'>a</item>
 *          <item t='s'>b</item>
 *          <item t='s'>c</item>
 *    </prop4>
 *  </object>
 * 
 * }
 * </pre>
 * @author <EMAIL> (<NAME>)
 *
 */
public class SObjectRemote extends SNodeRemoteContainer implements SObject, SObservable {
   
  /**
   * A serializer/deserializer of CNode objects to/from a Wave concurrent map
   */
  public class SubstrateMapSerializer implements org.waveprotocol.wave.model.util.Serializer<SNodeRemote> {

    
    @Override
    public String toString(SNodeRemote x) {

      // Order matters check SPrimitive first 
      if (x instanceof SPrimitive) {        
        SPrimitive p = (SPrimitive) x;
        return p.serialize();
      }
      
      if (x instanceof SNodeRemote) {
        SNodeRemote r = (SNodeRemote) x;  
        SubstrateId id = r.getSubstrateId();
        if (id != null) 
          return id.serialize();
      }
      
      return null;
    }

    @Override
    public SNodeRemote fromString(String s) {            
      Preconditions.checkNotNull(s, "Unable to deserialize a null value");    
     
      SubstrateId substrateId = SubstrateId.deserialize(s);
      if (substrateId != null) {
      
        if (substrateId.isMap())
          return loadMap(substrateId);
        
        if (substrateId.isText())
          return loadText(substrateId, null);
        
        return null;
      
      } else {      
        return SPrimitive.deserialize(s);      
      }
      
    }

    @Override
    public SNodeRemote fromString(String s, SNodeRemote defaultValue) {
{'fraction_non_alphanumeric': 0.11314048770855958, 'fraction_numerical': 0.008687925757725343, 'mean_word_length': 3.240267894516534, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 91, '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': '3316388', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3127, 'n_words': 779}