/// <summary> /// Takes in 2 <see cref="IDataContainer"/> returns new instance of <see cref="IDataContainer"/> /// which containes properties which are both in LHS and RHS, the values will be from LHS /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns>LHS ^ RHS</returns> public static IDataContainer Intersect(this IDataContainer lhs, IDataContainer rhs) { IDataContainer intersect = lhs is IPropertyContainer ? (IDataContainer) new PropertyContainer() : new DataContainer(); intersect.Name = lhs.Name; var lhsKeys = lhs.GetKeys(); var rhsKeys = rhs.GetKeys(); var intersectKeys = lhsKeys.Intersect(rhsKeys); foreach (var key in intersectKeys) { DataObject first = lhs.Find(key); // in case of nested IDataContainer if (first.GetValue() is IDataContainer dc) { var second = rhs.Find(first.Name).GetValue() as IDataContainer; if (dc.IsIdentical(second) == false) { first.SetValue(dc.Intersect(second)); } } intersect.Add(first); } return(intersect); }
/// <summary> /// Takes in 2 <see cref="IDataContainer"/> returns a new instance of <see cref="IDataContainer"/> /// Which contains all the properties in LHS and RHS exception the ones that are common to both /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static IDataContainer Except(this IDataContainer lhs, IDataContainer rhs) { IDataContainer difference = lhs is IPropertyContainer ? (IDataContainer) new PropertyContainer() : new DataContainer(); difference.Name = lhs.Name; foreach (var data in lhs) { difference.Add(data); } var lhsKeys = lhs.GetKeys(); var rhsKeys = rhs.GetKeys(); var intersectKeys = lhsKeys.Intersect(rhsKeys); // no need to handle nested IDataContainer, the root will be removed, no need to worry about children. foreach (var key in intersectKeys) { difference.Remove(lhs.Find(key)); } return(difference); }
/// <summary> /// Create a nested data container. /// </summary> /// <param name="name"></param> /// <param name="containerBuilder"></param> /// <returns></returns> public DataContainerBuilder DataContainer(string name, Action <DataContainerBuilder> containerBuilder) { if (config.ContainsData(name)) { DataContainerEvents.NotifyInformation($"Attempted to add invalid value : {name}"); return(this); } var builder = Create(name); containerBuilder?.Invoke(builder); config.Add(new ContainerDataObject(name, builder.Build())); return(this); }
public static void PutValue(this IDataContainer container, string key, object value) { if (container.ContainsData(key)) { container.SetValue(key, value); } else { if (container is IPropertyContainer) { container.Add(DataObjectFactory.GetPropertyObjectFor(key, value)); } else { container.Add(DataObjectFactory.GetDataObjectFor(key, value)); } } }
/// <summary> /// Takes in 2 <see cref="IDataContainer"/> returns a new instance of <see cref="IDataContainer"/> /// which contains all the properties in <paramref name="lhs"/> and <paramref name="rhs"/> /// </summary> /// <param name="lhs"></param> /// <param name="rhs"> LHS V RHS</param> /// <returns></returns> public static IDataContainer Union(this IDataContainer lhs, IDataContainer rhs) { IDataContainer union = lhs is IPropertyContainer ? (IDataContainer) new PropertyContainer() : new DataContainer(); union.Name = lhs.Name; foreach (DataObject obj in lhs) { union.Add(obj); } foreach (DataObject obj in rhs) { if (union.ContainsData(obj.Name) == false) { union.Add(obj); } else { // in case of nested IDataContainer if (obj.GetValue() is IDataContainer dc) { var unionObj = union.Find(obj.Name); IDataContainer unionDC = unionObj.GetValue() as IDataContainer; if (unionDC.IsIdentical(dc) == false) { unionObj.SetValue(unionDC.Union(dc)); } } } } return(union); }
private IDataContainer ProcessInternal(IDataContainer dataContainer) { var allData = dataContainer.ToArray(); // Set new data to keep data container's meta information. dataContainer.Clear(); //Parallel.ForEach(allData, data => //{ // IData processedData = null; // try // { // processedData = Process(data); // } // catch (Exception e) // { // Log("Processing error {0}", e.ToString()); // } // if (processedData == null) // return; // dataContainer.Add(processedData); //}); foreach (var data in allData) { IData processedData = null; try { processedData = Process(data); } catch (Exception e) { LogFormat("Processing error {0}", e.ToString()); } if (processedData == null) { continue; } dataContainer.Add(processedData); } ; return(dataContainer.Any() ? dataContainer : null); }
public void IDataContainer_RaisesCollectionChangedOnAdd() { IDataContainer A = DataContainerBuilder.Create().Build(); var listener = new CollectionChangedListener(A); Assert.Null(listener.LastChange); DataObject newObject = DataObjectFactory.GetDataObjectFor("A", 1); A.Add(newObject); Assert.NotNull(listener.LastChange); Assert.Equal(NotifyCollectionChangedAction.Add, listener.LastChange.Action); Assert.Single(listener.LastChange.NewItems); Assert.Same(newObject, listener.LastChange.NewItems[0]); }
/// <summary> /// Implementation for <see cref="DataObject.ReadXmlElement(string, XmlReader)"/> /// </summary> /// <param name="elementName"></param> /// <param name="reader"></param> /// <returns></returns> protected override bool ReadXmlElement(string elementName, XmlReader reader) { // call base if (base.ReadXmlElement(elementName, reader)) { return(true); } /// Read DataObject implementation if (elementName == ContainerDataObject.DC_START_ELEMENT_NAME) { var obj = DataObjectFactory.GetDataObject("dc"); if (obj != null) { using (var newReader = XmlReader.Create(new StringReader(reader.ReadOuterXml()))) { newReader.Read(); obj.ReadXml(newReader); if (obj.Type != DataObjectType.NotSupported) { readingHelper.Add(obj); } } } return(true); } /// Read type info else if (elementName == nameof(TypeInfo)) { CollectionType = reader.ReadObjectXml <TypeInfo>(); return(true); } return(false); }
/// <summary> /// /// </summary> /// <param name="dataContainer"></param> /// <returns></returns> public override IDataContainer PreProcess(IDataContainer dataContainer) { var rgbImages = dataContainer.OfType<RgbImageData>().ToArray(); if (rgbImages.Any()) { if (_rgbImageData != null) _rgbImageData.Dispose(); _rgbImageData = rgbImages.First().Copy() as RgbImageData; return null; } if (_rgbImageData != null) { dataContainer.Add(_rgbImageData.Copy()); _rgbImageData.Dispose(); _rgbImageData = null; } return dataContainer; }
/// <summary> /// Add new properties from second to first, if they already exist, keep the values. /// Same as <see cref="Union(IDataContainer, IDataContainer)"/> but does operation inplace instead of returning new intance; /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> public static bool Merge(this IDataContainer lhs, IDataContainer rhs) { bool result = false; bool innerResult = false; foreach (var data in rhs) { if (lhs.ContainsData(data.Name) == false) { lhs.Add(data); result = true; } else { DataObject lhsData = lhs.Find(data.Name); // No need to update value, but update the details if (data is PropertyObject propRhs && lhsData is PropertyObject propLhs) { result = propLhs.DisplayName != propRhs.DisplayName || propLhs.Category != propRhs.Category || propLhs.Description != propRhs.Description; propLhs.DisplayName = propRhs.DisplayName; propLhs.Category = propRhs.Category; propLhs.Description = propRhs.Description; } if (lhsData.GetValue() is IDataContainer dc) { IDataContainer rhsDC = data.GetValue() as IDataContainer; bool temp = dc.Merge(rhsDC); innerResult = temp || innerResult; } } } return(result || innerResult); }
/// <summary> /// Implementation for <see cref="DataObject.ReadXmlElement(string, XmlReader)"/> /// </summary> /// <param name="elementName"></param> /// <param name="reader"></param> /// <returns></returns> protected override bool ReadXmlElement(string elementName, XmlReader reader) { // call base implementation if (base.ReadXmlElement(elementName, reader)) { return(true); } /// Read underlying type for <see cref="ContainerDataObject"/> created from CLR objects if (elementName == nameof(TypeInfo)) { ObjectType = reader.ReadObjectXml <TypeInfo>(); return(true); } /// Read <see cref="DataObject"/> implementation else { if (DataObjectFactory.GetPropertyObject(reader.GetAttribute(TYPE_ID_ATTRIBUTE)) is DataObject obj) { using (var newReader = XmlReader.Create(new StringReader(reader.ReadOuterXml()))) { newReader.Read(); obj.ReadXml(newReader); if (obj.Type != DataObjectType.NotSupported) { _container.Add(obj); } } return(true); } } return(false); }
/// <summary> /// /// </summary> /// <param name="dataContainer"></param> /// <returns></returns> public override IDataContainer PreProcess(IDataContainer dataContainer) { var rgbImages = dataContainer.OfType <RgbImageData>().ToArray(); if (rgbImages.Any()) { if (_rgbImageData != null) { _rgbImageData.Dispose(); } _rgbImageData = rgbImages.First().Copy() as RgbImageData; return(null); } if (_rgbImageData != null) { dataContainer.Add(_rgbImageData.Copy()); _rgbImageData.Dispose(); _rgbImageData = null; } return(dataContainer); }
public bool Add(string value) { return(_DataContainer.Add(value)); }
private IDataContainer ProcessInternal(IDataContainer dataContainer) { var allData = dataContainer.ToArray(); // Set new data to keep data container's meta information. dataContainer.Clear(); //Parallel.ForEach(allData, data => //{ // IData processedData = null; // try // { // processedData = Process(data); // } // catch (Exception e) // { // Log("Processing error {0}", e.ToString()); // } // if (processedData == null) // return; // dataContainer.Add(processedData); //}); foreach (var data in allData) { IData processedData = null; try { processedData = Process(data); } catch (Exception e) { LogFormat("Processing error {0}", e.ToString()); } if (processedData == null) continue; dataContainer.Add(processedData); }; return dataContainer.Any() ? dataContainer : null; }