package org.jtheque.update.impl.versions;

import org.jtheque.core.able.Versionable;
import org.jtheque.errors.able.IErrorService;
import org.jtheque.resources.impl.DescriptorReader;
import org.jtheque.resources.impl.ModuleDescriptor;
import org.jtheque.resources.impl.ModuleVersion;
import org.jtheque.utils.bean.Version;
import org.jtheque.utils.collections.CollectionUtils;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/*
 * Copyright JTheque (Baptiste Wicht)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * A loader for the online version's file.
 *
 * @author Baptiste Wicht
 */
public final class DescriptorsLoader implements IVersionsLoader {
    private static final int CACHE_INITIAL_SIZE = 20;

    private final Map<Object, ModuleDescriptor> cache = new HashMap<Object, ModuleDescriptor>(CACHE_INITIAL_SIZE);

    @Resource
    private IErrorService errorService;

    @Override
    public Collection<Version> getVersions(Versionable object) {
        ModuleDescriptor descriptor = getModuleDescriptor(object);

        if (descriptor != null) {
            Collection<Version> versions = new ArrayList<Version>(descriptor.getVersions().size());

            for (ModuleVersion v : descriptor.getVersions()) {
                versions.add(v.getVersion());
            }

            return versions;
        }

        return CollectionUtils.emptyList();
    }

    @Override
    public Collection<ModuleVersion> getOnlineVersions(Versionable object) {
        return getModuleDescriptor(object).getVersions();
    }

    @Override
    public ModuleVersion getModuleVersion(Version version, Versionable object) {
        for (ModuleVersion m : getOnlineVersions(object)) {
            if (m.getVersion().equals(version)) {
                return m;
            }
        }

        return null;
    }

    @Override
    public Version getMostRecentVersion(Versionable object) {
        ModuleDescriptor versionsFile = getModuleDescriptor(object);

        return versionsFile != null ? versionsFile.getMostRecentVersion().getVersion() : null;
    }

    @Override
    public ModuleVersion getMostRecentModuleVersion(String url) {
        ModuleDescriptor versionsFile = DescriptorReader.readModuleDescriptor(url);

        return versionsFile != null ? versionsFile.getMostRecentVersion() : null;
    }

    /**
     * Return the version's file of the object.
     *
     * @param object The object to get the version's file.
     *
     * @return The version's file of the module.
     */
    private ModuleDescriptor getModuleDescriptor(Versionable object) {
        if (!cache.containsKey(object)) {
            cache.put(object, DescriptorReader.readModuleDescriptor(object));

            if (!cache.containsKey(object)) {
                errorService.addInternationalizedError("error.update.internet");
            }
        }

        return cache.get(object);
    }
}

@@ -1312,10 +1312,10 @@
 E = 
+1
 2
-0
 ;%0A%0A 
@@ -1331,30 +1331,30 @@
 e final Map%3C
-Object
+String
 , ModuleDesc
@@ -1385,14 +1385,14 @@
 Map%3C
-Object
+String
 , Mo
@@ -1619,32 +1619,51 @@
 escriptor(object
+.getDescriptorURL()
 );%0A%0A        if (
@@ -2132,16 +2132,35 @@
 r(object
+.getDescriptorURL()
 ).getVer
@@ -2601,16 +2601,35 @@
 r(object
+.getDescriptorURL()
 );%0A%0A    
@@ -2846,37 +2846,19 @@
 sFile = 
-DescriptorReader.read
+get
 ModuleDe
@@ -3043,51 +3043,37 @@
 ram 
-object The object to get the version's file
+url The url of the descriptor
 .%0A  
@@ -3183,34 +3183,26 @@
 criptor(
-Versionable object
+String url
 ) %7B%0A    
@@ -3220,38 +3220,35 @@
 che.containsKey(
-object
+url
 )) %7B%0A           
@@ -3258,22 +3258,19 @@
 che.put(
-object
+url
 , Descri
@@ -3301,22 +3301,19 @@
 criptor(
-object
+url
 ));%0A%0A   
@@ -3344,22 +3344,19 @@
 ainsKey(
-object
+url
 )) %7B%0A   
@@ -3483,22 +3483,19 @@
 che.get(
-object
+url
 );%0A    %7D

Improve caching of descriptors