duke@1: /*
duke@1: * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
duke@1: * published by the Free Software Foundation. Sun designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
duke@1: * by Sun in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1: * have any questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.comp;
duke@1:
jjg@119: import java.util.AbstractQueue;
jjg@119: import com.sun.tools.javac.util.Context;
jjg@119: import java.util.HashMap;
jjg@119: import java.util.Iterator;
jjg@119: import java.util.LinkedList;
jjg@119: import java.util.Map;
jjg@119: import java.util.Queue;
jjg@119: import javax.tools.JavaFileObject;
duke@1:
duke@1: /** A queue of all as yet unattributed classes.
duke@1: *
duke@1: *
This is NOT part of any API supported by Sun Microsystems. If
duke@1: * you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
jjg@119: public class Todo extends AbstractQueue> {
duke@1: /** The context key for the todo list. */
duke@1: protected static final Context.Key todoKey =
duke@1: new Context.Key();
duke@1:
duke@1: /** Get the Todo instance for this context. */
duke@1: public static Todo instance(Context context) {
duke@1: Todo instance = context.get(todoKey);
duke@1: if (instance == null)
duke@1: instance = new Todo(context);
duke@1: return instance;
duke@1: }
duke@1:
duke@1: /** Create a new todo list. */
duke@1: protected Todo(Context context) {
duke@1: context.put(todoKey, this);
duke@1: }
jjg@119:
jjg@119: public void append(Env env) {
jjg@119: add(env);
jjg@119: }
jjg@119:
jjg@119: @Override
jjg@119: public Iterator> iterator() {
jjg@119: return contents.iterator();
jjg@119: }
jjg@119:
jjg@119: @Override
jjg@119: public int size() {
jjg@119: return contents.size();
jjg@119: }
jjg@119:
jjg@119: public boolean offer(Env e) {
jjg@119: if (contents.add(e)) {
jjg@119: if (contentsByFile != null)
jjg@119: addByFile(e);
jjg@119: return true;
jjg@119: } else {
jjg@119: return false;
jjg@119: }
jjg@119: }
jjg@119:
jjg@119: public Env poll() {
jjg@119: if (size() == 0)
jjg@119: return null;
jjg@119: Env env = contents.remove(0);
jjg@119: if (contentsByFile != null)
jjg@119: removeByFile(env);
jjg@119: return env;
jjg@119: }
jjg@119:
jjg@119: public Env peek() {
jjg@119: return (size() == 0 ? null : contents.get(0));
jjg@119: }
jjg@119:
jjg@119: public Queue>> groupByFile() {
jjg@119: if (contentsByFile == null) {
jjg@119: contentsByFile = new LinkedList>>();
jjg@119: for (Env env: contents) {
jjg@119: addByFile(env);
jjg@119: }
jjg@119: }
jjg@119: return contentsByFile;
jjg@119: }
jjg@119:
jjg@119: private void addByFile(Env env) {
jjg@119: JavaFileObject file = env.toplevel.sourcefile;
jjg@119: if (fileMap == null)
jjg@119: fileMap = new HashMap();
jjg@119: FileQueue fq = fileMap.get(file);
jjg@119: if (fq == null) {
jjg@119: fq = new FileQueue();
jjg@119: fileMap.put(file, fq);
jjg@119: contentsByFile.add(fq);
jjg@119: }
jjg@119: fq.fileContents.add(env);
jjg@119: }
jjg@119:
jjg@119: private void removeByFile(Env env) {
jjg@119: JavaFileObject file = env.toplevel.sourcefile;
jjg@119: FileQueue fq = fileMap.get(file);
jjg@119: if (fq == null)
jjg@119: return;
jjg@119: if (fq.fileContents.remove(env)) {
jjg@119: if (fq.isEmpty()) {
jjg@119: fileMap.remove(file);
jjg@119: contentsByFile.remove(fq);
jjg@119: }
jjg@119: }
jjg@119: }
jjg@119:
jjg@119: LinkedList> contents = new LinkedList>();
jjg@119: LinkedList>> contentsByFile;
jjg@119: Map fileMap;
jjg@119:
jjg@119: class FileQueue extends AbstractQueue> {
jjg@119: @Override
jjg@119: public Iterator> iterator() {
jjg@119: return fileContents.iterator();
jjg@119: }
jjg@119:
jjg@119: @Override
jjg@119: public int size() {
jjg@119: return fileContents.size();
jjg@119: }
jjg@119:
jjg@119: public boolean offer(Env e) {
jjg@119: if (fileContents.offer(e)) {
jjg@119: contents.add(e);
jjg@119: return true;
jjg@119: }
jjg@119: return false;
jjg@119: }
jjg@119:
jjg@119: public Env poll() {
jjg@119: if (fileContents.size() == 0)
jjg@119: return null;
jjg@119: Env env = fileContents.remove(0);
jjg@119: contents.remove(env);
jjg@119: return env;
jjg@119: }
jjg@119:
jjg@119: public Env peek() {
jjg@119: return (fileContents.size() == 0 ? null : fileContents.get(0));
jjg@119: }
jjg@119:
jjg@119: LinkedList> fileContents = new LinkedList>();
jjg@119: }
duke@1: }