org.chromium.sdk/src/org/chromium/sdk/internal/ValueMirror.java
changeset 2 e4420d2515f1
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.sdk.internal;
       
     6 
       
     7 import org.chromium.sdk.JsValue.Type;
       
     8 
       
     9 /**
       
    10  * A representation of a datum (value) in the remote JavaScript VM. Must contain all the
       
    11  * data immutable, except for properties. Reference to properties is optional and may be set later.
       
    12  */
       
    13 public class ValueMirror {
       
    14 
       
    15   public static PropertyHoldingValueMirror createScalar(String value, Type type, String className) {
       
    16     return new ValueMirror(value, type, className).getProperties();
       
    17   }
       
    18 
       
    19   public static PropertyHoldingValueMirror createObject(int refID,
       
    20       SubpropertiesMirror subpropertiesMirror, Type type, String className) {
       
    21     if (subpropertiesMirror == null) {
       
    22       throw new NullPointerException();
       
    23     }
       
    24     return new ValueMirror(refID, subpropertiesMirror, type, className).getProperties();
       
    25   }
       
    26 
       
    27   public static ValueMirror createObjectUnknownProperties(int refID, Type type, String className) {
       
    28     return new ValueMirror(refID, null, type, className);
       
    29   }
       
    30 
       
    31   private final int ref;
       
    32 
       
    33   private final Type type;
       
    34 
       
    35   private final String value;
       
    36 
       
    37   private final String className;
       
    38 
       
    39   private volatile PropertyHoldingValueMirror properties = null;
       
    40 
       
    41   private ValueMirror(String value, Type type, String className) {
       
    42     this.type = type;
       
    43     this.value = value;
       
    44     this.ref = -1;
       
    45     this.className = className;
       
    46     this.properties = new PropertyHoldingValueMirror(this);
       
    47   }
       
    48 
       
    49   private ValueMirror(int refID, SubpropertiesMirror subpropertiesMirror, Type type,
       
    50       String className) {
       
    51     this.type = type;
       
    52     this.className = className;
       
    53     this.ref = refID;
       
    54     PropertyHoldingValueMirror propertiesMirror;
       
    55     if (subpropertiesMirror == null) {
       
    56       propertiesMirror = null;
       
    57     } else {
       
    58       propertiesMirror = new PropertyHoldingValueMirror(this, subpropertiesMirror);
       
    59     }
       
    60     this.properties = propertiesMirror;
       
    61     this.value = null;
       
    62   }
       
    63 
       
    64   public Type getType() {
       
    65     return type;
       
    66   }
       
    67 
       
    68   public PropertyHoldingValueMirror getProperties() {
       
    69     return properties;
       
    70   }
       
    71 
       
    72   public int getRef() {
       
    73     return ref;
       
    74   }
       
    75 
       
    76   /**
       
    77    * @return the type representation as a String
       
    78    */
       
    79   public String getTypeAsString() {
       
    80     switch (type) {
       
    81       case TYPE_NUMBER:
       
    82       case TYPE_OBJECT:
       
    83       case TYPE_ARRAY:
       
    84       case TYPE_FUNCTION:
       
    85       case TYPE_DATE:
       
    86         return JsDataTypeUtil.getJsonString(type);
       
    87       case TYPE_STRING:
       
    88       default:
       
    89         return "text";
       
    90     }
       
    91   }
       
    92 
       
    93   @Override
       
    94   public String toString() {
       
    95     switch (type) {
       
    96       case TYPE_UNDEFINED:
       
    97       case TYPE_NULL:
       
    98       case TYPE_DATE:
       
    99       case TYPE_STRING:
       
   100       case TYPE_NUMBER:
       
   101       case TYPE_BOOLEAN:
       
   102       case TYPE_REGEXP:
       
   103         return value == null
       
   104             ? ""
       
   105             : value;
       
   106       case TYPE_OBJECT:
       
   107       case TYPE_ARRAY:
       
   108         return "[" + className + "]";
       
   109       case TYPE_FUNCTION:
       
   110         return "[Function]";
       
   111       default:
       
   112         return "";
       
   113     }
       
   114   }
       
   115 
       
   116   public String getClassName() {
       
   117     return className;
       
   118   }
       
   119 
       
   120   private static Type getObjectJsType(String className) {
       
   121     return JsDataTypeUtil.fromJsonTypeAndClassName("object", className);
       
   122   }
       
   123 
       
   124   void mergeFrom(ValueMirror alternative) {
       
   125     synchronized (MERGE_VALUE_MIRROR_MONITOR) {
       
   126       if (alternative.properties != null) {
       
   127         if (this.properties == null) {
       
   128           this.properties =
       
   129               new PropertyHoldingValueMirror(this, alternative.properties.getSubpropertiesMirror());
       
   130         } else {
       
   131           mergeProperties(this.properties, alternative.properties);
       
   132         }
       
   133       }
       
   134     }
       
   135   }
       
   136 
       
   137   private static final Object MERGE_VALUE_MIRROR_MONITOR = new Object();
       
   138 
       
   139   /**
       
   140    * Merge a record from data base with a new record just received. Theoretically
       
   141    * the new record may have something that base version lacks.
       
   142    * However,this method is more of symbolic use right now.
       
   143    *
       
   144    * @param baseProperties record version which is kept in database
       
   145    * @param altProperties record version that just has come from outside and may
       
   146    *        contain additional data
       
   147    */
       
   148   private static void mergeProperties(PropertyHoldingValueMirror baseProperties,
       
   149       PropertyHoldingValueMirror altProperties) {
       
   150   }
       
   151 }