/*
** Module   : InMemoryFileManager.java
** Abstract : in-memory file manager replacement (bypasses the file-system)
**
** Copyright (c) 2009, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090303 Provides an in-memory file manager replacement (bypasses
**                  the file-system).
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.compile;                            
                            
import javax.tools.*;
import java.util.*;
import java.io.*;

/**
 * Provides a file manager for the J2SE tools which replaces normal file-system
 * processing with in-memory equivalents. 
 */
public class InMemoryFileManager
extends ForwardingJavaFileManager<JavaFileManager>
{
   /** Stores output instances. */
   private Map<String, InMemoryClass> output =
      new HashMap<String, InMemoryClass>();
   
   /**
    * Construct an instance and link the forwarding superclass with the given
    * file manager for all non-overridden methods.
    *
    * @param    fileMgr
    *           The file manager to handle all processing that is not explicitly
    *           overridden by this class.
    */
   public InMemoryFileManager(JavaFileManager fileMgr)
   {
      super(fileMgr);
   }
   
   /**
    * Remove the class file for the named output destination and return it to
    * the caller. {@link #getJavaFileForOutput} must have been previously
    * called for this same class name.
    *
    * @param    className
    *           Fully qualified class name (includes package and any inner
    *           class extensions) in proper Java Language Specification format
    *           WITHOUT any ".class" suffix.
    *
    * @return   The output destination.
    */
   public InMemoryClass getClassFile(String className)
   {
      return output.remove(className);
   }
   
   /**
    * Obtain the output destination for the given class name.
    *
    * @param    location
    *           Ignored.
    * @param    className
    *           Fully qualified class name (includes package and any inner
    *           class extensions) in proper Java Language Specification format
    *           WITHOUT any ".class" suffix.
    * @param    kind
    *           Ignored.
    * @param    sibling
    *           Ignored.
    *
    * @return   The output destination that is in-memory based.
    */
   public JavaFileObject getJavaFileForOutput(Location            location,
                                              String              className,
                                              JavaFileObject.Kind kind,
                                              FileObject          sibling)
   throws IOException
   {
      InMemoryClass file = output.get(className);
      
      if (file == null)
      {
         file = new InMemoryClass(className);
         output.put(className, file);
      }
      
      return file; 
   }
};

