PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/cache/ParentToMethods.java

#
Java | 109 lines | 65 code | 18 blank | 26 comment | 10 complexity | 4688a97ede6833b072846e0bfd90be12 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * ParentToMethods.java -
  3. *
  4. * Copyright 2005 Robert McKinnon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.jedit.ruby.cache;
  21. import org.jedit.ruby.ast.Method;
  22. import org.jedit.ruby.ast.ParentMember;
  23. import java.util.*;
  24. /**
  25. * @author robmckinnon at users.sourceforge.net
  26. */
  27. final class ParentToMethods {
  28. private final Map<String, Set<Method>> fullNameToMethods = new HashMap<String, Set<Method>>();
  29. private final Map<String, Set<Method>> nameToMethods = new HashMap<String, Set<Method>>();
  30. private NameToParents nameToParents;
  31. private Set<Method> allMethods;
  32. final void setNameToParents(NameToParents nameToParents) {
  33. this.nameToParents = nameToParents;
  34. }
  35. final List<Method> getMethodList(String memberName) {
  36. Set<Method> methodSet = getMethodSet(memberName);
  37. List<Method> methods = new ArrayList<Method>(methodSet);
  38. if (methods.size() > 0) {
  39. Collections.sort(methods);
  40. }
  41. return methods;
  42. }
  43. final Set<Method> getMethodSet(String memberName) {
  44. Set<Method> methodSet = fullNameToMethods.get(memberName);
  45. if (methodSet == null) {
  46. methodSet = nameToMethods.get(memberName);
  47. }
  48. if (methodSet == null) {
  49. methodSet = new HashSet<Method>();
  50. }
  51. return methodSet;
  52. }
  53. /**
  54. * Note: Have to addMembers methods separately because there
  55. * may be some classes defined across more than one file.
  56. */
  57. final void add(ParentMember member) {
  58. Set<Method> methods = member.getMethods();
  59. add(member, methods);
  60. }
  61. final void add(ParentMember member, Set<Method> methods) {
  62. String fullName = member.getFullName();
  63. String name = member.getName();
  64. load(fullName, methods, name, fullNameToMethods, nameToMethods);
  65. }
  66. private static void load(String fullName, Set<Method> methods, String name, Map<String, Set<Method>> fullNameToMethods, Map<String, Set<Method>> nameToMethods) {
  67. if (fullNameToMethods.containsKey(fullName)) {
  68. fullNameToMethods.get(fullName).addAll(methods);
  69. nameToMethods.get(name).addAll(methods);
  70. } else {
  71. fullNameToMethods.put(fullName, methods);
  72. nameToMethods.put(name, methods);
  73. }
  74. }
  75. final void clear() {
  76. fullNameToMethods.clear();
  77. nameToMethods.clear();
  78. }
  79. final Set<Method> getAllMethods() {
  80. if(allMethods == null) {
  81. allMethods = new HashSet<Method>();
  82. for (String parentName : nameToParents.getAllParentNames()) {
  83. allMethods.addAll(getMethodList(parentName));
  84. }
  85. }
  86. return new HashSet<Method>(allMethods);
  87. }
  88. final void resetAllMethodsList() {
  89. allMethods = null;
  90. }
  91. }