FSTDocumentSet.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  18. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  19. @class FSTDocument;
  20. NS_ASSUME_NONNULL_BEGIN
  21. /**
  22. * DocumentSet is an immutable (copy-on-write) collection that holds documents in order specified
  23. * by the provided comparator. We always add a document key comparator on top of what is provided
  24. * to guarantee document equality based on the key.
  25. */
  26. @interface FSTDocumentSet : NSObject
  27. /** Creates a new, empty FSTDocumentSet sorted by the given comparator, then by keys. */
  28. + (instancetype)documentSetWithComparator:(NSComparator)comparator;
  29. - (instancetype)init __attribute__((unavailable("Use a static constructor instead")));
  30. - (NSUInteger)count;
  31. /** Returns true if the dictionary contains no elements. */
  32. - (BOOL)isEmpty;
  33. /** Returns YES if this set contains a document with the given key. */
  34. - (BOOL)containsKey:(const firebase::firestore::model::DocumentKey &)key;
  35. /** Returns the document from this set with the given key if it exists or nil if it doesn't. */
  36. - (FSTDocument *_Nullable)documentForKey:(const firebase::firestore::model::DocumentKey &)key;
  37. /**
  38. * Returns the first document in the set according to its built in ordering, or nil if the set
  39. * is empty.
  40. */
  41. - (FSTDocument *_Nullable)firstDocument;
  42. /**
  43. * Returns the last document in the set according to its built in ordering, or nil if the set
  44. * is empty.
  45. */
  46. - (FSTDocument *_Nullable)lastDocument;
  47. /**
  48. * Returns the index of the document with the provided key in the document set. Returns NSNotFound
  49. * if the key is not present.
  50. */
  51. - (NSUInteger)indexOfKey:(const firebase::firestore::model::DocumentKey &)key;
  52. - (NSEnumerator<FSTDocument *> *)documentEnumerator;
  53. /** Returns a copy of the documents in this set as an array. This is O(n) on the size of the set. */
  54. - (NSArray<FSTDocument *> *)arrayValue;
  55. /**
  56. * Returns the documents as a FSTMaybeDocumentDictionary. This is O(1) as this leverages our
  57. * internal representation.
  58. */
  59. - (FSTMaybeDocumentDictionary *)dictionaryValue;
  60. /** Returns a new FSTDocumentSet that contains the given document. */
  61. - (instancetype)documentSetByAddingDocument:(FSTDocument *_Nullable)document;
  62. /** Returns a new FSTDocumentSet that excludes any document associated with the given key. */
  63. - (instancetype)documentSetByRemovingKey:(const firebase::firestore::model::DocumentKey &)key;
  64. @end
  65. NS_ASSUME_NONNULL_END