PageRenderTime 390ms CodeModel.GetById 378ms app.highlight 9ms RepoModel.GetById 1ms 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
  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 */
 20package org.jedit.ruby.cache;
 21
 22import org.jedit.ruby.ast.Method;
 23import org.jedit.ruby.ast.ParentMember;
 24
 25import java.util.*;
 26
 27/**
 28 * @author robmckinnon at users.sourceforge.net
 29 */
 30final class ParentToMethods {
 31
 32    private final Map<String, Set<Method>> fullNameToMethods = new HashMap<String, Set<Method>>();
 33    private final Map<String, Set<Method>> nameToMethods = new HashMap<String, Set<Method>>();
 34    private NameToParents nameToParents;
 35    private Set<Method> allMethods;
 36
 37    final void setNameToParents(NameToParents nameToParents) {
 38        this.nameToParents = nameToParents;
 39    }
 40
 41    final List<Method> getMethodList(String memberName) {
 42        Set<Method> methodSet = getMethodSet(memberName);
 43        List<Method> methods = new ArrayList<Method>(methodSet);
 44
 45        if (methods.size() > 0) {
 46            Collections.sort(methods);
 47        }
 48
 49        return methods;
 50    }
 51
 52    final Set<Method> getMethodSet(String memberName) {
 53        Set<Method> methodSet = fullNameToMethods.get(memberName);
 54        if (methodSet == null) {
 55            methodSet = nameToMethods.get(memberName);
 56        }
 57        if (methodSet == null) {
 58            methodSet = new HashSet<Method>();
 59        }
 60        return methodSet;
 61    }
 62
 63    /**
 64     * Note: Have to addMembers methods separately because there
 65     * may be some classes defined across more than one file.
 66     */
 67    final void add(ParentMember member) {
 68        Set<Method> methods = member.getMethods();
 69        add(member, methods);
 70    }
 71
 72    final void add(ParentMember member, Set<Method> methods) {
 73        String fullName = member.getFullName();
 74        String name = member.getName();
 75
 76        load(fullName, methods, name, fullNameToMethods, nameToMethods);
 77    }
 78
 79    private static void load(String fullName, Set<Method> methods, String name, Map<String, Set<Method>> fullNameToMethods, Map<String, Set<Method>> nameToMethods) {
 80        if (fullNameToMethods.containsKey(fullName)) {
 81            fullNameToMethods.get(fullName).addAll(methods);
 82            nameToMethods.get(name).addAll(methods);
 83        } else {
 84            fullNameToMethods.put(fullName, methods);
 85            nameToMethods.put(name, methods);
 86        }
 87    }
 88
 89    final void clear() {
 90        fullNameToMethods.clear();
 91        nameToMethods.clear();
 92    }
 93
 94    final Set<Method> getAllMethods() {
 95        if(allMethods == null) {
 96            allMethods = new HashSet<Method>();
 97
 98            for (String parentName : nameToParents.getAllParentNames()) {
 99                allMethods.addAll(getMethodList(parentName));
100            }
101        }
102
103        return new HashSet<Method>(allMethods);
104    }
105
106    final void resetAllMethodsList() {
107        allMethods = null;
108    }
109}