예제 #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            int id;

            return(int.TryParse(Convert.ToString(reader.Value), out id)
                                ? ConditionID.Parse(id)
                                : null);
        }
예제 #2
0
 private static void RemoveChild(IEnumerable <Condition> conditions, ConditionID id)
 {
     foreach (var parent in conditions.OfType <IParentCondition>())
     {
         parent.RemoveChild(id);
         RemoveChild(parent.Children, id);
     }
 }
예제 #3
0
        public void Remove(ConditionID conditionID)
        {
            if (HasCondition(conditionID) == false)
            {
                throw new ConditionNotFoundException(conditionID);
            }

            RemoveChild(conditionID);
        }
예제 #4
0
        private void RemoveChild(ConditionID childID)
        {
            var removed = _conditions.RemoveAll(c => c.ID == childID);

            if (removed > 0)
            {
                return;
            }

            RemoveChild(_conditions, childID);
        }
예제 #5
0
        public Condition CreateCondition(ConditionID conditionID, Dictionary <string, object> conditionProperties)
        {
            conditionProperties = new Dictionary <string, object>(conditionProperties, StringComparer.OrdinalIgnoreCase);

            if (conditionProperties.TryGetValue(TypeKey, out var type) == false)
            {
                throw new ConditionException("The Type was not specified");
            }

            if (Conditions.Value.TryGetValue(Convert.ToString(type), out var create) == false)
            {
                throw new ConditionException($"Unknown condition type '{type}'");
            }

            var condition = create(conditionProperties);

            condition.ID = conditionID;

            return(condition);
        }
예제 #6
0
        private static Condition FindCondition(IEnumerable <Condition> conditions, ConditionID id)
        {
            foreach (var condition in conditions)
            {
                if (condition.ID == id)
                {
                    return(condition);
                }

                if (condition is IParentCondition multi)
                {
                    var found = FindCondition(multi.Children, id);

                    if (found != null)
                    {
                        return(found);
                    }
                }
            }

            return(null);
        }
예제 #7
0
        public void Add(Condition child, ConditionID parentConditionID = null)
        {
            if (parentConditionID == null)
            {
                _conditions.Add(child);
                return;
            }

            var condition = FindCondition(parentConditionID);

            if (condition == null)
            {
                throw new ConditionNotFoundException(parentConditionID);
            }

            var parent = condition as IParentCondition;

            if (parent is null)
            {
                throw new ConditionException($"{condition.GetType().Name} does not support children.");
            }

            parent.AddChild(child);
        }
예제 #8
0
 protected override object Parse(string value) => ConditionID.Parse(Convert.ToInt32(value));
 public ConditionNotFoundException(ConditionID conditionID)
     : base($"Unable to find a condition with the ID '{conditionID}'")
 {
 }
예제 #10
0
 public Condition FindCondition(ConditionID id) => FindCondition(_conditions, id);
예제 #11
0
 public bool HasCondition(ConditionID id) => FindCondition(_conditions, id) != null;
예제 #12
0
        public bool CanAdd(Condition child, ConditionID parentConditionID)
        {
            var condition = FindCondition(parentConditionID);

            return(condition is IParentCondition parent && parent.CanAdd(child));
        }