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.Lists; |
| 17 import com.google.common.collect.Maps; |
| 18 import com.google.dart.engine.context.AnalysisContext; |
| 19 import com.google.dart.engine.element.Element; |
| 20 import com.google.dart.engine.index.Location; |
| 21 import com.google.dart.engine.index.Relationship; |
| 22 |
| 23 import java.util.List; |
| 24 import java.util.Map; |
| 25 |
| 26 /** |
| 27 * A single index file in-memory presentation. |
| 28 * |
| 29 * @coverage dart.engine.index |
| 30 */ |
| 31 public class IndexNode { |
| 32 private final AnalysisContext context; |
| 33 private final ElementCodec elementCodec; |
| 34 private final RelationshipCodec relationshipCodec; |
| 35 private final Map<RelationKeyData, List<LocationData>> relations = Maps.newHas
hMap(); |
| 36 |
| 37 public IndexNode(AnalysisContext context, ElementCodec elementCodec, |
| 38 RelationshipCodec relationshipCodec) { |
| 39 this.context = context; |
| 40 this.elementCodec = elementCodec; |
| 41 this.relationshipCodec = relationshipCodec; |
| 42 } |
| 43 |
| 44 /** |
| 45 * Returns the {@link AnalysisContext} this node is created for. |
| 46 */ |
| 47 public AnalysisContext getContext() { |
| 48 return context; |
| 49 } |
| 50 |
| 51 /** |
| 52 * Returns number of locations in this node. |
| 53 */ |
| 54 public int getLocationCount() { |
| 55 int locationCount = 0; |
| 56 for (List<LocationData> locations : relations.values()) { |
| 57 locationCount += locations.size(); |
| 58 } |
| 59 return locationCount; |
| 60 } |
| 61 |
| 62 /** |
| 63 * Returns the recorded relations. |
| 64 */ |
| 65 public Map<RelationKeyData, List<LocationData>> getRelations() { |
| 66 return relations; |
| 67 } |
| 68 |
| 69 /** |
| 70 * Return the locations of the elements that have the given relationship with
the given element. |
| 71 * |
| 72 * @param element the the element that has the relationship with the locations
to be returned |
| 73 * @param relationship the {@link Relationship} between the given element and
the locations to be |
| 74 * returned |
| 75 */ |
| 76 public Location[] getRelationships(Element element, Relationship relationship)
{ |
| 77 // prepare key |
| 78 RelationKeyData key = new RelationKeyData( |
| 79 elementCodec, |
| 80 relationshipCodec, |
| 81 element, |
| 82 relationship); |
| 83 // find LocationData(s) |
| 84 List<LocationData> locationDatas = relations.get(key); |
| 85 if (locationDatas == null) { |
| 86 return Location.EMPTY_ARRAY; |
| 87 } |
| 88 // convert to Location(s) |
| 89 List<Location> locations = Lists.newArrayList(); |
| 90 for (LocationData locationData : locationDatas) { |
| 91 Location location = locationData.getLocation(context, elementCodec); |
| 92 if (location != null) { |
| 93 locations.add(location); |
| 94 } |
| 95 } |
| 96 return locations.toArray(new Location[locations.size()]); |
| 97 } |
| 98 |
| 99 /** |
| 100 * Records that the given element and location have the given relationship. |
| 101 * |
| 102 * @param element the element that is related to the location |
| 103 * @param relationship the {@link Relationship} between the element and the lo
cation |
| 104 * @param location the {@link Location} where relationship happens |
| 105 */ |
| 106 public void recordRelationship(Element element, Relationship relationship, Loc
ation location) { |
| 107 RelationKeyData key = new RelationKeyData( |
| 108 elementCodec, |
| 109 relationshipCodec, |
| 110 element, |
| 111 relationship); |
| 112 // prepare LocationData(s) |
| 113 List<LocationData> locationDatas = relations.get(key); |
| 114 if (locationDatas == null) { |
| 115 locationDatas = Lists.newArrayList(); |
| 116 relations.put(key, locationDatas); |
| 117 } |
| 118 // add new LocationData |
| 119 locationDatas.add(new LocationData(elementCodec, location)); |
| 120 } |
| 121 |
| 122 /** |
| 123 * Sets relations data. This method is used during loading data from a storage
. |
| 124 */ |
| 125 public void setRelations(Map<RelationKeyData, List<LocationData>> relations) { |
| 126 this.relations.clear(); |
| 127 this.relations.putAll(relations); |
| 128 } |
| 129 } |
OLD | NEW |