Package files

Source Code of files.FileManager

package files;


import java.util.concurrent.ConcurrentHashMap;

import common.files.FileDigest;
import common.files.SerializableFile;
import files.sharing.Sharer;
import files.sharing.SharingManager;


public class FileManager
{
  private ConcurrentHashMap<FileDigest, IndexedFile> files;
  private SharingManager sharingManager;

  public FileManager()
  {
    files = new ConcurrentHashMap<FileDigest, IndexedFile>();
    sharingManager = new SharingManager();
  }

  public void addSharedFile(SerializableFile file, int sharerId)
  {
    getIndexedFile(file).addSharer(sharingManager.getSharer(sharerId));
  }

  private IndexedFile getIndexedFile(SerializableFile file)
  {
    IndexedFile indexedFile = retrieveIndexedFile(file);
    if (indexedFile == null)
    {
      indexedFile = indexFile(file);
    }
    return indexedFile;
  }

  public IndexedFile retrieveIndexedFile(SerializableFile file)
  {
    return files.get(file.getHashData());
  }

  private IndexedFile indexFile(SerializableFile file)
  {
    IndexedFile newFile = new IndexedFile(file);
    IndexedFile previousFile = files.putIfAbsent(file.getHashData(), newFile);
    if (previousFile == null)
    {
      return newFile;
    }
    else
    {
      return previousFile;
    }
  }

  public void removeSharedFile(SerializableFile file, int sharerId)
  {
    IndexedFile indexedFile = retrieveIndexedFile(file);
    Sharer sharer = sharingManager.getSharer(sharerId);

    sharer.unregisterSharedFile(indexedFile);
    indexedFile.removeSharer(sharer);
  }

  public void removeIndexedFile(IndexedFile file)
  {
    files.remove(file.getHashData());
  }

  public void removeSharer(int id)
  {
    sharingManager.removeSharer(id);
  }

  public Sharer getSharerById(int id)
  {
    return sharingManager.getSharer(id);
  }

  public int getNumberOfFiles()
  {
    return files.size();
  }

}
TOP

Related Classes of files.FileManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.