1 19 20 package org.netbeans.api.languages; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.SortedMap ; 27 import java.util.TreeMap ; 28 29 30 35 public class ASTItem { 36 37 private String mimeType; 38 private int offset; 39 private int length = -1; 40 41 private List <ASTItem> children; 42 private ASTPath path; 43 44 ASTItem ( 45 String mimeType, 46 int offset, 47 int length, 48 List <? extends ASTItem> children 49 ) { 50 this.mimeType = mimeType; 51 this.offset = offset; 52 this.length = length; 53 54 this.children = new ArrayList <ASTItem> (); 57 if (children != null) { 58 Iterator <? extends ASTItem> it = children.iterator (); 59 while (it.hasNext ()) { 60 ASTItem item = it.next (); 61 if (item == null) 62 throw new NullPointerException (); 63 this.children.add (item); 66 } 68 } 69 this.children = Collections.unmodifiableList (this.children); 70 } 71 72 77 public int getOffset () { 78 return offset; 79 } 80 81 86 public String getMimeType () { 87 return mimeType; 88 } 89 90 95 public List <ASTItem> getChildren () { 96 return children; 97 } 98 99 105 public int getEndOffset () { 106 return getOffset () + getLength (); 107 } 108 109 114 public int getLength () { 115 if (length < 0) { 116 List <ASTItem> l = getChildren (); 117 if (l.isEmpty ()) 118 length = 0; 119 else { 120 ASTItem last = l.get (l.size () - 1); 121 length = last.getEndOffset () - getOffset (); 122 } 123 } 124 return length; 125 } 126 127 134 public ASTPath findPath (int offset) { 135 return findPath (new ArrayList <ASTItem> (), offset); 136 } 137 138 private ASTPath findPath (List <ASTItem> path, int offset) { 139 if (offset < getOffset ()) return ASTPath.create (path); 140 if (offset > getEndOffset ()) return ASTPath.create (path); 141 path.add (this); 142 if (getChildren ().isEmpty ()) 143 return ASTPath.create (path); 144 if (getChildren ().size () > 10) 145 return findPath2 (path, offset); 146 Iterator <ASTItem> it = getChildren ().iterator (); 147 while (it.hasNext ()) { 148 ASTItem item = it.next (); 149 if (offset < item.getEndOffset () && 150 item.getOffset () <= offset 151 ) 152 return item.findPath (path, offset); 153 } 154 return ASTPath.create (path); 155 } 156 157 private ASTPath findPath2 (List <ASTItem> path, int offset) { 158 TreeMap <Integer ,ASTItem> childrenMap = getChildrenMap (); 159 SortedMap <Integer ,ASTItem> headMap = childrenMap.headMap (new Integer (offset + 1)); 160 if (headMap.isEmpty ()) 161 return ASTPath.create (path); 162 Integer key = headMap.lastKey (); 163 ASTItem item = childrenMap.get (key); 164 ASTPath path2 = item.findPath (path, offset); 165 if (path2 == null) 166 return ASTPath.create (path); 167 return path2; 168 } 169 170 private TreeMap <Integer ,ASTItem> childrenMap = null; 171 172 private TreeMap <Integer ,ASTItem> getChildrenMap () { 173 if (childrenMap == null) { 174 childrenMap = new TreeMap <Integer ,ASTItem> (); 175 Iterator <ASTItem> it = getChildren ().iterator (); 176 while (it.hasNext ()) { 177 ASTItem item = it.next (); 178 childrenMap.put (new Integer (item.getOffset ()), item); 179 } 180 } 181 return childrenMap; 182 } 183 } | Popular Tags |