This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit b56aa78f64f1ad44d1aaaeffb32d0474d74f7d47 Author: Shad Storhaug <s...@shadstorhaug.com> AuthorDate: Fri Nov 1 13:25:14 2019 +0700 Lucene.Net.Support.IdentityComparer: Added singleton pattern to prevent creating unnecessary instances of IdentityComparer at runtime. --- src/Lucene.Net.Queries/Function/ValueSource.cs | 2 +- .../Store/MockDirectoryWrapper.cs | 2 +- src/Lucene.Net/Index/SegmentCoreReaders.cs | 2 +- src/Lucene.Net/Support/IdentityComparer.cs | 40 +++++++++++++++++++++- src/Lucene.Net/Support/IdentityHashMap.cs | 8 ++--- src/Lucene.Net/Support/IdentityHashSet.cs | 6 ++-- 6 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Lucene.Net.Queries/Function/ValueSource.cs b/src/Lucene.Net.Queries/Function/ValueSource.cs index 25616b4..60d10a8 100644 --- a/src/Lucene.Net.Queries/Function/ValueSource.cs +++ b/src/Lucene.Net.Queries/Function/ValueSource.cs @@ -65,7 +65,7 @@ namespace Lucene.Net.Queries.Function /// </summary> public static IDictionary NewContext(IndexSearcher searcher) { - var context = new Hashtable(new IdentityComparer()); + var context = new Hashtable(IdentityComparer.Default); context["searcher"] = searcher; return context; } diff --git a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs index 99cfdfa..bd62788 100644 --- a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs +++ b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs @@ -124,7 +124,7 @@ namespace Lucene.Net.Store // use this for tracking files for crash. // additionally: provides debugging information in case you leave one open - private readonly ConcurrentDictionary<IDisposable, Exception> openFileHandles = new ConcurrentDictionary<IDisposable, Exception>(new IdentityComparer<IDisposable>()); + private readonly ConcurrentDictionary<IDisposable, Exception> openFileHandles = new ConcurrentDictionary<IDisposable, Exception>(IdentityComparer<IDisposable>.Default); // NOTE: we cannot initialize the Map here due to the // order in which our constructor actually does this diff --git a/src/Lucene.Net/Index/SegmentCoreReaders.cs b/src/Lucene.Net/Index/SegmentCoreReaders.cs index d6fbee2..718345d 100644 --- a/src/Lucene.Net/Index/SegmentCoreReaders.cs +++ b/src/Lucene.Net/Index/SegmentCoreReaders.cs @@ -112,7 +112,7 @@ namespace Lucene.Net.Index } } - private readonly ISet<ICoreDisposedListener> coreClosedListeners = new ConcurrentHashSet<ICoreDisposedListener>(new IdentityComparer<ICoreDisposedListener>()); + private readonly ISet<ICoreDisposedListener> coreClosedListeners = new ConcurrentHashSet<ICoreDisposedListener>(IdentityComparer<ICoreDisposedListener>.Default); internal SegmentCoreReaders(SegmentReader owner, Directory dir, SegmentCommitInfo si, IOContext context, int termsIndexDivisor) { diff --git a/src/Lucene.Net/Support/IdentityComparer.cs b/src/Lucene.Net/Support/IdentityComparer.cs index e66c6eb..da1ed5c 100644 --- a/src/Lucene.Net/Support/IdentityComparer.cs +++ b/src/Lucene.Net/Support/IdentityComparer.cs @@ -21,8 +21,26 @@ namespace Lucene.Net.Support * limitations under the License. */ - public class IdentityComparer<T> : IEqualityComparer<T> + /// <summary> + /// Represents a comparison operation that tests equality by reference + /// instead of equality by value. Basically, the comparison is done by + /// checking if <see cref="System.Object.ReferenceEquals(object, object)"/> + /// returns true rather than by calling the "Equals" function. + /// </summary> + /// <typeparam name="T">The type of object to test for reference equality. Must be a class, not a struct.</typeparam> + public class IdentityComparer<T> : IEqualityComparer<T> where T : class { + /// <summary> + /// Gets an <see cref="IdentityComparer{T}"/> object that tests equality by reference + /// instead of equality by value. Basically, the comparison is done by + /// checking if <see cref="System.Object.ReferenceEquals(object, object)"/> + /// returns true rather than by calling the "Equals" function. + /// </summary> + public static IdentityComparer<T> Default => new IdentityComparer<T>(); + + internal IdentityComparer() + { } + public bool Equals(T x, T y) { return ReferenceEquals(x, y); @@ -34,8 +52,28 @@ namespace Lucene.Net.Support } } + /// <summary> + /// Represents a comparison operation that tests equality by reference + /// instead of equality by value. Basically, the comparison is done by + /// checking if <see cref="System.Object.ReferenceEquals(object, object)"/> + /// returns true rather than by calling the "Equals" function. + /// <para/> + /// Note that the assumption is that the object is passed will be a reference type, + /// although it is not strictly enforced. + /// </summary> public class IdentityComparer : IEqualityComparer { + /// <summary> + /// Gets an <see cref="IdentityComparer{T}"/> object that tests equality by reference + /// instead of equality by value. Basically, the comparison is done by + /// checking if <see cref="System.Object.ReferenceEquals(object, object)"/> + /// returns true rather than by calling the "Equals" function. + /// </summary> + public static IdentityComparer Default => new IdentityComparer(); + + internal IdentityComparer() + { } + public new bool Equals(object x, object y) { return ReferenceEquals(x, y); diff --git a/src/Lucene.Net/Support/IdentityHashMap.cs b/src/Lucene.Net/Support/IdentityHashMap.cs index e7a381d..174b921 100644 --- a/src/Lucene.Net/Support/IdentityHashMap.cs +++ b/src/Lucene.Net/Support/IdentityHashMap.cs @@ -19,20 +19,20 @@ namespace Lucene.Net.Support * limitations under the License. */ - public class IdentityHashMap<TKey, TValue> : HashMap<TKey, TValue> + public class IdentityHashMap<TKey, TValue> : HashMap<TKey, TValue> where TKey : class { public IdentityHashMap() - : base(new IdentityComparer<TKey>()) + : base(IdentityComparer<TKey>.Default) { } public IdentityHashMap(int initialCapacity) - : base(initialCapacity, new IdentityComparer<TKey>()) + : base(initialCapacity, IdentityComparer<TKey>.Default) { } public IdentityHashMap(IDictionary<TKey, TValue> wrappedDictionary) - : base(wrappedDictionary, new IdentityComparer<TKey>()) + : base(wrappedDictionary, IdentityComparer<TKey>.Default) { } } diff --git a/src/Lucene.Net/Support/IdentityHashSet.cs b/src/Lucene.Net/Support/IdentityHashSet.cs index 06709c0..5695b65 100644 --- a/src/Lucene.Net/Support/IdentityHashSet.cs +++ b/src/Lucene.Net/Support/IdentityHashSet.cs @@ -19,15 +19,15 @@ namespace Lucene.Net.Support * limitations under the License. */ - public class IdentityHashSet<T> : HashSet<T> + public class IdentityHashSet<T> : HashSet<T> where T : class { public IdentityHashSet() - : base(new IdentityComparer<T>()) + : base(IdentityComparer<T>.Default) { } public IdentityHashSet(IEnumerable<T> collection) - : base(collection, new IdentityComparer<T>()) + : base(collection, IdentityComparer<T>.Default) { } }