PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/content/file/FileComponentDeclarations.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 600 lines | 489 code | 46 blank | 65 comment | 54 complexity | 5d2b99e58c4ce49f3889b89e65db5f09 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2010 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.cnd.modelimpl.content.file;
  43. import java.io.IOException;
  44. import java.lang.ref.WeakReference;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.EnumMap;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.SortedMap;
  53. import java.util.TreeMap;
  54. import java.util.concurrent.locks.ReadWriteLock;
  55. import java.util.concurrent.locks.ReentrantReadWriteLock;
  56. import org.netbeans.modules.cnd.api.model.CsmDeclaration;
  57. import org.netbeans.modules.cnd.api.model.CsmFunction;
  58. import org.netbeans.modules.cnd.api.model.CsmOffsetable;
  59. import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
  60. import org.netbeans.modules.cnd.api.model.CsmUID;
  61. import org.netbeans.modules.cnd.api.model.CsmVariable;
  62. import org.netbeans.modules.cnd.api.model.services.CsmSelect.CsmFilter;
  63. import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
  64. import org.netbeans.modules.cnd.modelimpl.csm.FunctionImpl;
  65. import org.netbeans.modules.cnd.modelimpl.csm.NamespaceImpl;
  66. import org.netbeans.modules.cnd.modelimpl.csm.VariableImpl;
  67. import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl;
  68. import org.netbeans.modules.cnd.modelimpl.repository.FileDeclarationsKey;
  69. import org.netbeans.modules.cnd.modelimpl.repository.RepositoryUtils;
  70. import org.netbeans.modules.cnd.modelimpl.uid.UIDCsmConverter;
  71. import org.netbeans.modules.cnd.modelimpl.uid.UIDObjectFactory;
  72. import org.netbeans.modules.cnd.modelimpl.uid.UIDUtilities;
  73. import org.netbeans.modules.cnd.repository.spi.Persistent;
  74. import org.netbeans.modules.cnd.repository.spi.RepositoryDataInput;
  75. import org.netbeans.modules.cnd.repository.spi.RepositoryDataOutput;
  76. import org.netbeans.modules.cnd.repository.support.SelfPersistent;
  77. import org.openide.util.CharSequences;
  78. /**
  79. *
  80. * @author Alecander Simon
  81. */
  82. public class FileComponentDeclarations extends FileComponent implements Persistent, SelfPersistent {
  83. private final TreeMap<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>> declarations;
  84. private WeakReference<Map<CsmDeclaration.Kind,SortedMap<NameKey, CsmUID<CsmOffsetableDeclaration>>>> sortedDeclarations;
  85. private final ReadWriteLock declarationsLock = new ReentrantReadWriteLock();
  86. /**
  87. * Stores the UIDs of the static functions declarations (not definitions)
  88. * This is necessary for finding definitions/declarations
  89. * since file-level static functions (i.e. c-style static functions) aren't registered in project
  90. */
  91. private final Collection<CsmUID<CsmFunction>> staticFunctionDeclarationUIDs;
  92. private final Collection<CsmUID<CsmVariable>> staticVariableUIDs;
  93. private final ReadWriteLock staticLock = new ReentrantReadWriteLock();
  94. // empty stub
  95. private static final FileComponentDeclarations EMPTY = new FileComponentDeclarations() {
  96. @Override
  97. void put() {
  98. }
  99. };
  100. public static FileComponentDeclarations empty() {
  101. return EMPTY;
  102. }
  103. FileComponentDeclarations(FileComponentDeclarations other, boolean empty) {
  104. super(other);
  105. try {
  106. if (!empty) {
  107. other.declarationsLock.readLock().lock();
  108. }
  109. declarations = new TreeMap<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>>(
  110. empty ? Collections.<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>>emptyMap() : other.declarations);
  111. } finally {
  112. if (!empty) {
  113. other.declarationsLock.readLock().unlock();
  114. }
  115. }
  116. try {
  117. if (!empty) {
  118. other.staticLock.readLock().lock();
  119. }
  120. staticFunctionDeclarationUIDs = new ArrayList<CsmUID<CsmFunction>>(
  121. empty ? Collections.<CsmUID<CsmFunction>>emptyList() : other.staticFunctionDeclarationUIDs);
  122. staticVariableUIDs = new ArrayList<CsmUID<CsmVariable>>(
  123. empty ? Collections.<CsmUID<CsmVariable>>emptyList() : other.staticVariableUIDs);
  124. } finally {
  125. if (!empty) {
  126. other.staticLock.readLock().unlock();
  127. }
  128. }
  129. }
  130. public FileComponentDeclarations(FileImpl file) {
  131. super(new FileDeclarationsKey(file));
  132. declarations = new TreeMap<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>>();
  133. staticFunctionDeclarationUIDs = new ArrayList<CsmUID<CsmFunction>>(0);
  134. staticVariableUIDs = new ArrayList<CsmUID<CsmVariable>>(0);
  135. }
  136. public FileComponentDeclarations(RepositoryDataInput input) throws IOException {
  137. super(input);
  138. UIDObjectFactory factory = UIDObjectFactory.getDefaultFactory();
  139. this.declarations = factory.readOffsetSortedToUIDMap(input, null);
  140. int collSize = input.readInt();
  141. if (collSize <= 0) {
  142. staticFunctionDeclarationUIDs = new ArrayList<CsmUID<CsmFunction>>(0);
  143. } else {
  144. staticFunctionDeclarationUIDs = new ArrayList<CsmUID<CsmFunction>>(collSize);
  145. }
  146. UIDObjectFactory.getDefaultFactory().readUIDCollection(staticFunctionDeclarationUIDs, input, collSize);
  147. collSize = input.readInt();
  148. if (collSize <= 0) {
  149. staticVariableUIDs = new ArrayList<CsmUID<CsmVariable>>(0);
  150. } else {
  151. staticVariableUIDs = new ArrayList<CsmUID<CsmVariable>>(collSize);
  152. }
  153. UIDObjectFactory.getDefaultFactory().readUIDCollection(staticVariableUIDs, input, collSize);
  154. }
  155. // only for EMPTY static field
  156. private FileComponentDeclarations() {
  157. super((org.netbeans.modules.cnd.repository.spi.Key)null);
  158. declarations = new TreeMap<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>>();
  159. staticFunctionDeclarationUIDs = new ArrayList<CsmUID<CsmFunction>>(0);
  160. staticVariableUIDs = new ArrayList<CsmUID<CsmVariable>>(0);
  161. }
  162. Collection<CsmUID<CsmOffsetableDeclaration>> clean() {
  163. Collection<CsmUID<CsmOffsetableDeclaration>> uids;
  164. try {
  165. declarationsLock.writeLock().lock();
  166. uids = new ArrayList<CsmUID<CsmOffsetableDeclaration>>(declarations.values());
  167. sortedDeclarations = null;
  168. declarations.clear();
  169. } finally {
  170. declarationsLock.writeLock().unlock();
  171. }
  172. try {
  173. staticLock.writeLock().lock();
  174. staticFunctionDeclarationUIDs.clear();
  175. staticVariableUIDs.clear();
  176. } finally {
  177. staticLock.writeLock().unlock();
  178. }
  179. put();
  180. return uids;
  181. }
  182. public boolean hasDeclarations() {
  183. return declarations.size() != 0;
  184. }
  185. public Collection<CsmOffsetableDeclaration> getDeclarations() {
  186. Collection<CsmOffsetableDeclaration> decls;
  187. try {
  188. declarationsLock.readLock().lock();
  189. Collection<CsmUID<CsmOffsetableDeclaration>> uids = declarations.values();
  190. decls = UIDCsmConverter.UIDsToDeclarations(uids);
  191. } finally {
  192. declarationsLock.readLock().unlock();
  193. }
  194. return decls;
  195. }
  196. public Iterator<CsmOffsetableDeclaration> getDeclarations(CsmFilter filter) {
  197. // can be called only from FileImpl.getDeclarations(fileter)
  198. Iterator<CsmOffsetableDeclaration> out;
  199. try {
  200. declarationsLock.readLock().lock();
  201. out = UIDCsmConverter.UIDsToDeclarationsFiltered(declarations.values(), filter);
  202. } finally {
  203. declarationsLock.readLock().unlock();
  204. }
  205. return out;
  206. }
  207. public int getDeclarationsSize(){
  208. try {
  209. declarationsLock.readLock().lock();
  210. return declarations.size();
  211. } finally {
  212. declarationsLock.readLock().unlock();
  213. }
  214. }
  215. public Collection<CsmUID<CsmOffsetableDeclaration>> getDeclarations(CsmDeclaration.Kind[] kinds, CharSequence prefix) {
  216. Collection<CsmUID<CsmOffsetableDeclaration>> out = null;
  217. try {
  218. declarationsLock.readLock().lock();
  219. Map<CsmDeclaration.Kind, SortedMap<NameKey, CsmUID<CsmOffsetableDeclaration>>> map = null;
  220. if (sortedDeclarations != null) {
  221. map = sortedDeclarations.get();
  222. }
  223. if (map == null) {
  224. map = new EnumMap<CsmDeclaration.Kind, SortedMap<NameKey, CsmUID<CsmOffsetableDeclaration>>>(CsmDeclaration.Kind.class);
  225. for(CsmUID<CsmOffsetableDeclaration> anUid : declarations.values()){
  226. CsmDeclaration.Kind kind = UIDUtilities.getKind(anUid);
  227. SortedMap<NameKey, CsmUID<CsmOffsetableDeclaration>> val = map.get(kind);
  228. if (val == null){
  229. val = new TreeMap<NameKey, CsmUID<CsmOffsetableDeclaration>>();
  230. map.put(kind, val);
  231. }
  232. val.put(new NameKey(anUid), anUid);
  233. }
  234. sortedDeclarations = new WeakReference<Map<CsmDeclaration.Kind, SortedMap<NameKey, CsmUID<CsmOffsetableDeclaration>>>>(map);
  235. }
  236. out = new ArrayList<CsmUID<CsmOffsetableDeclaration>>();
  237. for(CsmDeclaration.Kind kind : kinds) {
  238. SortedMap<NameKey, CsmUID<CsmOffsetableDeclaration>> val = map.get(kind);
  239. if (val != null) {
  240. if (prefix == null) {
  241. out.addAll(val.values());
  242. } else {
  243. NameKey fromKey = new NameKey(prefix, 0);
  244. NameKey toKey = new NameKey(prefix, Integer.MAX_VALUE);
  245. out.addAll(val.subMap(fromKey, toKey).values());
  246. }
  247. }
  248. }
  249. } finally {
  250. declarationsLock.readLock().unlock();
  251. }
  252. return out;
  253. }
  254. public CsmOffsetableDeclaration findExistingDeclaration(int startOffset, int endOffset, CharSequence name) {
  255. OffsetSortedKey key = new OffsetSortedKey(startOffset, Math.abs(CharSequences.create(name).hashCode()));
  256. CsmUID<CsmOffsetableDeclaration> anUid = null;
  257. try {
  258. declarationsLock.readLock().lock();
  259. anUid = declarations.get(key);
  260. // It seems next line wrong, so commented when method was moved from FileImpl
  261. //sortedDeclarations = null;
  262. } finally {
  263. declarationsLock.readLock().unlock();
  264. }
  265. if (anUid != null && UIDUtilities.getEndOffset(anUid) != endOffset) {
  266. anUid = null;
  267. }
  268. return UIDCsmConverter.UIDtoDeclaration(anUid);
  269. }
  270. public CsmOffsetableDeclaration findExistingDeclaration(int startOffset, CharSequence name, CsmDeclaration.Kind kind) {
  271. OffsetSortedKey key = new OffsetSortedKey(startOffset, Math.abs(CharSequences.create(name).hashCode()));
  272. CsmUID<CsmOffsetableDeclaration> anUid = null;
  273. try {
  274. declarationsLock.readLock().lock();
  275. anUid = declarations.get(key);
  276. // It seems next line wrong, so commented when method was moved from FileImpl
  277. //sortedDeclarations = null;
  278. } finally {
  279. declarationsLock.readLock().unlock();
  280. }
  281. if (anUid != null && UIDUtilities.getKind(anUid) != kind) {
  282. anUid = null;
  283. }
  284. return UIDCsmConverter.UIDtoDeclaration(anUid);
  285. }
  286. public Collection<CsmUID<CsmOffsetableDeclaration>> getDeclarations(int startOffset, int endOffset) {
  287. List<CsmUID<CsmOffsetableDeclaration>> res;
  288. try {
  289. declarationsLock.readLock().lock();
  290. res = getDeclarationsByOffset(startOffset-1);
  291. OffsetSortedKey fromKey = new OffsetSortedKey(startOffset,0);
  292. OffsetSortedKey toKey = new OffsetSortedKey(endOffset,0);
  293. SortedMap<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>> map = declarations.subMap(fromKey, toKey);
  294. for(Map.Entry<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>> entry : map.entrySet()){
  295. CsmUID<CsmOffsetableDeclaration> anUid = entry.getValue();
  296. int start = UIDUtilities.getStartOffset(anUid);
  297. int end = UIDUtilities.getEndOffset(anUid);
  298. if (start >= endOffset) {
  299. break;
  300. }
  301. if(end >= startOffset && start < endOffset) {
  302. res.add(anUid);
  303. }
  304. }
  305. } finally {
  306. declarationsLock.readLock().unlock();
  307. }
  308. return res;
  309. }
  310. public Iterator<CsmOffsetableDeclaration> getDeclarations(int offset) {
  311. List<CsmUID<CsmOffsetableDeclaration>> res;
  312. try {
  313. declarationsLock.readLock().lock();
  314. res = getDeclarationsByOffset(offset);
  315. } finally {
  316. declarationsLock.readLock().unlock();
  317. }
  318. return UIDCsmConverter.UIDsToDeclarations(res).iterator();
  319. }
  320. // call under read lock
  321. private List<CsmUID<CsmOffsetableDeclaration>> getDeclarationsByOffset(int offset){
  322. List<CsmUID<CsmOffsetableDeclaration>> res = new ArrayList<CsmUID<CsmOffsetableDeclaration>>();
  323. OffsetSortedKey key = new OffsetSortedKey(offset+1,0); // NOI18N
  324. outer : while(true) {
  325. SortedMap<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>> head = declarations.headMap(key);
  326. if (head.isEmpty()) {
  327. break;
  328. }
  329. OffsetSortedKey last = head.lastKey();
  330. while(true) {
  331. if (last == null) {
  332. break outer;
  333. }
  334. CsmUID<CsmOffsetableDeclaration> aUid = declarations.get(last);
  335. int from = UIDUtilities.getStartOffset(aUid);
  336. int to = UIDUtilities.getEndOffset(aUid);
  337. if (from <= offset && offset <= to) {
  338. res.add(0, aUid);
  339. key = last;
  340. } else {
  341. SortedMap<OffsetSortedKey, CsmUID<CsmOffsetableDeclaration>> headMap = head.headMap(last);
  342. if(!headMap.isEmpty()) {
  343. OffsetSortedKey higherKey = headMap.lastKey();
  344. if(higherKey != null) {
  345. CsmUID<CsmOffsetableDeclaration> higher = head.get(higherKey);
  346. int higherTo = UIDUtilities.getEndOffset(higher);
  347. if(higherTo >= to) {
  348. last = higherKey;
  349. continue;
  350. }
  351. }
  352. }
  353. break outer;
  354. }
  355. break;
  356. }
  357. }
  358. return res;
  359. }
  360. /**
  361. * Gets the list of the static functions declarations (not definitions)
  362. * This is necessary for finding definitions/declarations
  363. * since file-level static functions (i.e. c-style static functions) aren't registered in project
  364. */
  365. public Collection<CsmFunction> getStaticFunctionDeclarations() {
  366. Collection<CsmFunction> out;
  367. try {
  368. staticLock.readLock().lock();
  369. out = UIDCsmConverter.UIDsToDeclarations(staticFunctionDeclarationUIDs);
  370. } finally {
  371. staticLock.readLock().unlock();
  372. }
  373. return out;
  374. }
  375. public Iterator<CsmFunction> getStaticFunctionDeclarations(CsmFilter filter) {
  376. Iterator<CsmFunction> out;
  377. try {
  378. staticLock.readLock().lock();
  379. out = UIDCsmConverter.UIDsToDeclarationsFiltered(staticFunctionDeclarationUIDs, filter);
  380. } finally {
  381. staticLock.readLock().unlock();
  382. }
  383. return out;
  384. }
  385. public Collection<CsmVariable> getStaticVariableDeclarations() {
  386. Collection<CsmVariable> out;
  387. try {
  388. staticLock.readLock().lock();
  389. out = UIDCsmConverter.UIDsToDeclarations(staticVariableUIDs);
  390. } finally {
  391. staticLock.readLock().unlock();
  392. }
  393. return out;
  394. }
  395. public Iterator<CsmVariable> getStaticVariableDeclarations(CsmFilter filter) {
  396. Iterator<CsmVariable> out;
  397. try {
  398. staticLock.readLock().lock();
  399. out = UIDCsmConverter.UIDsToDeclarationsFiltered(staticVariableUIDs, filter);
  400. } finally {
  401. staticLock.readLock().unlock();
  402. }
  403. return out;
  404. }
  405. private OffsetSortedKey getOffsetSortKey(CsmOffsetableDeclaration declaration) {
  406. return new OffsetSortedKey(declaration);
  407. }
  408. public CsmUID<CsmOffsetableDeclaration> addDeclaration(CsmOffsetableDeclaration decl) {
  409. CsmUID<CsmOffsetableDeclaration> uidDecl = RepositoryUtils.put(decl);
  410. try {
  411. declarationsLock.writeLock().lock();
  412. declarations.put(getOffsetSortKey(decl), uidDecl);
  413. sortedDeclarations = null;
  414. } finally {
  415. declarationsLock.writeLock().unlock();
  416. }
  417. // TODO: remove this dirty hack!
  418. if (decl instanceof VariableImpl<?>) {
  419. VariableImpl<?> v = (VariableImpl<?>) decl;
  420. if (!NamespaceImpl.isNamespaceScope(v, true)) {
  421. v.setScope(decl.getContainingFile());
  422. addStaticVariableDeclaration(uidDecl);
  423. }
  424. }
  425. if (CsmKindUtilities.isFunctionDeclaration(decl)) {
  426. if (decl instanceof FunctionImpl<?>) {
  427. FunctionImpl<?> fi = (FunctionImpl<?>) decl;
  428. if (!NamespaceImpl.isNamespaceScope(fi)) {
  429. fi.setScope(decl.getContainingFile());
  430. addStaticFunctionDeclaration(uidDecl);
  431. }
  432. }
  433. }
  434. put();
  435. return uidDecl;
  436. }
  437. @SuppressWarnings("unchecked")
  438. private void addStaticFunctionDeclaration(CsmUID<?> uidDecl) {
  439. try {
  440. staticLock.writeLock().lock();
  441. staticFunctionDeclarationUIDs.add((CsmUID<CsmFunction>) uidDecl);
  442. } finally {
  443. staticLock.writeLock().unlock();
  444. }
  445. }
  446. @SuppressWarnings("unchecked")
  447. private void addStaticVariableDeclaration(CsmUID<?> uidDecl) {
  448. try {
  449. staticLock.writeLock().lock();
  450. staticVariableUIDs.add((CsmUID<CsmVariable>) uidDecl);
  451. } finally {
  452. staticLock.writeLock().unlock();
  453. }
  454. }
  455. public void removeDeclaration(CsmOffsetableDeclaration declaration) {
  456. CsmUID<CsmOffsetableDeclaration> uidDecl;
  457. try {
  458. declarationsLock.writeLock().lock();
  459. uidDecl = declarations.remove(getOffsetSortKey(declaration));
  460. sortedDeclarations = null;
  461. } finally {
  462. declarationsLock.writeLock().unlock();
  463. }
  464. RepositoryUtils.remove(uidDecl, declaration);
  465. // update repository
  466. put();
  467. }
  468. @Override
  469. public void write(RepositoryDataOutput output) throws IOException {
  470. super.write(output);
  471. UIDObjectFactory factory = UIDObjectFactory.getDefaultFactory();
  472. try {
  473. declarationsLock.readLock().lock();
  474. factory.writeOffsetSortedToUIDMap(this.declarations, output, false);
  475. } finally {
  476. declarationsLock.readLock().unlock();
  477. }
  478. try {
  479. staticLock.readLock().lock();
  480. UIDObjectFactory.getDefaultFactory().writeUIDCollection(staticFunctionDeclarationUIDs, output, false);
  481. UIDObjectFactory.getDefaultFactory().writeUIDCollection(staticVariableUIDs, output, false);
  482. } finally {
  483. staticLock.readLock().unlock();
  484. }
  485. }
  486. public static final class OffsetSortedKey implements Comparable<OffsetSortedKey>, Persistent, SelfPersistent {
  487. private final int start;
  488. private final int name;
  489. public OffsetSortedKey(CsmOffsetableDeclaration declaration) {
  490. start = ((CsmOffsetable) declaration).getStartOffset();
  491. name = Math.abs(declaration.getName().hashCode());
  492. }
  493. public OffsetSortedKey(int offset, int name) {
  494. start = offset;
  495. this.name = name;
  496. }
  497. @Override
  498. public int compareTo(OffsetSortedKey o) {
  499. int res = start - o.start;
  500. if (res == 0) {
  501. res = name - o.name;
  502. }
  503. return res;
  504. }
  505. @Override
  506. public boolean equals(Object obj) {
  507. if (obj instanceof OffsetSortedKey) {
  508. OffsetSortedKey key = (OffsetSortedKey) obj;
  509. return compareTo(key)==0;
  510. }
  511. return false;
  512. }
  513. @Override
  514. public int hashCode() {
  515. int hash = 7;
  516. hash = 37 * hash + this.start;
  517. hash = 37 * hash + this.name;
  518. return hash;
  519. }
  520. @Override
  521. public String toString() {
  522. return "OffsetSortedKey: " + this.name + "[" + this.start; // NOI18N
  523. }
  524. @Override
  525. public void write(RepositoryDataOutput output) throws IOException {
  526. output.writeInt(start);
  527. output.writeInt(name);
  528. }
  529. public OffsetSortedKey(RepositoryDataInput input) throws IOException {
  530. start = input.readInt();
  531. name = input.readInt();
  532. }
  533. }
  534. public static final class NameKey implements Comparable<NameKey> {
  535. private int start = 0;
  536. private CharSequence name;
  537. public NameKey(CsmUID<CsmOffsetableDeclaration> anUid) {
  538. name = UIDUtilities.getName(anUid);
  539. start = UIDUtilities.getStartOffset(anUid);
  540. }
  541. public NameKey(CharSequence name, int offset) {
  542. this.name = name;
  543. start = offset;
  544. }
  545. @Override
  546. public int compareTo(NameKey o) {
  547. int res = CharSequences.comparator().compare(name, o.name);
  548. if (res == 0) {
  549. res = start - o.start;
  550. }
  551. return res;
  552. }
  553. }
  554. }