Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Common / Utils / Boolean / Literal.cs
1 //---------------------------------------------------------------------
2 // <copyright file="Literal.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.Collections.Generic;
12 using System.Text;
13 using System.Globalization;
14 using System.Collections.ObjectModel;
15 using System.Diagnostics;
16
17 namespace System.Data.Common.Utils.Boolean
18 {
19     /// <summary>
20     /// Represents a literal in a normal form expression of the form:
21     /// 
22     ///         Term
23     /// 
24     /// or
25     /// 
26     ///         !Term
27     /// </summary>
28     /// <typeparam name="T_Identifier"></typeparam>
29     internal sealed class Literal<T_Identifier> : NormalFormNode<T_Identifier>,
30         IEquatable<Literal<T_Identifier>>
31     {
32         private readonly TermExpr<T_Identifier> _term;
33         private readonly bool _isTermPositive;
34
35         /// <summary>
36         /// Initialize a new literal.
37         /// </summary>
38         /// <param name="term">Term</param>
39         /// <param name="isTermPositive">Sign of term</param>
40         internal Literal(TermExpr<T_Identifier> term, bool isTermPositive)
41             : base(isTermPositive ? (BoolExpr<T_Identifier>)term : (BoolExpr<T_Identifier>)new NotExpr<T_Identifier>(term))
42         {
43             Debug.Assert(null != term);
44             _term = term;
45             _isTermPositive = isTermPositive;
46         }
47
48         /// <summary>
49         /// Gets literal term.
50         /// </summary>
51         internal TermExpr<T_Identifier> Term
52         {
53             get { return _term; }
54         }
55
56         /// <summary>
57         /// Gets sign of term.
58         /// </summary>
59         internal bool IsTermPositive
60         {
61             get { return _isTermPositive; }
62         }
63
64         /// <summary>
65         /// Creates a negated version of this literal.
66         /// </summary>
67         /// <returns>!this</returns>
68         internal Literal<T_Identifier> MakeNegated()
69         {
70             return IdentifierService<T_Identifier>.Instance.NegateLiteral(this);
71         }
72
73         public override string ToString()
74         {
75             return StringUtil.FormatInvariant("{0}{1}",
76                 _isTermPositive ? String.Empty : "!",
77                 _term);
78         }
79
80         public override bool Equals(object obj)
81         {
82             Debug.Fail("use typed Equals");
83             return Equals(obj as Literal<T_Identifier>);
84         }
85
86         public bool Equals(Literal<T_Identifier> other)
87         {
88             return null != other &&
89                 other._isTermPositive == _isTermPositive &&
90                 other._term.Equals(_term);
91         }
92
93         public override int GetHashCode()
94         {
95             return _term.GetHashCode();
96         }
97     }
98 }