/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.addon.projects.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Singleton;

import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.spi.ProjectCache;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.furnace.util.Assert;

/**
 * A simple in-memory {@link ProjectCache}.
 * 
 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 */
@Singleton
public class InMemoryProjectCache implements ProjectCache
{
   private Map<String, Project> projects = new ConcurrentHashMap<String, Project>();

   @Override
   public Project get(DirectoryResource dir)
   {
      Assert.notNull(dir, "Directory Resource should not be null");
      return projects.get(dir.getFullyQualifiedName());
   }

   @Override
   public void invalidate()
   {
      this.projects.clear();
   }

   @Override
   public void store(Project project)
   {
      Assert.notNull(project, "Project should not be null");
      this.projects.put(project.getProjectRoot().getFullyQualifiedName(), project);
   }

   @Override
   public void evict(Project project)
   {
      this.projects.remove(project.getProjectRoot().getFullyQualifiedName());
   }

}

@@ -220,16 +220,52 @@
 .impl;%0A%0A
+import java.lang.ref.WeakReference;%0A
 import j
@@ -278,16 +278,16 @@
 il.Map;%0A
-
 import j
@@ -777,16 +777,22 @@
 private 
+final 
 Map%3CStri
@@ -795,24 +795,38 @@
 String, 
+WeakReference%3C
 Project%3E
  project
@@ -817,16 +817,17 @@
 Project%3E
+%3E
  project
@@ -860,24 +860,38 @@
 String, 
+WeakReference%3C
 Project%3E
 ();%0A%0A   
@@ -882,16 +882,17 @@
 Project%3E
+%3E
 ();%0A%0A   
@@ -1023,20 +1023,43 @@
 %22);%0A
+%0A
       
-return
+WeakReference%3CProject%3E ref =
  pro
@@ -1093,24 +1093,131 @@
 fiedName());
+%0A%0A      Project project = null;%0A      if (ref != null)%0A         project = ref.get();%0A%0A      return project;
 %0A   %7D%0A%0A   @O
@@ -1483,24 +1483,51 @@
 Name(), 
+new WeakReference%3CProject%3E(
 project)
 ;%0A   %7D%0A%0A
@@ -1518,16 +1518,17 @@
 project)
+)
 ;%0A   %7D%0A%0A

Fix memory leak in Projects addon