OLD | NEW |
(Empty) | |
| 1 package com.google.dart.engine.internal.index.file; |
| 2 |
| 3 import com.google.common.collect.Maps; |
| 4 import com.google.dart.engine.context.AnalysisContext; |
| 5 |
| 6 import java.util.List; |
| 7 import java.util.Map; |
| 8 |
| 9 /** |
| 10 * A {@link NodeManager} that keeps {@link IndexNode}s in memory. |
| 11 */ |
| 12 public class MemoryNodeManager implements NodeManager { |
| 13 public final ContextCodec contextCodec = new ContextCodec(); |
| 14 public final StringCodec stringCodec = new StringCodec(); |
| 15 public final ElementCodec elementCodec = new ElementCodec(stringCodec); |
| 16 public final RelationshipCodec relationshipCodec = new RelationshipCodec(strin
gCodec); |
| 17 |
| 18 private final Map<String, IndexNode> nodes = Maps.newHashMap(); |
| 19 private final Map<String, Integer> nodeLocationCounts = Maps.newHashMap(); |
| 20 private int locationCount = 0; |
| 21 |
| 22 @Override |
| 23 public void clear() { |
| 24 nodes.clear(); |
| 25 } |
| 26 |
| 27 @Override |
| 28 public ContextCodec getContextCodec() { |
| 29 return contextCodec; |
| 30 } |
| 31 |
| 32 @Override |
| 33 public ElementCodec getElementCodec() { |
| 34 return elementCodec; |
| 35 } |
| 36 |
| 37 @Override |
| 38 public int getLocationCount() { |
| 39 return locationCount; |
| 40 } |
| 41 |
| 42 @Override |
| 43 public IndexNode getNode(String name) { |
| 44 return nodes.get(name); |
| 45 } |
| 46 |
| 47 @Override |
| 48 public StringCodec getStringCodec() { |
| 49 return stringCodec; |
| 50 } |
| 51 |
| 52 public boolean isEmpty() { |
| 53 for (IndexNode node : nodes.values()) { |
| 54 Map<RelationKeyData, List<LocationData>> relations = node.getRelations(); |
| 55 if (!relations.isEmpty()) { |
| 56 return false; |
| 57 } |
| 58 } |
| 59 return true; |
| 60 } |
| 61 |
| 62 @Override |
| 63 public IndexNode newNode(AnalysisContext context) { |
| 64 return new IndexNode(context, elementCodec, relationshipCodec); |
| 65 } |
| 66 |
| 67 @Override |
| 68 public void putNode(String name, IndexNode node) { |
| 69 // update location count |
| 70 { |
| 71 locationCount -= getLocationCount(name); |
| 72 int nodeLocationCount = node.getLocationCount(); |
| 73 nodeLocationCounts.put(name, nodeLocationCount); |
| 74 locationCount += nodeLocationCount; |
| 75 } |
| 76 // remember the node |
| 77 nodes.put(name, node); |
| 78 } |
| 79 |
| 80 @Override |
| 81 public void removeNode(String name) { |
| 82 nodes.remove(name); |
| 83 } |
| 84 |
| 85 private int getLocationCount(String name) { |
| 86 Integer locationCount = nodeLocationCounts.get(name); |
| 87 return locationCount != null ? locationCount : 0; |
| 88 } |
| 89 } |
OLD | NEW |