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.collect.Maps; |
| 17 import com.google.dart.engine.context.AnalysisContext; |
| 18 |
| 19 import java.util.Map; |
| 20 |
| 21 /** |
| 22 * A helper that encodes/decodes {@link AnalysisContext}s from/to integers. |
| 23 * |
| 24 * @coverage dart.engine.index |
| 25 */ |
| 26 public class ContextCodec { |
| 27 /** |
| 28 * A table mapping contexts to their unique indices. |
| 29 */ |
| 30 private final Map<AnalysisContext, Integer> contextToIndex = Maps.newHashMap()
; |
| 31 |
| 32 /** |
| 33 * A table mapping indices to the corresponding contexts. |
| 34 */ |
| 35 private final Map<Integer, AnalysisContext> indexToContext = Maps.newHashMap()
; |
| 36 |
| 37 /** |
| 38 * The next id to assign. |
| 39 */ |
| 40 private int nextId; |
| 41 |
| 42 /** |
| 43 * Returns the {@link AnalysisContext} that corresponds to the given index. |
| 44 */ |
| 45 public AnalysisContext decode(int index) { |
| 46 return indexToContext.get(index); |
| 47 } |
| 48 |
| 49 /** |
| 50 * Returns an unique index for the given {@link AnalysisContext}. |
| 51 */ |
| 52 public int encode(AnalysisContext context) { |
| 53 Integer index = contextToIndex.get(context); |
| 54 if (index == null) { |
| 55 index = nextId++; |
| 56 contextToIndex.put(context, index); |
| 57 indexToContext.put(index, context); |
| 58 } |
| 59 return index; |
| 60 } |
| 61 |
| 62 /** |
| 63 * Removes the given {@link AnalysisContext}. |
| 64 */ |
| 65 public void removeContext(AnalysisContext context) { |
| 66 Integer id = contextToIndex.remove(context); |
| 67 if (id != null) { |
| 68 indexToContext.remove(id); |
| 69 } |
| 70 } |
| 71 } |
OLD | NEW |