/// <summary> /// Try and get the entry with a given name. /// </summary> /// <param name="name">The name of the entry to retrieve.</param> /// <param name="token">The token, if it is found.</param> /// <returns><see langword="true"/> if the token is found, <see langword="false"/> otherwise.</returns> public bool TryGet(NameToken name, out IToken token) { if (name == null) { throw new ArgumentNullException(nameof(name)); } return(Data.TryGetValue(name.Data, out token)); }
/// <summary> /// Try and get the entry with a given name and a specific data type. /// </summary> /// <typeparam name="T">The expected data type of the dictionary value.</typeparam> /// <param name="name">The name of the entry to retrieve.</param> /// <param name="token">The token, if it is found.</param> /// <returns><see langword="true"/> if the token is found with this type, <see langword="false"/> otherwise.</returns> public bool TryGet <T>(NameToken name, out T token) where T : IToken { token = default(T); if (!TryGet(name, out var t) || !(t is T typedToken)) { return(false); } token = typedToken; return(true); }
internal T Get <T>(NameToken name, IPdfTokenScanner scanner) where T : IToken { if (!TryGet(name, out var token) || !(token is T typedToken)) { if (!(token is IndirectReferenceToken indirectReference)) { throw new PdfDocumentFormatException($"Dictionary does not contain token with name {name} of type {typeof(T).Name}."); } typedToken = DirectObjectFinder.Get <T>(indirectReference, scanner); } return(typedToken); }
/// <summary> /// Try and get the entry with a given name and type or look-up the object if it's an indirect reference. /// </summary> internal bool TryGet <T>(NameToken name, IPdfTokenScanner tokenScanner, out T token) where T : IToken { token = default(T); if (!TryGet(name, out var t) || !(t is T typedToken)) { if (t is IndirectReferenceToken reference) { return(DirectObjectFinder.TryGet(reference, tokenScanner, out token)); } return(false); } token = typedToken; return(true); }
/// <summary> /// Create a copy of this dictionary with the additional entry (or override the value of the existing entry). /// </summary> /// <param name="key">The key of the entry to create or override.</param> /// <param name="value">The value of the entry to create or override.</param> /// <returns>A new <see cref="DictionaryToken"/> with the entry created or modified.</returns> public DictionaryToken With(NameToken key, IToken value) => With(key.Data, value);
/// <summary> /// Whether the dictionary contains an entry with this name. /// </summary> /// <param name="name">The name to check.</param> /// <returns><see langword="true"/> if the token is found, <see langword="false"/> otherwise.</returns> public bool ContainsKey(NameToken name) { return(Data.ContainsKey(name.Data)); }
/// <summary> /// Are these names identical? /// </summary> public bool Equals(NameToken other) { return(string.Equals(Data, other?.Data)); }
/// <summary> /// Creates a copy of this dictionary with the entry with the specified key removed (if it exists). /// </summary> /// <param name="key">The key of the entry to remove.</param> /// <returns>A new <see cref="DictionaryToken"/> with the entry removed.</returns> public DictionaryToken Without(NameToken key) => Without(key.Data);