package ai.subut.kurjun.repo.cache;


import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.google.inject.Singleton;

import ai.subut.kurjun.model.metadata.MetadataCache;
import ai.subut.kurjun.model.repository.Repository;


/**
 * Factory class for meta data cache instances. As meta data caching is done per repository, this factory returns the
 * same instance for a certain repository. Repositories are identified by their URLs.
 *
 */
@Singleton
public class MetadataCacheFactory
{

    private ConcurrentMap<String, MetadataCache> caches = new ConcurrentHashMap<>();


    public MetadataCache get( Repository repository )
    {
        String key = makeKey( repository );
        MetadataCache newCache = new MetadataCacheImpl( repository );

        MetadataCache existing = caches.putIfAbsent( key, newCache );
        return existing != null ? existing : newCache;
    }


    private String makeKey( Repository repository )
    {
        URL url = repository.getUrl();
        if ( url != null )
        {
            return url.toString();
        }
        throw new IllegalArgumentException( "Supplied repository does have its URL set." );
    }
}


@@ -47,15 +47,17 @@
 ava.
-net.URL
+util.UUID
 ;%0Aim
@@ -564,16 +564,22 @@
 private 
+final 
 Concurre
@@ -1024,14 +1024,14 @@
    U
-RL url
+UID id
  = r
@@ -1047,11 +1047,18 @@
 .get
-Url
+Identifier
 ();%0A
@@ -1070,19 +1070,18 @@
    if ( 
-url
+id
  != null
@@ -1116,11 +1116,10 @@
 urn 
-url
+id
 .toS
@@ -1207,16 +1207,20 @@
 ry does 
+not 
 have its
@@ -1220,19 +1220,26 @@
 ave its 
-URL
+identifier
  set.%22 )

use identifier as a key