author | Eugene Ostroukhov <eugeneo@symbian.org> |
Mon, 07 Jun 2010 16:51:19 -0700 | |
changeset 355 | 8726e95bcbba |
parent 276 | f2f4a1259de8 |
permissions | -rw-r--r-- |
2 | 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.Collections; |
|
8 |
import java.util.Comparator; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Map; |
|
11 |
import java.util.SortedMap; |
|
12 |
import java.util.TreeMap; |
|
13 |
||
14 |
import org.chromium.sdk.JsArray; |
|
15 |
import org.chromium.sdk.JsVariable; |
|
16 |
import org.chromium.sdk.internal.tools.v8.MethodIsBlockingException; |
|
17 |
||
18 |
/** |
|
19 |
* A generic implementation of the JsArray interface. |
|
20 |
*/ |
|
21 |
class JsArrayImpl extends JsObjectImpl implements JsArray { |
|
22 |
||
23 |
/** |
|
24 |
* An indexed sparse array of elements. Keys are indices, values are elements. |
|
25 |
*/ |
|
26 |
private SortedMap<Integer, JsVariableImpl> indexToElementMap; |
|
27 |
||
28 |
/** |
|
29 |
* This constructor implies lazy resolution of object properties. |
|
30 |
* |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
31 |
* @param context this array belongs in |
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
32 |
* @param variableFqn the fully qualified name of the variable holding this array |
2 | 33 |
* @param valueState the mirror corresponding to this array |
34 |
*/ |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
35 |
JsArrayImpl(InternalContext context, String variableFqn, ValueMirror valueState) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
36 |
super(context, variableFqn, valueState); |
2 | 37 |
} |
38 |
||
39 |
private synchronized void ensureElementsMap() throws MethodIsBlockingException { |
|
40 |
if (indexToElementMap != null) { |
|
41 |
return; |
|
42 |
} |
|
43 |
SortedMap<Integer, JsVariableImpl> map = |
|
44 |
// TODO(peter.rybin): do we need this comparator at all? |
|
45 |
new TreeMap<Integer, JsVariableImpl>(new Comparator<Integer>() { |
|
46 |
public int compare(Integer o1, Integer o2) { |
|
47 |
return o1 - o2; |
|
48 |
} |
|
49 |
}); |
|
50 |
||
51 |
for (JsVariableImpl prop : getProperties()) { |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
52 |
Object name = prop.getRawNameAsObject(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
53 |
if (name instanceof Number == false) { |
2 | 54 |
continue; |
55 |
} |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
56 |
Number index = (Number) name; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
57 |
map.put(index.intValue(), prop); |
2 | 58 |
} |
59 |
indexToElementMap = Collections.unmodifiableSortedMap(map); |
|
60 |
} |
|
61 |
||
62 |
public JsVariable get(int index) throws MethodIsBlockingException { |
|
63 |
ensureElementsMap(); |
|
64 |
return indexToElementMap.get(index); |
|
65 |
} |
|
66 |
||
67 |
public SortedMap<Integer, ? extends JsVariable> toSparseArray() throws MethodIsBlockingException { |
|
68 |
ensureElementsMap(); |
|
69 |
return indexToElementMap; |
|
70 |
} |
|
71 |
||
72 |
public int length() { |
|
73 |
// TODO(peter.rybin) optimize it: either read "length" from remote or count PropertyReference |
|
74 |
// rather than JsVariableImpl |
|
75 |
int lastIndex = -1; |
|
76 |
List<JsVariableImpl> properties = getSubpropertiesHelper().getPropertiesLazily(); |
|
77 |
// TODO(peter.rybin): rename propRefs |
|
78 |
for (JsVariableImpl prop : properties) { |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
79 |
Object name = prop.getRawNameAsObject(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
80 |
if (name instanceof Number == false) { |
2 | 81 |
continue; |
82 |
} |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
83 |
Number index = (Number) name; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
84 |
int intIndex = index.intValue(); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
85 |
if (intIndex > lastIndex) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
86 |
lastIndex = intIndex; |
2 | 87 |
} |
88 |
} |
|
89 |
return lastIndex + 1; |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public String toString() { |
|
94 |
SortedMap<Integer, ? extends JsVariable> elements; |
|
95 |
try { |
|
96 |
elements = toSparseArray(); |
|
97 |
} catch (MethodIsBlockingException e) { |
|
98 |
return "[JsArray: Exception in retrieving data]"; |
|
99 |
} |
|
100 |
StringBuilder result = new StringBuilder(); |
|
101 |
result.append("[JsArray: length=").append(elements.size()); |
|
102 |
for (Map.Entry<Integer, ? extends JsVariable> entry : elements.entrySet()) { |
|
103 |
result.append(',').append(entry.getKey()).append('=').append(entry.getValue()); |
|
104 |
} |
|
105 |
result.append(']'); |
|
106 |
return result.toString(); |
|
107 |
} |
|
108 |
||
109 |
@Override |
|
110 |
public JsArrayImpl asArray() { |
|
111 |
return this; |
|
112 |
} |
|
113 |
} |