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); }
private static void RemoveChild(IEnumerable <Condition> conditions, ConditionID id) { foreach (var parent in conditions.OfType <IParentCondition>()) { parent.RemoveChild(id); RemoveChild(parent.Children, id); } }
public void Remove(ConditionID conditionID) { if (HasCondition(conditionID) == false) { throw new ConditionNotFoundException(conditionID); } RemoveChild(conditionID); }
private void RemoveChild(ConditionID childID) { var removed = _conditions.RemoveAll(c => c.ID == childID); if (removed > 0) { return; } RemoveChild(_conditions, childID); }
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); }
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); }
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); }
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}'") { }
public Condition FindCondition(ConditionID id) => FindCondition(_conditions, id);
public bool HasCondition(ConditionID id) => FindCondition(_conditions, id) != null;
public bool CanAdd(Condition child, ConditionID parentConditionID) { var condition = FindCondition(parentConditionID); return(condition is IParentCondition parent && parent.CanAdd(child)); }