using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace VersOne.Epub
{
///
/// A container for a subset of content files within the EPUB book.
///
/// The type of the content files stored within the collection.
/// The type of the content files stored within the collection.
public class EpubContentCollection
where TLocalContentFile : EpubLocalContentFile
where TRemoteContentFile : EpubRemoteContentFile
{
private readonly Dictionary localByKey;
private readonly Dictionary localByFilePath;
private readonly Dictionary remoteByUrl;
///
/// Initializes a new instance of the class.
///
/// Local content files to be stored within this container.
/// Remote content files to be stored within this container.
public EpubContentCollection(ReadOnlyCollection? local = null, ReadOnlyCollection? remote = null)
{
Local = local ?? new ReadOnlyCollection(new List());
Remote = remote ?? new ReadOnlyCollection(new List());
localByKey = new Dictionary();
localByFilePath = new Dictionary();
foreach (TLocalContentFile localContentFile in Local)
{
if (localByKey.ContainsKey(localContentFile.Key))
{
throw new EpubPackageException($"Incorrect EPUB manifest: item with href = \"{localContentFile.Key}\" is not unique.");
}
localByKey.Add(localContentFile.Key, localContentFile);
if (localByFilePath.ContainsKey(localContentFile.FilePath))
{
throw new EpubPackageException($"Incorrect EPUB manifest: item with absolute file path = \"{localContentFile.FilePath}\" is not unique.");
}
localByFilePath.Add(localContentFile.FilePath, localContentFile);
}
remoteByUrl = new Dictionary();
foreach (TRemoteContentFile remoteContentFile in Remote)
{
if (remoteByUrl.ContainsKey(remoteContentFile.Url))
{
throw new EpubPackageException($"Incorrect EPUB manifest: item with href = \"{remoteContentFile.Url}\" is not unique.");
}
remoteByUrl.Add(remoteContentFile.Url, remoteContentFile);
}
}
///
/// Gets a collection of local content files stored within this container.
///
public ReadOnlyCollection Local { get; }
///
/// Gets a collection of remote content files stored within this container.
///
public ReadOnlyCollection Remote { get; }
///
/// Determines whether a local content file with the specified value exists in this container.
///
/// The value of the local content file to locate in this container.
///
/// true if the local content file with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool ContainsLocalFileWithKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return localByKey.ContainsKey(key);
}
///
/// Gets the local content file with the specified value.
///
/// The of the local content file to get.
/// The local content file with the specified value.
/// is null.
///
/// Local content file with the specified value does not exist in this container.
///
public TLocalContentFile GetLocalFileByKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
try
{
return localByKey[key];
}
catch (KeyNotFoundException)
{
throw new EpubContentCollectionException($"Local content file with key = \"{key}\" does not exist in this container.");
}
}
///
/// Gets the local content file with the specified value.
///
/// The of the local content file to get.
///
/// When this method returns, contains the local content file with the specified value,
/// if such local content file exists in the container; otherwise, null.
///
/// true if the local content file with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool TryGetLocalFileByKey(string key, out TLocalContentFile localContentFile)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return localByKey.TryGetValue(key, out localContentFile);
}
///
/// Determines whether a local content file with the specified value exists in this container.
///
/// The value of the local content file to locate in this container.
///
/// true if the local content file with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool ContainsLocalFileWithFilePath(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
return localByFilePath.ContainsKey(filePath);
}
///
/// Gets the local content file with the specified value.
///
/// The of the local content file to get.
/// The local content file with the specified value.
/// is null.
///
/// Local content file with the specified value does not exist in this container.
///
public TLocalContentFile GetLocalFileByFilePath(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
try
{
return localByFilePath[filePath];
}
catch (KeyNotFoundException)
{
throw new EpubContentCollectionException($"Local content file with file path = \"{filePath}\" does not exist in this container.");
}
}
///
/// Gets the local content file with the specified value.
///
/// The of the local content file to get.
///
/// When this method returns, contains the local content file with the specified value,
/// if such local content file exists in the container; otherwise, null.
///
/// true if the local content file with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool TryGetLocalFileByFilePath(string filePath, out TLocalContentFile localContentFile)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
return localByFilePath.TryGetValue(filePath, out localContentFile);
}
///
/// Determines whether a remote content file with the specified value exists in this container.
///
/// The value of the remote content file to locate in this container.
///
/// true if the remote content file with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool ContainsRemoteFileWithUrl(string url)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
return remoteByUrl.ContainsKey(url);
}
///
/// Gets the remote content file with the specified value.
///
/// The of the remote content file to get.
/// The remote content file with the specified value.
/// is null.
///
/// Remote content file with the specified value does not exist in this container.
///
public TRemoteContentFile GetRemoteFileByUrl(string url)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
try
{
return remoteByUrl[url];
}
catch (KeyNotFoundException)
{
throw new EpubContentCollectionException($"Remote content file with URL = \"{url}\" does not exist in this container.");
}
}
///
/// Gets the remote content file with the specified value.
///
/// The of the remote content file to get.
///
/// When this method returns, contains the remote content file with the specified value,
/// if such remote content file exists in the container; otherwise, null.
///
/// true if the remote content file with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool TryGetRemoteFileByUrl(string url, out TRemoteContentFile remoteContentFile)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
return remoteByUrl.TryGetValue(url, out remoteContentFile);
}
}
}