PageRenderTime 118ms CodeModel.GetById 112ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 116 lines | 71 code | 19 blank | 26 comment | 10 complexity | d79325dffd245540a2c0e76c3b85e6f9 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.Member;
 23import org.jedit.ruby.ast.ParentMember;
 24import org.jedit.ruby.ast.ClassMember;
 25import org.jedit.ruby.ast.MemberVisitorAdapter;
 26
 27import java.util.*;
 28
 29/**
 30 * Manages mapping of module or class name
 31 * to cached {@link ParentMember} object
 32 * that have that name.
 33 * 
 34 * @author robmckinnon at users.sourceforge.net
 35 */
 36final class NameToParents {
 37
 38    private final Map<String, ParentMember> fullNameToMember = new HashMap<String, ParentMember>();
 39    private final Map<String, ParentMember> nameToMember = new HashMap<String, ParentMember>();
 40    private final ClassVisitor classVisitor = new ClassVisitor();
 41
 42    private List<Member> allMembers;
 43    private ParentToImmediateMethods parentToImmediateMethods;
 44
 45    final void add(ParentMember member) {
 46        String fullName = member.getFullName();
 47        String name = member.getName();
 48        fullNameToMember.put(fullName, member);
 49        nameToMember.put(name, member);
 50    }
 51
 52    final ClassMember getClass(String name) {
 53        ParentMember member = getMember(name);
 54
 55        if (member == null) {
 56            return null;
 57        } else {
 58            member.accept(classVisitor);
 59            return classVisitor.classMember;
 60        }
 61    }
 62
 63    final ParentMember getMember(String name) {
 64        if (fullNameToMember.containsKey(name)) {
 65            return fullNameToMember.get(name);
 66        } else if(nameToMember.containsKey(name)) {
 67            return nameToMember.get(name);
 68        } else {
 69            return null;
 70        }
 71    }
 72
 73    final void clear() {
 74        fullNameToMember.clear();
 75        nameToMember.clear();
 76    }
 77
 78    final List<Member> getAllMembers() {
 79        if (allMembers == null) {
 80            allMembers = new ArrayList<Member>();
 81
 82            for (String parentName : getAllParentNames()) {
 83                allMembers.add(fullNameToMember.get(parentName));
 84                allMembers.addAll(parentToImmediateMethods.getImmediateMethodList(parentName));
 85            }
 86        }
 87
 88        return allMembers;
 89    }
 90
 91    final List<String> getAllParentNames() {
 92        List<String> names = new ArrayList<String>(fullNameToMember.keySet());
 93        Collections.sort(names);
 94        return names;
 95    }
 96
 97    final Collection<ParentMember> getAllParents() {
 98        return fullNameToMember.values();
 99    }
100
101    final void reset() {
102        allMembers = null;
103    }
104
105    public final void setParentToImmediateMethods(ParentToImmediateMethods parentToImmediateMethods) {
106        this.parentToImmediateMethods = parentToImmediateMethods;
107    }
108
109    private static final class ClassVisitor extends MemberVisitorAdapter {
110        ClassMember classMember = null;
111
112        public final void handleClass(ClassMember clas) {
113            classMember = clas;
114        }
115    }
116}