/*
 * 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.lang.ref.WeakReference;
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 final Map<String, WeakReference<Project>> projects = new ConcurrentHashMap<>();

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

      WeakReference<Project> ref = projects.get(dir.getFullyQualifiedName());

      Project project = null;
      if (ref != null)
         project = ref.get();

      return project;
   }

   @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.getRootDirectory().getFullyQualifiedName(), new WeakReference<Project>(project));
   }

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

}

@@ -224,44 +224,8 @@
 l;%0A%0A
-import java.lang.ref.WeakReference;%0A
 impo
@@ -263,29 +263,12 @@
 til.
-concurrent.Concurrent
+Weak
 Hash
@@ -742,38 +742,24 @@
 String, 
-WeakReference%3C
 Project%3E
 %3E projec
@@ -750,17 +750,16 @@
 Project%3E
-%3E
  project
@@ -770,18 +770,12 @@
 new 
-Concurrent
+Weak
 Hash
@@ -919,41 +919,29 @@
 %22);%0A
-%0A
       
-WeakReference%3CProject%3E ref
+Project project
  = p
@@ -986,93 +986,8 @@
 ));%0A
-%0A      Project project = null;%0A      if (ref != null)%0A         project = ref.get();%0A%0A
     
@@ -1288,43 +1288,16 @@
 (), 
-new WeakReference%3CProject%3E(
 project)
 );%0A 
@@ -1292,17 +1292,16 @@
 project)
-)
 ;%0A   %7D%0A%0A

Using WeakHashMap instead of a ConcurrentHashMap.