Coverage Report - org.jtheque.core.managers.update.versions.VersionsLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
VersionsLoader
0 %
0/24
0 %
0/14
2
 
 1  
 package org.jtheque.core.managers.update.versions;
 2  
 
 3  
 import org.jtheque.core.managers.error.IErrorManager;
 4  
 import org.jtheque.core.managers.module.beans.ModuleContainer;
 5  
 import org.jtheque.core.managers.update.Updatable;
 6  
 import org.jtheque.utils.bean.Version;
 7  
 import org.jtheque.utils.collections.CollectionUtils;
 8  
 
 9  
 import javax.annotation.Resource;
 10  
 import java.util.Collection;
 11  
 import java.util.HashMap;
 12  
 import java.util.Map;
 13  
 
 14  
 /*
 15  
  * This file is part of JTheque.
 16  
  *
 17  
  * JTheque is free software: you can redistribute it and/or modify
 18  
  * it under the terms of the GNU General Public License as published by
 19  
  * the Free Software Foundation, either version 3 of the License.
 20  
  *
 21  
  * JTheque is distributed in the hope that it will be useful,
 22  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 23  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 24  
  * GNU General Public License for more details.
 25  
  *
 26  
  * You should have received a copy of the GNU General Public License
 27  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 28  
  */
 29  
 
 30  
 /**
 31  
  * A loader for the online version's file. 
 32  
  * 
 33  
  * @author Baptiste Wicht 
 34  
  */
 35  
 public final class VersionsLoader implements IVersionsLoader {
 36  
     private final Map<Object, VersionsFile> cache;
 37  
 
 38  
     private static final int CACHE_INITIAL_SIZE = 20;
 39  
 
 40  
     @Resource
 41  
     private IErrorManager errorManager;
 42  
 
 43  
     /**
 44  
      * Construct a new VersionsLoader.
 45  
      */
 46  
     public VersionsLoader() {
 47  0
         super();
 48  
 
 49  0
         cache = new HashMap<Object, VersionsFile>(CACHE_INITIAL_SIZE);
 50  0
     }
 51  
 
 52  
     @Override
 53  
     public Version getVersion(Object object) {
 54  0
         if (isModule(object)) {
 55  0
             return new Version(((ModuleContainer) object).getInfos().version());
 56  0
         } else if (isUpdatable(object)) {
 57  0
             return ((Updatable) object).getVersion();
 58  
         }
 59  
 
 60  0
         return null;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Test if the object is a module.
 65  
      *
 66  
      * @param object The object to test.
 67  
      * @return true if the object is a module else false.
 68  
      */
 69  
     private static boolean isModule(Object object) {
 70  0
         return object instanceof ModuleContainer;
 71  
     }
 72  
 
 73  
     /**
 74  
      * Test if the object is an updatable.
 75  
      *
 76  
      * @param object The object to test.
 77  
      * @return true if the object is an updatable else false.
 78  
      */
 79  
     private static boolean isUpdatable(Object object) {
 80  0
         return object instanceof Updatable;
 81  
     }
 82  
 
 83  
     @Override
 84  
     public Collection<Version> getVersions(Object object) {
 85  0
         VersionsFile file = getVersionsFile(object);
 86  
 
 87  0
         return file == null ? CollectionUtils.<Version>emptyList() : CollectionUtils.expand(file.getVersions(), new VersionExpander());
 88  
     }
 89  
 
 90  
     @Override
 91  
     public Collection<OnlineVersion> getOnlineVersions(Object object) {
 92  0
         return getVersionsFile(object).getVersions();
 93  
     }
 94  
 
 95  
     @Override
 96  
     public OnlineVersion getOnlineVersion(Version version, Object object) {
 97  0
         for (OnlineVersion v : getOnlineVersions(object)) {
 98  0
             if (v.getVersion().equals(version)) {
 99  0
                 return v;
 100  
             }
 101  
         }
 102  
 
 103  0
         return null;
 104  
     }
 105  
 
 106  
     @Override
 107  
     public InstallVersion getInstallVersion(String versionFileURL) {
 108  0
         return new VersionsFileReader().read(versionFileURL).getInstallVersion();
 109  
     }
 110  
 
 111  
     @Override
 112  
     public Version getMostRecentVersion(Object object) {
 113  0
         return getVersionsFile(object).getMostRecentVersion().getVersion();
 114  
     }
 115  
 
 116  
     /**
 117  
      * Return the version's file of the object.
 118  
      *
 119  
      * @param object The object to get the version's file.
 120  
      * @return The version's file of the module.
 121  
      */
 122  
     private VersionsFile getVersionsFile(Object object) {
 123  0
         if (!cache.containsKey(object)) {
 124  0
             cache.put(object, new VersionsFileReader().read(object));
 125  
 
 126  0
             if (!cache.containsKey(object)) {
 127  0
                 errorManager.addInternationalizedError("error.update.internet");
 128  
             }
 129  
         }
 130  
 
 131  0
         return cache.get(object);
 132  
     }
 133  
 }