/// <summary>
        /// Where
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="predicate"></param>
        /// <param name="capacity"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IQueueSource <T> Where <T>(this IQueueSource <T> queue, Func <T, Task <bool> > predicate, int?capacity = null)
        {
            AsyncQueue <T> outQueue = new AsyncQueue <T>(capacity ?? 2);

            Func <Task> worker = async delegate {
                while (true)
                {
                    IOptional <T> item = await queue.Dequeue(CancellationToken.None);

                    if (!item.HasValue)
                    {
                        break;
                    }
                    if (await predicate(item.Value))
                    {
                        await outQueue.Enqueue(item.Value, CancellationToken.None);
                    }
                }

                outQueue.WriteEof();
            };

            Task _dummy = Task.Run(worker);

            return(outQueue);
        }
        /// <summary>
        /// Select
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="func"></param>
        /// <param name="capacity"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <returns></returns>
        public static IQueueSource <U> Select <T, U>(this IQueueSource <T> queue, Func <T, Task <U> > func, int?capacity = null)
        {
            AsyncQueue <U> outQueue = new AsyncQueue <U>(capacity ?? 2);

            Func <Task> worker = async delegate {
                while (true)
                {
                    IOptional <T> item = await queue.Dequeue(CancellationToken.None);

                    if (!item.HasValue)
                    {
                        break;
                    }
                    U item2 = await func(item.Value);

                    await outQueue.Enqueue(item2, CancellationToken.None);
                }

                outQueue.WriteEof();
            };

            Task.Run(worker);

            return(outQueue);
        }
        private void SetVisibility()
        {
            if (string.IsNullOrWhiteSpace(PropertyBinding))
            {
                return;
            }

            Type         metadata = Owner.SyntaxNode.GetType();
            PropertyInfo property = metadata.GetProperty(PropertyBinding);

            if (property == null)
            {
                return;
            }

            if (property.IsOptional())
            {
                IOptional optional = (IOptional)property.GetValue(Owner.SyntaxNode);
                IsVisible           = optional.HasValue;
                IsTemporallyVisible = false;
                ResetHideOptionFlag = false;
            }
            else
            {
                IsVisible           = true;
                IsTemporallyVisible = false;
                ResetHideOptionFlag = false;
            }
        }
Пример #4
0
        /// <summary>
        /// propertyName parameter must be of type generic list
        /// </summary>
        public ISyntaxNode Create(ISyntaxNode concept, string propertyName, Type child)
        {
            ISyntaxNode item = (ISyntaxNode)Activator.CreateInstance(child);

            item.Parent = concept;

            IList        list;
            PropertyInfo property = concept.GetType().GetProperty(propertyName);

            if (property.IsOptional())
            {
                IOptional optional = (IOptional)property.GetValue(concept);
                list = (IList)optional.Value;
                if (list == null)
                {
                    Type listType = property.PropertyType.GetProperty("Value").PropertyType;
                    list           = (IList)Activator.CreateInstance(listType);
                    optional.Value = list;
                }
            }
            else
            {
                list = (IList)property.GetValue(concept);
                if (list == null)
                {
                    Type listType = property.PropertyType;
                    list = (IList)Activator.CreateInstance(listType);
                    property.SetValue(concept, list);
                }
            }

            list.Add(item);
            return(item);
        }
Пример #5
0
        public void ForCharacteraandStringEmptyStringShouldReturnEmptyString()
        {
            var    a      = new IOptional(new Character('a'));
            IMatch actual = a.Match(string.Empty);

            Assert.Equal(string.Empty, actual.RemainingText());
        }
        private ISyntaxNode GetConceptFromPropertyBinding(ISyntaxNodeViewModel conceptViewModel)
        {
            if (conceptViewModel.Owner == null)
            {
                return(conceptViewModel.SyntaxNode); // it is syntax tree root node
            }
            ISyntaxNodeViewModel parentNode = conceptViewModel.Owner;
            string      propertyBinding     = conceptViewModel.PropertyBinding;
            ISyntaxNode parentConcept       = parentNode.SyntaxNode;

            PropertyInfo property = parentConcept.GetPropertyInfo(propertyBinding);
            IOptional    optional = null;

            if (property.IsOptional())
            {
                optional = (IOptional)property.GetValue(parentConcept);
            }
            ISyntaxNode concept = null;

            if (optional == null)
            {
                concept = (ISyntaxNode)property.GetValue(parentConcept);
            }
            else if (optional.HasValue)
            {
                concept = (ISyntaxNode)optional.Value;
            }
            return(concept);
        }
Пример #7
0
        public void ForCharacterMinusandStringMinus123ShouldReturn123()
        {
            var    a      = new IOptional(new Character('-'));
            IMatch actual = a.Match("-123");

            Assert.Equal("123", actual.RemainingText());
        }
Пример #8
0
        public void ForCharacteraandStringbcShouldReturnbc()
        {
            var    a      = new IOptional(new Character('a'));
            IMatch actual = a.Match("bc");

            Assert.Equal("bc", actual.RemainingText());
        }
Пример #9
0
        public void ForCharacterAAndStringAbcShouldReturnTrue()
        {
            IOptional a      = new IOptional(new Character('a'));
            IMatch    actual = a.Match(Text);

            Assert.True(actual.Succes());
        }
Пример #10
0
        /**
         * Creates a new Optional as a blank node in a given Model.
         * @param model  the Model to create the OPTIONAL in
         * @param elements  the elements of the OPTIONAL
         * @return a new Optional
         */
        public static IOptional createOptional(SpinProcessor model, IElementList elements)
        {
            IOptional optional = (IOptional)model.CreateResource(SP.ClassOptional).As(typeof(OptionalImpl));

            optional.AddProperty(SP.PropertyElements, elements);
            return(optional);
        }
Пример #11
0
        public static ISyntaxNode CreateRepeatableConcept(Type repeatable, ISyntaxNode parent, string propertyName)
        {
            ISyntaxNode concept = (ISyntaxNode)Activator.CreateInstance(repeatable);

            concept.Parent = parent;

            IList        list;
            PropertyInfo property = parent.GetPropertyInfo(propertyName);

            if (property.IsOptional())
            {
                IOptional optional = (IOptional)property.GetValue(parent);
                list = (IList)optional.Value;
                if (list == null)
                {
                    Type listType = property.PropertyType.GetProperty("Value").PropertyType;
                    list           = (IList)Activator.CreateInstance(listType);
                    optional.Value = list;
                }
            }
            else
            {
                list = (IList)property.GetValue(parent);
                if (list == null)
                {
                    Type listType = property.PropertyType;
                    list = (IList)Activator.CreateInstance(listType);
                    property.SetValue(parent, list);
                }
            }
            list.Add(concept);
            return(concept);
        }
Пример #12
0
        public void RemoveOption(string propertyName)
        {
            if (string.IsNullOrWhiteSpace(propertyName))
            {
                return;
            }

            PropertyInfo property = SyntaxNode.GetPropertyInfo(propertyName);

            if (!property.IsOptional())
            {
                return;
            }
            IOptional optional = (IOptional)property.GetValue(SyntaxNode);

            if (!optional.HasValue)
            {
                return;
            }

            optional.HasValue = false;
            var nodes = this.GetNodesByPropertyName(propertyName);

            foreach (var node in nodes)
            {
                node.IsVisible = false;
            }
        }
Пример #13
0
 public GenericModConfigMenuSetup(
     IModHelper helper,
     IManifest manifest,
     IOptional <IGenericModConfigMenuApi> configApiFactory,
     HudConfig hudConfig,
     ConfigManager <HudConfig> hudConfigManager,
     FishConfig fishConfig,
     ConfigManager <FishConfig> fishConfigManager,
     TreasureConfig treasureConfig,
     ConfigManager <TreasureConfig> treasureConfigManager
     )
 {
     this.helper    = helper ?? throw new ArgumentNullException(nameof(helper));
     this.manifest  = manifest ?? throw new ArgumentNullException(nameof(manifest));
     this.configApi = configApiFactory
                      ?? throw new ArgumentNullException(nameof(configApiFactory));
     this.hudConfig        = hudConfig ?? throw new ArgumentNullException(nameof(hudConfig));
     this.hudConfigManager = hudConfigManager
                             ?? throw new ArgumentNullException(nameof(hudConfigManager));
     this.fishConfig        = fishConfig ?? throw new ArgumentNullException(nameof(fishConfig));
     this.fishConfigManager = fishConfigManager
                              ?? throw new ArgumentNullException(nameof(fishConfigManager));
     this.treasureConfig =
         treasureConfig ?? throw new ArgumentNullException(nameof(treasureConfig));
     this.treasureConfigManager = treasureConfigManager
                                  ?? throw new ArgumentNullException(nameof(treasureConfigManager));
 }
Пример #14
0
        public void ForCharacteraandStringnullShouldReturnTrue()
        {
            var    a      = new IOptional(new Character('a'));
            IMatch actual = a.Match(null);

            Assert.True(actual.Succes());
        }
Пример #15
0
        public void ForCharacteraandStringnullShouldReturnnull()
        {
            var    a      = new IOptional(new Character('a'));
            IMatch actual = a.Match(null);

            Assert.Null(actual.RemainingText());
        }
Пример #16
0
        public void ForCharacterMinusandString123ShouldReturntrue()
        {
            var    sign   = new IOptional(new Character('-'));
            IMatch actual = sign.Match("123");

            Assert.True(actual.Succes());
        }
Пример #17
0
 public override bool Equals(object obj)
 {
     return(obj switch {
         IOptional <TValue> optional => Equals(optional),
         TValue value => Equals(value),
         _ => Equals(this, obj)
     });
Пример #18
0
 public IPosition GetInterpolatedFrame(
     float frame,
     IOptional <float[]>?defaultAxes = null,
     bool useLoopingInterpolation    = false)
 => this.impl_.GetInterpolatedFrame(
     frame,
     defaultAxes,
     useLoopingInterpolation);
Пример #19
0
        public IOptional <TReject> TryResolveAndReject(IDependencyResolver resolver, TTarget target)
        {
            IOptional <TResolvableValue> resolvedValue = TryResolve(resolver);

            return(resolvedValue.HasValue
                ? Reject(target, resolvedValue.Value)
                : Optional <TReject> .NoValue);
        }
Пример #20
0
        /// <summary>
        /// Registration.
        /// </summary>
        /// <param name="registration"></param>
        /// <param name="instance"></param>
        public RegistrationProcess(Registration registration, object instance)
        {
            Argument.NotNull(nameof(registration), registration);
            Argument.NotNull(nameof(instance), instance);

            Registration = registration;
            Instance     = new Optional <object>(instance);
        }
Пример #21
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="resolution">Resolution.</param>
        public ParameterInjection(string name, Resolution resolution)
        {
            Argument.NotNull(nameof(name), name);
            Argument.NotNull(nameof(resolution), resolution);

            Name       = name;
            Resolution = new Optional <Resolution>(resolution);
        }
Пример #22
0
 public static Option<T> TryCast<T>(this IOptional option) where T : class
 {
     return option
         .MatchUntyped(
             x => x is T t
                 ? t
                 : default(Option<T>),
             () => default);
Пример #23
0
    public ItemAttributes RemoveItemInHand()
    {
        AssertItemInHand();
        var returnItem = this.itemInHand.GetValue();

        this.itemInHand = Optional.None <ItemAttributes>("Removed item in hand to place in inventory");
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
        return(returnItem);
    }
 /// <summary>
 /// Value or throw
 /// </summary>
 /// <param name="optional"></param>
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="TException"></typeparam>
 /// <returns></returns>
 public static T ValueOrThrow <T, TException>(this IOptional <T, TException> optional)
     where TException : Exception
 {
     if (optional.HasValue)
     {
         return(optional.Value);
     }
     throw optional.Exception;
 }
Пример #25
0
        public DialogBase()
        {
            InitializeComponent();

            this.optional = this as IOptional;

            this.StartPosition = FormStartPosition.CenterParent;
            this.MinimumSize   = this.Size;
        }
Пример #26
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="interfaceType"></param>
        /// <param name="name"></param>
        /// <param name="injections"></param>
        public Resolution(
            Type interfaceType,
            string name           = null,
            Injections injections = null)
        {
            Argument.NotNull(nameof(interfaceType), interfaceType);

            InterfaceType = interfaceType;
            Name          = new Optional <string>(name);
            Injections    = new Optional <Injections>(injections);
        }
Пример #27
0
        public bool TryResolveAndInject(IDependencyResolver resolver, TTarget target)
        {
            IOptional <TResolvableValue> resolvedValue = TryResolve(resolver);

            if (!resolvedValue.HasValue)
            {
                return(false);
            }

            Inject(target, resolvedValue.Value);
            return(true);
        }
Пример #28
0
        public bool TryResolve(IDependencyResolver resolver, out object value)
        {
            IOptional <TValue> resolvedValue = TryResolve(resolver);

            if (resolvedValue.HasValue)
            {
                value = resolvedValue.Value;
                return(true);
            }

            value = null;
            return(false);
        }
        private void WriteOption(Utf8JsonWriter writer, DummyAssembly source, PropertyInfo info, JsonSerializerOptions options)
        {
            writer.WritePropertyName(info.Name);

            writer.WriteStartObject();

            IOptional optional = (IOptional)info.GetValue(source);

            writer.WriteBoolean("HasValue", optional.HasValue);

            if (!optional.HasValue)
            {
                writer.WriteNull("Value");
                writer.WriteEndObject();
                return;
            }

            object value        = optional.Value;
            Type   propertyType = info.PropertyType.GetGenericArguments()[0];

            writer.WritePropertyName("Value");

            if (value == null)
            {
                JsonSerializer.Serialize(writer, value, propertyType);
            }
            else if (info.IsRepeatable())
            {
                WriteArray(writer, (IEnumerable)value, options);
            }
            else if (value is DummyAssembly)
            {
                Write(writer, (DummyAssembly)value, options);
            }
            else if (value is Assembly)
            {
                JsonSerializer.Serialize(writer, ((Assembly)value).FullName, typeof(string));
            }
            else if (value is Type)
            {
                JsonSerializer.Serialize(writer, ((Type)value).AssemblyQualifiedName, typeof(string));
            }
            else
            {
                JsonSerializer.Serialize(writer, value, value.GetType());
            }

            writer.WriteEndObject();
        }
Пример #30
0
        private void CancelAcquireWrite(long id)
        {
            lock (syncRoot) {
                IOptional <WaitingWrite> opt = waitingWrites.Cancel(id);
                if (opt.HasValue)
                {
                    opt.Value.k.PostResult(new AcquireWriteCancelled());

                    if (opt.Value.ctr.HasValue)
                    {
                        opt.Value.ctr.Value.PostDispose();
                    }
                }
            }
        }