OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014, the Dart project authors. |
| 3 * |
| 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 * |
| 7 * http://www.eclipse.org/legal/epl-v10.html |
| 8 * |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
| 12 * the License. |
| 13 */ |
| 14 package com.google.dart.engine.internal.index.file; |
| 15 |
| 16 import com.google.common.annotations.VisibleForTesting; |
| 17 import com.google.common.collect.Lists; |
| 18 import com.google.common.collect.Maps; |
| 19 |
| 20 import java.util.List; |
| 21 import java.util.Map; |
| 22 |
| 23 /** |
| 24 * A helper that encodes/decodes {@link String}s from/to integers. |
| 25 * |
| 26 * @coverage dart.engine.index |
| 27 */ |
| 28 public class StringCodec { |
| 29 /** |
| 30 * A table mapping names to their unique indices. |
| 31 */ |
| 32 private final Map<String, Integer> nameToIndex = Maps.newHashMap(); |
| 33 |
| 34 /** |
| 35 * A table mapping indices to the corresponding strings. |
| 36 */ |
| 37 private final List<String> indexToName = Lists.newArrayList(); |
| 38 |
| 39 /** |
| 40 * Returns the {@link String} that corresponds to the given index. |
| 41 */ |
| 42 public String decode(int index) { |
| 43 return indexToName.get(index); |
| 44 } |
| 45 |
| 46 /** |
| 47 * Returns an unique index for the given {@link String}. |
| 48 */ |
| 49 public int encode(String name) { |
| 50 Integer index = nameToIndex.get(name); |
| 51 if (index == null) { |
| 52 index = indexToName.size(); |
| 53 nameToIndex.put(name, index); |
| 54 indexToName.add(name); |
| 55 } |
| 56 return index; |
| 57 } |
| 58 |
| 59 @VisibleForTesting |
| 60 public Map<String, Integer> getNameToIndex() { |
| 61 return nameToIndex; |
| 62 } |
| 63 } |
OLD | NEW |