Coverage Report - org.jtheque.core.managers.file.FileManager
 
Classes in this File Line Coverage Branch Coverage Complexity
FileManager
0 %
0/47
0 %
0/24
1.875
 
 1  
 package org.jtheque.core.managers.file;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.AbstractActivableManager;
 20  
 import org.jtheque.core.managers.file.able.BackupReader;
 21  
 import org.jtheque.core.managers.file.able.BackupWriter;
 22  
 import org.jtheque.core.managers.file.able.Backuper;
 23  
 import org.jtheque.core.managers.file.able.FileType;
 24  
 import org.jtheque.core.managers.file.able.Restorer;
 25  
 import org.jtheque.utils.StringUtils;
 26  
 import org.jtheque.utils.io.FileException;
 27  
 
 28  
 import java.io.File;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Arrays;
 31  
 import java.util.Collection;
 32  
 import java.util.EnumMap;
 33  
 import java.util.Map;
 34  
 
 35  
 /**
 36  
  * A FileManager implementation.
 37  
  *
 38  
  * @author Baptiste Wicht
 39  
  */
 40  
 public final class FileManager extends AbstractActivableManager implements IFileManager {
 41  
     private Backuper[] backupers;
 42  
     private Restorer[] restorers;
 43  
 
 44  
     private final Map<FileType, Collection<BackupWriter>> backupWriters;
 45  
     private final Map<FileType, Collection<BackupReader>> backupReaders;
 46  
 
 47  
     /**
 48  
      * Construct a new FileManager.
 49  
      */
 50  
     public FileManager() {
 51  0
         super();
 52  
 
 53  0
         backupWriters = new EnumMap<FileType, Collection<BackupWriter>>(FileType.class);
 54  0
         backupReaders = new EnumMap<FileType, Collection<BackupReader>>(FileType.class);
 55  0
     }
 56  
 
 57  
     @Override
 58  
     public void backup(FileType format, File file) throws FileException {
 59  0
         Backuper backuper = getBackuperFor(format);
 60  
 
 61  0
         if (backuper == null) {
 62  0
             throw new IllegalArgumentException("No backuper can be found for this format");
 63  
         }
 64  
 
 65  0
         backuper.backup(file, backupWriters.get(format));
 66  0
     }
 67  
 
 68  
     @Override
 69  
     public void restore(FileType format, File file) throws FileException {
 70  0
         Restorer restorer = getRestorerFor(format);
 71  
 
 72  0
         if (restorer == null) {
 73  0
             throw new IllegalArgumentException("No restorer can be found for this format");
 74  
         }
 75  
 
 76  0
         restorer.restore(file, backupReaders.get(format));
 77  0
     }
 78  
 
 79  
     @Override
 80  
     public void registerBackupWriter(FileType format, BackupWriter writer) {
 81  0
         Collection<BackupWriter> list = backupWriters.get(format);
 82  
 
 83  0
         if (list == null) {
 84  0
             list = new ArrayList<BackupWriter>(10);
 85  0
             backupWriters.put(format, list);
 86  
         }
 87  
 
 88  0
         list.add(writer);
 89  0
     }
 90  
 
 91  
     @Override
 92  
     public String formatUTFToWrite(String utf) {
 93  0
         return StringUtils.isEmpty(utf) ? IFileManager.UTF_NULL : utf;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public String formatUTFToRead(String utf) {
 98  0
         return IFileManager.UTF_NULL.equals(utf) ? "" : utf;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public void registerBackupReader(FileType format, BackupReader reader) {
 103  0
         Collection<BackupReader> list = backupReaders.get(format);
 104  
 
 105  0
         if (list == null) {
 106  0
             list = new ArrayList<BackupReader>(10);
 107  0
             backupReaders.put(format, list);
 108  
         }
 109  
 
 110  0
         list.add(reader);
 111  0
     }
 112  
 
 113  
     @Override
 114  
     public void unregisterBackupReader(FileType format, BackupReader reader) {
 115  0
         backupReaders.get(format).remove(reader);
 116  0
     }
 117  
 
 118  
     @Override
 119  
     public void unregisterBackupWriter(FileType format, BackupWriter writer) {
 120  0
         backupWriters.get(format).remove(writer);
 121  0
     }
 122  
 
 123  
     @Override
 124  
     public Collection<BackupReader> getBackupReaders(FileType format) {
 125  0
         return backupReaders.get(format);
 126  
     }
 127  
 
 128  
     @Override
 129  
     public boolean isBackupPossible(FileType format) {
 130  0
         return backupReaders.get(format) != null;
 131  
     }
 132  
 
 133  
     @Override
 134  
     public boolean isRestorePossible(FileType format) {
 135  0
         return backupWriters.get(format) != null;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Set the backupers. This is not for use, this is only for Spring Injection.
 140  
      *
 141  
      * @param backupers The backupers to set.
 142  
      */
 143  
     public void setBackupers(Backuper[] backupers) {
 144  0
         this.backupers = Arrays.copyOf(backupers, backupers.length);
 145  0
     }
 146  
 
 147  
     /**
 148  
      * Set the restorers.  This is not for use, this is only for Spring Injection.
 149  
      *
 150  
      * @param restorers The restorers to set.
 151  
      */
 152  
     public void setRestorers(Restorer[] restorers) {
 153  0
         this.restorers = Arrays.copyOf(restorers, restorers.length);
 154  0
     }
 155  
 
 156  
     /**
 157  
      * Return the Restorer for the format.
 158  
      *
 159  
      * @param format The format to search.
 160  
      * @return The Restorer.
 161  
      */
 162  
     private Restorer getRestorerFor(FileType format) {
 163  0
         for (Restorer b : restorers) {
 164  0
             if (b.canImportFrom(format)) {
 165  0
                 return b;
 166  
             }
 167  
         }
 168  
 
 169  0
         return null;
 170  
     }
 171  
 
 172  
     /**
 173  
      * Return the Backuper for the format.
 174  
      *
 175  
      * @param format The format to search.
 176  
      * @return The Backuper.
 177  
      */
 178  
     private Backuper getBackuperFor(FileType format) {
 179  0
         for (Backuper b : backupers) {
 180  0
             if (b.canExportTo(format)) {
 181  0
                 return b;
 182  
             }
 183  
         }
 184  
 
 185  0
         return null;
 186  
     }
 187  
 }