org.chromium.debug.core/src/org/chromium/debug/core/model/ArrayValue.java
changeset 2 e4420d2515f1
child 52 f577ea64429e
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.debug.core.model;
       
     6 
       
     7 import java.util.ArrayList;
       
     8 import java.util.List;
       
     9 import java.util.SortedMap;
       
    10 
       
    11 import org.chromium.sdk.JsArray;
       
    12 import org.chromium.sdk.JsVariable;
       
    13 import org.eclipse.debug.core.DebugException;
       
    14 import org.eclipse.debug.core.model.IIndexedValue;
       
    15 import org.eclipse.debug.core.model.IVariable;
       
    16 
       
    17 /**
       
    18  * An IIndexedValue implementation for an array element range using a JsArray
       
    19  * instance.
       
    20  */
       
    21 public class ArrayValue extends Value implements IIndexedValue {
       
    22 
       
    23   private final IVariable[] elements;
       
    24 
       
    25   public ArrayValue(IChromiumDebugTarget debugTarget, JsArray array) {
       
    26     super(debugTarget, array);
       
    27     this.elements = createElements();
       
    28   }
       
    29 
       
    30   private IVariable[] createElements() {
       
    31     SortedMap<Integer, ? extends JsVariable> elements = ((JsArray) getJsValue()).toSparseArray();
       
    32     List<IVariable> variables = new ArrayList<IVariable>(elements.size());
       
    33     for (JsVariable jsVar : elements.values()) {
       
    34       variables.add(new Variable(getDebugTarget(), jsVar, false));
       
    35     }
       
    36     return variables.toArray(new IVariable[variables.size()]);
       
    37   }
       
    38 
       
    39   public int getInitialOffset() {
       
    40     return 0;
       
    41   }
       
    42 
       
    43   public int getSize() throws DebugException {
       
    44     return elements.length;
       
    45   }
       
    46 
       
    47   public IVariable getVariable(int offset) throws DebugException {
       
    48     return elements[offset];
       
    49   }
       
    50 
       
    51   public IVariable[] getVariables(int offset, int length) throws DebugException {
       
    52     IVariable[] result = new IVariable[length];
       
    53     System.arraycopy(elements, offset, result, 0, length);
       
    54     return result;
       
    55   }
       
    56 
       
    57   @Override
       
    58   public IVariable[] getVariables() throws DebugException {
       
    59     return elements;
       
    60   }
       
    61 
       
    62   @Override
       
    63   public boolean hasVariables() throws DebugException {
       
    64     return elements.length > 0;
       
    65   }
       
    66 
       
    67 }