org.chromium.sdk/src/org/chromium/sdk/internal/SubpropertiesMirror.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 java.util.Arrays;
       
     8 import java.util.Collections;
       
     9 import java.util.List;
       
    10 
       
    11 import org.chromium.sdk.internal.protocol.data.FunctionValueHandle;
       
    12 import org.chromium.sdk.internal.protocol.data.ObjectValueHandle;
       
    13 import org.chromium.sdk.internal.tools.v8.V8ProtocolUtil;
       
    14 
       
    15 /**
       
    16  * This class is intended to hold properties either already parsed or to be parsed on demand.
       
    17  */
       
    18 public abstract class SubpropertiesMirror {
       
    19   public abstract List<? extends PropertyReference> getProperties();
       
    20 
       
    21   public abstract List<? extends PropertyReference> getInternalProperties();
       
    22 
       
    23   public abstract Object getAdditionalProperties();
       
    24 
       
    25   public static class ObjectValueBased extends JsonBased<ObjectValueHandle> {
       
    26     private final ObjectValueHandle objectValueHandle;
       
    27     public ObjectValueBased(ObjectValueHandle valueHandle,
       
    28         AdditionalPropertyFactory<ObjectValueHandle> additionalPropertyFactory) {
       
    29       super(additionalPropertyFactory);
       
    30       this.objectValueHandle = valueHandle;
       
    31     }
       
    32     @Override
       
    33     protected ObjectValueHandle getObjectForFactory() {
       
    34       return objectValueHandle;
       
    35     }
       
    36     @Override
       
    37     protected ObjectValueHandle getObjectValue() {
       
    38       return objectValueHandle;
       
    39     }
       
    40   }
       
    41   public static class FunctionValueBased extends JsonBased<FunctionValueHandle> {
       
    42     private final FunctionValueHandle functionValueHandle;
       
    43     public FunctionValueBased(FunctionValueHandle functionValueHandle,
       
    44         AdditionalPropertyFactory<FunctionValueHandle> additionalPropertyFactory) {
       
    45       super(additionalPropertyFactory);
       
    46       this.functionValueHandle = functionValueHandle;
       
    47     }
       
    48     @Override
       
    49     protected FunctionValueHandle getObjectForFactory() {
       
    50       return functionValueHandle;
       
    51     }
       
    52     @Override
       
    53     protected ObjectValueHandle getObjectValue() {
       
    54       return functionValueHandle.getSuper();
       
    55     }
       
    56   }
       
    57 
       
    58   /**
       
    59    * Keeps properties in for of JSON and parses JSON on demand.
       
    60    */
       
    61   public static abstract class JsonBased<T> extends SubpropertiesMirror {
       
    62     private final AdditionalPropertyFactory<T> additionalPropertyFactory;
       
    63 
       
    64     private List<? extends PropertyReference> properties = null;
       
    65     private List<? extends PropertyReference> internalProperties = null;
       
    66     private Object additionalProperties = null;
       
    67 
       
    68     public JsonBased(AdditionalPropertyFactory<T> additionalPropertyFactory) {
       
    69       if (additionalPropertyFactory == null) {
       
    70         additionalPropertyFactory = NO_OP_FACTORY;
       
    71       }
       
    72       this.additionalPropertyFactory = additionalPropertyFactory;
       
    73     }
       
    74 
       
    75     @Override
       
    76     public synchronized List<? extends PropertyReference> getProperties() {
       
    77       if (properties == null) {
       
    78         properties = V8ProtocolUtil.extractObjectProperties(getObjectValue());
       
    79       }
       
    80       return properties;
       
    81     }
       
    82 
       
    83     @Override
       
    84     public synchronized List<? extends PropertyReference> getInternalProperties() {
       
    85       if (internalProperties == null) {
       
    86         internalProperties = V8ProtocolUtil.extractObjectInternalProperties(getObjectValue());
       
    87       }
       
    88       return internalProperties;
       
    89     }
       
    90 
       
    91     protected abstract ObjectValueHandle getObjectValue();
       
    92 
       
    93     @Override
       
    94     public Object getAdditionalProperties() {
       
    95       if (additionalProperties == null) {
       
    96         additionalProperties =
       
    97             additionalPropertyFactory.createAdditionalProperties(getObjectForFactory());
       
    98       }
       
    99       return additionalProperties;
       
   100     }
       
   101     protected abstract T getObjectForFactory();
       
   102 
       
   103     public interface AdditionalPropertyFactory<T> {
       
   104       Object createAdditionalProperties(T jsonWithProperties);
       
   105     }
       
   106 
       
   107     private static AdditionalPropertyFactory NO_OP_FACTORY = new AdditionalPropertyFactory<Void>() {
       
   108       public Object createAdditionalProperties(Void jsonWithProperties) {
       
   109         return EMPTY_OBJECT;
       
   110       }
       
   111     };
       
   112   }
       
   113 
       
   114   static class ListBased extends SubpropertiesMirror {
       
   115     private final List<PropertyReference> list;
       
   116 
       
   117     ListBased(PropertyReference ... refs) {
       
   118       this.list = Collections.unmodifiableList(Arrays.asList(refs));
       
   119     }
       
   120 
       
   121     @Override
       
   122     public List<? extends PropertyReference> getProperties() {
       
   123       return list;
       
   124     }
       
   125 
       
   126     @Override
       
   127     public List<? extends PropertyReference> getInternalProperties() {
       
   128       return Collections.emptyList();
       
   129     }
       
   130 
       
   131     @Override
       
   132     public Object getAdditionalProperties() {
       
   133       return EMPTY_OBJECT;
       
   134     }
       
   135   }
       
   136 
       
   137   static final SubpropertiesMirror EMPTY = new SubpropertiesMirror() {
       
   138     @Override
       
   139     public List<? extends PropertyReference> getProperties() {
       
   140       return Collections.emptyList();
       
   141     }
       
   142 
       
   143     @Override
       
   144     public List<? extends PropertyReference> getInternalProperties() {
       
   145       return Collections.emptyList();
       
   146     }
       
   147 
       
   148     @Override
       
   149     public Object getAdditionalProperties() {
       
   150       return EMPTY_OBJECT;
       
   151     }
       
   152   };
       
   153 
       
   154   private static final Object EMPTY_OBJECT = new Object();
       
   155 }