OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | |
5 /** | 4 /** |
6 * @constructor | |
7 * @template T | 5 * @template T |
| 6 * @unrestricted |
8 */ | 7 */ |
9 WebInspector.CharacterIdMap = function() | 8 WebInspector.CharacterIdMap = class { |
10 { | 9 constructor() { |
11 /** @type {!Map<T, string>} */ | 10 /** @type {!Map<T, string>} */ |
12 this._elementToCharacter = new Map(); | 11 this._elementToCharacter = new Map(); |
13 /** @type {!Map<string, T>} */ | 12 /** @type {!Map<string, T>} */ |
14 this._characterToElement = new Map(); | 13 this._characterToElement = new Map(); |
15 this._charCode = 33; | 14 this._charCode = 33; |
| 15 } |
| 16 |
| 17 /** |
| 18 * @param {T} object |
| 19 * @return {string} |
| 20 */ |
| 21 toChar(object) { |
| 22 var character = this._elementToCharacter.get(object); |
| 23 if (!character) { |
| 24 if (this._charCode >= 0xFFFF) |
| 25 throw new Error('CharacterIdMap ran out of capacity!'); |
| 26 character = String.fromCharCode(this._charCode++); |
| 27 this._elementToCharacter.set(object, character); |
| 28 this._characterToElement.set(character, object); |
| 29 } |
| 30 return character; |
| 31 } |
| 32 |
| 33 /** |
| 34 * @param {string} character |
| 35 * @return {?T} |
| 36 */ |
| 37 fromChar(character) { |
| 38 return this._characterToElement.get(character) || null; |
| 39 } |
16 }; | 40 }; |
17 | |
18 WebInspector.CharacterIdMap.prototype = { | |
19 /** | |
20 * @param {T} object | |
21 * @return {string} | |
22 */ | |
23 toChar: function(object) | |
24 { | |
25 var character = this._elementToCharacter.get(object); | |
26 if (!character) { | |
27 if (this._charCode >= 0xFFFF) | |
28 throw new Error("CharacterIdMap ran out of capacity!"); | |
29 character = String.fromCharCode(this._charCode++); | |
30 this._elementToCharacter.set(object, character); | |
31 this._characterToElement.set(character, object); | |
32 } | |
33 return character; | |
34 }, | |
35 | |
36 /** | |
37 * @param {string} character | |
38 * @return {?T} | |
39 */ | |
40 fromChar: function(character) | |
41 { | |
42 return this._characterToElement.get(character) || null; | |
43 } | |
44 }; | |
OLD | NEW |