/* * * (c) Copyright Ascensio System Limited 2010-2021 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ namespace ASC.ActiveDirectory.Base.Expressions { /// /// Criteria /// public class Criteria : ICloneable { private readonly CriteriaType _type; private readonly List _expressions = new List(); private readonly List _nestedCriteras = new List(); /// /// Constructor /// /// Type of critera /// Expressions public Criteria(CriteriaType type, params Expression[] expressions) { _expressions.AddRange(expressions); _type = type; } /// /// Add nested expressions as And criteria /// /// Expressions /// Self public Criteria And(params Expression[] expressions) { _nestedCriteras.Add(All(expressions)); return this; } /// /// Add nested expressions as Or criteria /// /// Expressions /// Self public Criteria Or(params Expression[] expressions) { _nestedCriteras.Add(Any(expressions)); return this; } /// /// Add nested Criteria /// /// /// себя public Criteria Add(Criteria nested) { _nestedCriteras.Add(nested); return this; } /// /// Criteria as a string /// /// Criteria string public override string ToString() { var criteria = "({0}{1}{2})"; var expressions = _expressions.Aggregate(string.Empty, (current, expr) => current + expr.ToString()); var criterias = _nestedCriteras.Aggregate(string.Empty, (current, crit) => current + crit.ToString()); return string.Format(criteria, _type == CriteriaType.And ? "&" : "|", expressions, criterias); } /// /// Group of Expression union as And /// /// Expressions /// new Criteria public static Criteria All(params Expression[] expressions) { return new Criteria(CriteriaType.And, expressions); } /// /// Group of Expression union as Or /// /// Expressions /// new Criteria public static Criteria Any(params Expression[] expressions) { return new Criteria(CriteriaType.Or, expressions); } #region ICloneable Members /// /// ICloneable implemetation /// /// Clone object public object Clone() { var cr = new Criteria(_type); foreach (var ex in _expressions) { cr._expressions.Add(ex.Clone() as Expression); } foreach (var nc in _nestedCriteras) { cr._nestedCriteras.Add(nc.Clone() as Criteria); } return cr; } #endregion } }