Skip to content

Reuse the same generic EmptyMapClass instance across the project #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Async/Hql/Ast/HqlFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override bool AppliesTo(Dialect.Dialect dialect)

protected HQLQueryPlan CreateQueryPlan(string hql, bool scalar)
{
return new QueryExpressionPlan(new StringQueryExpression(hql), scalar, new CollectionHelper.EmptyMapClass<string, IFilter>(), Sfi);
return new QueryExpressionPlan(new StringQueryExpression(hql), scalar, CollectionHelper.EmptyDictionary<string, IFilter>(), Sfi);
}

protected HQLQueryPlan CreateQueryPlan(string hql)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/BulkManipulation/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NHibernate.Test.BulkManipulation
{
public class BaseFixture: TestCase
{
private readonly IDictionary<string, IFilter> emptyfilters = new CollectionHelper.EmptyMapClass<string, IFilter>();
private readonly IDictionary<string, IFilter> emptyfilters = CollectionHelper.EmptyDictionary<string, IFilter>();

#region Overrides of TestCase

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Hql/Ast/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NHibernate.Test.Hql.Ast
{
public class BaseFixture: TestCase
{
private readonly IDictionary<string, IFilter> emptyfilters = new CollectionHelper.EmptyMapClass<string, IFilter>();
private readonly IDictionary<string, IFilter> emptyfilters = CollectionHelper.EmptyDictionary<string, IFilter>();

#region Overrides of TestCase

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Hql/Ast/HqlFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override bool AppliesTo(Dialect.Dialect dialect)

protected HQLQueryPlan CreateQueryPlan(string hql, bool scalar)
{
return new QueryExpressionPlan(new StringQueryExpression(hql), scalar, new CollectionHelper.EmptyMapClass<string, IFilter>(), Sfi);
return new QueryExpressionPlan(new StringQueryExpression(hql), scalar, CollectionHelper.EmptyDictionary<string, IFilter>(), Sfi);
}

protected HQLQueryPlan CreateQueryPlan(string hql)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH1849/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void ExecutesCustomSqlFunctionContains()
{
string hql = @"from Customer c where contains(c.Name, :smth)";

HQLQueryPlan plan = new QueryExpressionPlan(new StringQueryExpression(hql), false, new CollectionHelper.EmptyMapClass<string, IFilter>(), Sfi);
HQLQueryPlan plan = new QueryExpressionPlan(new StringQueryExpression(hql), false, CollectionHelper.EmptyDictionary<string, IFilter>(), Sfi);

Assert.AreEqual(1, plan.ParameterMetadata.NamedParameterNames.Count);
Assert.AreEqual(1, plan.QuerySpaces.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void TheModuleOperationShouldAddParenthesisToAvoidWrongSentence()

public string GetSql(string query)
{
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), new CollectionHelper.EmptyMapClass<string, IFilter>(), Sfi);
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), CollectionHelper.EmptyDictionary<string, IFilter>(), Sfi);
qt.Compile(null, false);
return qt.SQLString;
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Cfg/MappingSchema/AbstractDecoratable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NHibernate.Cfg.MappingSchema
[Serializable]
public abstract class AbstractDecoratable : IDecoratable
{
private static readonly IDictionary<string, MetaAttribute> EmptyMetaData = new CollectionHelper.EmptyMapClass<string, MetaAttribute>();
private static readonly IDictionary<string, MetaAttribute> EmptyMetaData = CollectionHelper.EmptyDictionary<string, MetaAttribute>();

[NonSerialized]
[XmlIgnore]
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Cfg/XmlHbmBinding/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class Binder
protected static readonly INHibernateLogger log = NHibernateLogger.For(typeof (Binder));

protected static readonly IDictionary<string, MetaAttribute> EmptyMeta =
new CollectionHelper.EmptyMapClass<string, MetaAttribute>();
CollectionHelper.EmptyDictionary<string, MetaAttribute>();

protected readonly Mappings mappings;

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Cfg/XmlHbmBinding/ResultSetMappingBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private IDictionary<string, string[]> BindPropertyResults(string alias, HbmRetur
{
newPropertyResults[entry.Key] = entry.Value;
}
return newPropertyResults.Count == 0 ? (IDictionary<string, string[]>)new CollectionHelper.EmptyMapClass<string, string[]>() : newPropertyResults;
return newPropertyResults.Count == 0 ? (IDictionary<string, string[]>)CollectionHelper.EmptyDictionary<string, string[]>() : newPropertyResults;
}

private static List<string> GetResultColumns(HbmReturnProperty returnPropertySchema)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Engine/JoinSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public JoinSequence AddJoin(IAssociationType associationType, string alias, Join

public JoinFragment ToJoinFragment()
{
return ToJoinFragment(new CollectionHelper.EmptyMapClass<string, IFilter>(), true);
return ToJoinFragment(CollectionHelper.EmptyDictionary<string, IFilter>(), true);
}

public JoinFragment ToJoinFragment(IDictionary<string, IFilter> enabledFilters, bool includeExtraJoins)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private void PostProcessDML(IRestrictableStatement statement)
if (persister.DiscriminatorType != null)
{
new SyntheticAndFactory(this)
.AddDiscriminatorWhereFragment(statement, persister, new CollectionHelper.EmptyMapClass<string, IFilter>(), fromElement.TableAlias);
.AddDiscriminatorWhereFragment(statement, persister, CollectionHelper.EmptyDictionary<string, IFilter>(), fromElement.TableAlias);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/NHibernate/Impl/SessionFactoryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,15 +616,15 @@ public string GetIdentifierPropertyName(string className)
public IType[] GetReturnTypes(String queryString)
{
return
queryPlanCache.GetHQLQueryPlan(queryString.ToQueryExpression(), false, new CollectionHelper.EmptyMapClass<string, IFilter>()).
queryPlanCache.GetHQLQueryPlan(queryString.ToQueryExpression(), false, CollectionHelper.EmptyDictionary<string, IFilter>()).
ReturnMetadata.ReturnTypes;
}

/// <summary> Get the return aliases of a query</summary>
public string[] GetReturnAliases(string queryString)
{
return
queryPlanCache.GetHQLQueryPlan(queryString.ToQueryExpression(), false, new CollectionHelper.EmptyMapClass<string, IFilter>()).
queryPlanCache.GetHQLQueryPlan(queryString.ToQueryExpression(), false, CollectionHelper.EmptyDictionary<string, IFilter>()).
ReturnMetadata.ReturnAliases;
}

Expand Down Expand Up @@ -1163,7 +1163,7 @@ private IDictionary<string, HibernateException> CheckNamedQueries()
{
log.Debug("Checking named query: {0}", queryName);
//TODO: BUG! this currently fails for named queries for non-POJO entities
queryPlanCache.GetHQLQueryPlan(qd.QueryString.ToQueryExpression(), false, new CollectionHelper.EmptyMapClass<string, IFilter>());
queryPlanCache.GetHQLQueryPlan(qd.QueryString.ToQueryExpression(), false, CollectionHelper.EmptyDictionary<string, IFilter>());
}
catch (QueryException e)
{
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Impl/SqlQueryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected internal override IDictionary<string, LockMode> LockModes
get
{
//we never need to apply locks to the SQL
return new CollectionHelper.EmptyMapClass<string, LockMode>();
return CollectionHelper.EmptyDictionary<string, LockMode>();
}
}

Expand Down Expand Up @@ -236,7 +236,7 @@ public ISQLQuery AddJoin(string alias, string path, LockMode lockMode)
string ownerAlias = path.Substring(0, loc);
string role = path.Substring(loc + 1);
queryReturns.Add(
new NativeSQLQueryJoinReturn(alias, ownerAlias, role, new CollectionHelper.EmptyMapClass<string, string[]>(), lockMode));
new NativeSQLQueryJoinReturn(alias, ownerAlias, role, CollectionHelper.EmptyDictionary<string, string[]>(), lockMode));
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Impl/StatelessSessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public override IType GetFilterParameterType(string filterParameterName)

public override IDictionary<string, IFilter> EnabledFilters
{
get { return new CollectionHelper.EmptyMapClass<string, IFilter>(); }
get { return CollectionHelper.EmptyDictionary<string, IFilter>(); }
}

public override IQueryTranslator[] GetQueries(IQueryExpression query, bool scalar)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Loader/AbstractEntityJoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected virtual void InitAll(SqlString whereString, SqlString orderByString, L
IList<OuterJoinableAssociation> allAssociations = new List<OuterJoinableAssociation>(associations);
allAssociations.Add(
new OuterJoinableAssociation(persister.EntityType, null, null, alias, JoinType.LeftOuterJoin, null, Factory,
new CollectionHelper.EmptyMapClass<string, IFilter>()));
CollectionHelper.EmptyDictionary<string, IFilter>()));

InitPersisters(allAssociations, lockMode);
InitStatementString(whereString, orderByString, lockMode);
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Loader/Collection/OneToManyJoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public OneToManyJoinWalker(IQueryableCollection oneToManyPersister, int batchSiz
IList<OuterJoinableAssociation> allAssociations = new List<OuterJoinableAssociation>(associations);
allAssociations.Add(
new OuterJoinableAssociation(oneToManyPersister.CollectionType, null, null, alias, JoinType.LeftOuterJoin, null, Factory,
new CollectionHelper.EmptyMapClass<string, IFilter>()));
CollectionHelper.EmptyDictionary<string, IFilter>()));

InitPersisters(allAssociations, LockMode.None);
InitStatementString(elementPersister, alias, batchSize, subquery);
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Loader/DefaultEntityAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DefaultEntityAliases : IEntityAliases
private readonly IDictionary<string, string[]> userProvidedAliases;

public DefaultEntityAliases(ILoadable persister, string suffix)
: this(new CollectionHelper.EmptyMapClass<string, string[]>(), persister, suffix) {}
: this(CollectionHelper.EmptyDictionary<string, string[]>(), persister, suffix) {}

/// <summary>
/// Calculate and cache select-clause suffixes.
Expand Down Expand Up @@ -147,4 +147,4 @@ private static void Intern(string[] strings)
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate/Loader/Entity/CascadeEntityJoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class CascadeEntityJoinWalker : AbstractEntityJoinWalker

public CascadeEntityJoinWalker(IOuterJoinLoadable persister, CascadingAction action,
ISessionFactoryImplementor factory)
: base(persister, factory, new CollectionHelper.EmptyMapClass<string, IFilter>())
: base(persister, factory, CollectionHelper.EmptyDictionary<string, IFilter>())
{
cascadeAction = action;
SqlStringBuilder whereCondition = WhereString(Alias, persister.IdentifierColumnNames, 1)
//include the discriminator and class-level where, but not filters
.Add(persister.FilterFragment(Alias, new CollectionHelper.EmptyMapClass<string, IFilter>()));
.Add(persister.FilterFragment(Alias, CollectionHelper.EmptyDictionary<string, IFilter>()));

InitAll(whereCondition.ToSqlString(), SqlString.Empty, LockMode.Read);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Loader/Entity/CascadeEntityLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NHibernate.Loader.Entity
public class CascadeEntityLoader : AbstractEntityLoader
{
public CascadeEntityLoader(IOuterJoinLoadable persister, CascadingAction action, ISessionFactoryImplementor factory)
: base(persister, persister.IdentifierType, factory, new CollectionHelper.EmptyMapClass<string, IFilter>())
: base(persister, persister.IdentifierType, factory, CollectionHelper.EmptyDictionary<string, IFilter>())
{
JoinWalker walker = new CascadeEntityJoinWalker(persister, action, factory);
InitFromWalker(walker);
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Loader/Entity/EntityJoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public EntityJoinWalker(IOuterJoinLoadable persister, string[] uniqueKey, int ba

SqlStringBuilder whereCondition = WhereString(Alias, uniqueKey, batchSize)
//include the discriminator and class-level where, but not filters
.Add(persister.FilterFragment(Alias, new CollectionHelper.EmptyMapClass<string, IFilter>()));
.Add(persister.FilterFragment(Alias, CollectionHelper.EmptyDictionary<string, IFilter>()));

InitAll(whereCondition.ToSqlString(), SqlString.Empty, lockMode);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Loader/GeneratedCollectionAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public GeneratedCollectionAliases(IDictionary<string, string[]> userProvidedAlia
}

public GeneratedCollectionAliases(ICollectionPersister persister, string str)
: this(new CollectionHelper.EmptyMapClass<string, string[]>(), persister, str) {}
: this(CollectionHelper.EmptyDictionary<string, string[]>(), persister, str) {}

private string[] GetUserProvidedCompositeElementAliases(string[] defaultAliases)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public AbstractCollectionPersister(Mapping.Collection collection, ICacheConcurre
public void PostInstantiate()
{
initializer = queryLoaderName == null
? CreateCollectionInitializer(new CollectionHelper.EmptyMapClass<string, IFilter>())
? CreateCollectionInitializer(CollectionHelper.EmptyDictionary<string, IFilter>())
: new NamedQueryCollectionInitializer(queryLoaderName, this);
}

Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,7 @@ protected void CreateUniqueKeyLoaders()
//don't need filters for the static loaders
uniqueKeyLoaders[propertyNames[i]] =
CreateUniqueKeyLoader(propertyTypes[i], GetPropertyColumnNames(i),
new CollectionHelper.EmptyMapClass<string, IFilter>());
CollectionHelper.EmptyDictionary<string, IFilter>());
}
}
}
Expand Down Expand Up @@ -2197,7 +2197,7 @@ protected IUniqueEntityLoader CreateEntityLoader(LockMode lockMode, IDictionary<

protected IUniqueEntityLoader CreateEntityLoader(LockMode lockMode)
{
return CreateEntityLoader(lockMode, new CollectionHelper.EmptyMapClass<string, IFilter>());
return CreateEntityLoader(lockMode, CollectionHelper.EmptyDictionary<string, IFilter>());
}

protected bool Check(int rows, object id, int tableNumber, IExpectation expectation, DbCommand statement)
Expand Down
16 changes: 16 additions & 0 deletions src/NHibernate/Util/CollectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ public IEnumerator GetEnumerator()

public static readonly IEnumerable EmptyEnumerable = new EmptyEnumerableClass();
public static readonly IDictionary EmptyMap = new EmptyMapClass();

public static IDictionary<TKey, TValue> EmptyDictionary<TKey, TValue>()
{
return EmptyMapClass<TKey, TValue>.Instance;
}

public static readonly ICollection EmptyCollection = EmptyMap;
// Since v5
[Obsolete("It has no more usages in NHibernate and will be removed in a future version.")]
Expand Down Expand Up @@ -463,8 +469,18 @@ public object Current
[Serializable]
public class EmptyMapClass<TKey, TValue> : IDictionary<TKey, TValue>
{
#pragma warning disable 618 // Constructor is obsolete, to be switched to non-obsolete but private.
internal static readonly IDictionary<TKey, TValue> Instance = new EmptyMapClass<TKey, TValue>();
#pragma warning restore 618

private static readonly EmptyEnumerator<TKey, TValue> emptyEnumerator = new EmptyEnumerator<TKey, TValue>();

// Since v5.1. To be switched to private.
[Obsolete("Please use CollectionHelper.EmptyDictionary<TKey, TValue>() instead.")]
public EmptyMapClass()
{
}

#region IDictionary<TKey,TValue> Members

public bool ContainsKey(TKey key)
Expand Down