|
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.List; |
|
8 import java.util.concurrent.ConcurrentHashMap; |
|
9 import java.util.concurrent.ConcurrentMap; |
|
10 |
|
11 import org.chromium.sdk.internal.protocol.data.SomeHandle; |
|
12 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException; |
|
13 import org.chromium.sdk.internal.tools.v8.V8ProtocolUtil; |
|
14 import org.json.simple.JSONObject; |
|
15 |
|
16 /** |
|
17 * A facility for storage and retrieval of handle objects using their "ref" IDs. |
|
18 */ |
|
19 public class HandleManager { |
|
20 |
|
21 private final ConcurrentMap<Long, SomeHandle> refToHandle = |
|
22 new ConcurrentHashMap<Long, SomeHandle>(); |
|
23 |
|
24 public void putAll(List<SomeHandle> list) { |
|
25 for (SomeHandle handle : list) { |
|
26 put(handle.handle(), handle); |
|
27 } |
|
28 } |
|
29 |
|
30 SomeHandle put(Long ref, JSONObject object) { |
|
31 SomeHandle smthWithHandle; |
|
32 try { |
|
33 smthWithHandle = V8ProtocolUtil.getV8Parser().parse(object, SomeHandle.class); |
|
34 } catch (JsonProtocolParseException e) { |
|
35 throw new RuntimeException(e); |
|
36 } |
|
37 put(ref, smthWithHandle); |
|
38 return smthWithHandle; |
|
39 } |
|
40 |
|
41 private void put(Long ref, SomeHandle smthWithHandle) { |
|
42 SomeHandle oldObject = refToHandle.putIfAbsent(ref, smthWithHandle); |
|
43 if (oldObject != null) { |
|
44 mergeValues(oldObject, smthWithHandle); |
|
45 } |
|
46 } |
|
47 |
|
48 void put(SomeHandle someHandle) { |
|
49 if (someHandle.handle() >= 0) { |
|
50 put(someHandle.handle(), someHandle); |
|
51 } |
|
52 } |
|
53 |
|
54 public SomeHandle getHandle(Long ref) { |
|
55 return refToHandle.get(ref); |
|
56 } |
|
57 |
|
58 private static void mergeValues(SomeHandle oldObject, SomeHandle newObject) { |
|
59 } |
|
60 } |