Example #1
0
        public static ICollection <U> Transform <T, U>(ICollection <T> target, CollectionUtils.Functor
                                                       <T, U> functor)
        {
            ICollection <U> result = new AList <U>();

            foreach (T element in target)
            {
                U mapped = functor.Invoke(element);
                if (mapped != null)
                {
                    result.AddItem(mapped);
                }
            }
            return(result);
        }
Example #2
0
 // Calls the block on every attachment dictionary. The block can return a different dictionary,
 // which will be replaced in the rev's properties. If it returns nil, the operation aborts.
 // Returns YES if any changes were made.
 public virtual bool MutateAttachments(CollectionUtils.Functor <IDictionary <string,
                                                                             object>, IDictionary <string, object> > functor)
 {
     {
         IDictionary <string, object> properties       = GetProperties();
         IDictionary <string, object> editedProperties = null;
         IDictionary <string, object> attachments      = (IDictionary <string, object>)properties
                                                         .Get("_attachments");
         IDictionary <string, object> editedAttachments = null;
         foreach (string name in attachments.Keys)
         {
             IDictionary <string, object> attachment = (IDictionary <string, object>)attachments
                                                       .Get(name);
             IDictionary <string, object> editedAttachment = functor.Invoke(attachment);
             if (editedAttachment == null)
             {
                 return(false);
             }
             // block canceled
             if (editedAttachment != attachment)
             {
                 if (editedProperties == null)
                 {
                     // Make the document properties and _attachments dictionary mutable:
                     editedProperties  = new Dictionary <string, object>(properties);
                     editedAttachments = new Dictionary <string, object>(attachments);
                     editedProperties.Put("_attachments", editedAttachments);
                 }
                 editedAttachments.Put(name, editedAttachment);
             }
         }
         if (editedProperties != null)
         {
             SetProperties(editedProperties);
             return(true);
         }
         return(false);
     }
 }