Skip to content

Commit

Permalink
Mixed nullable fixes (beardgame#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
artog committed Oct 24, 2022
1 parent 07d7912 commit 8baa6be
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Bearded.Utilities/Algorithms/CoffmanGraham.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static ImmutableList<ImmutableHashSet<T>> createLayers<T>(

public static ISolver SolverForReducedGraphs(int maxLayerSize) => new ReducedGraphSolver(maxLayerSize);

private struct DecreasingNumberSequence : IComparable<DecreasingNumberSequence>, IComparable
private readonly struct DecreasingNumberSequence : IComparable<DecreasingNumberSequence>, IComparable
{
private readonly ImmutableList<int> numbers;

Expand All @@ -186,7 +186,7 @@ private DecreasingNumberSequence(ImmutableList<int> numbers)
this.numbers = numbers;
}

public int CompareTo(object obj) => CompareTo((DecreasingNumberSequence) obj);
public int CompareTo(object? obj) => CompareTo((DecreasingNumberSequence) (obj ?? throw new ArgumentNullException()));

public int CompareTo(DecreasingNumberSequence other)
{
Expand Down
2 changes: 1 addition & 1 deletion Bearded.Utilities/Collections/PrefixTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private struct Node
{
private readonly Dictionary<char, Node>? values;

public string Key { get; }
public string? Key { get; }

#region creating

Expand Down
4 changes: 3 additions & 1 deletion Bearded.Utilities/Collections/PriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Bearded.Utilities.Collections;
/// </summary>
/// <typeparam name="TPriority"></typeparam>
/// <typeparam name="TValue"></typeparam>
public sealed class PriorityQueue<TPriority, TValue> : StaticPriorityQueue<TPriority, TValue> where TPriority : IComparable<TPriority>
public sealed class PriorityQueue<TPriority, TValue> : StaticPriorityQueue<TPriority, TValue>
where TPriority : IComparable<TPriority>
where TValue : notnull
{
private readonly Dictionary<TValue, int> valueDict = new Dictionary<TValue, int>();

Expand Down
7 changes: 3 additions & 4 deletions Bearded.Utilities/Core/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static Platform detectPlatform()
#endregion

#region User settings directory
private static string userSettingsDirectory;
private static string? userSettingsDirectory;

private static string buildUserSettingsDirectory()
{
Expand All @@ -67,8 +67,7 @@ private static string buildUserSettingsDirectory()
return linuxConfigDir;

var linuxHomeDir = System.Environment.GetEnvironmentVariable("HOME");
// ReSharper disable once AssignNullToNotNullAttribute
return string.IsNullOrEmpty(linuxConfigDir) ? "." : Path.Combine(linuxHomeDir, ".config");
return string.IsNullOrEmpty(linuxConfigDir) ? "." : Path.Combine(linuxHomeDir!, ".config");
default:
throw new InvalidOperationException("Encountered unknown platform.");
}
Expand All @@ -80,7 +79,7 @@ private static string buildUserSettingsDirectory()
/// For OSX: ~/Library/Application Support
/// For Linux: ~/.config
/// </summary>
public static string UserSettingsDirectory => userSettingsDirectory ?? (userSettingsDirectory = buildUserSettingsDirectory());
public static string UserSettingsDirectory => userSettingsDirectory ??= buildUserSettingsDirectory();

/// <summary>
/// Gets the default user setting directory for a given game name.
Expand Down
2 changes: 1 addition & 1 deletion Bearded.Utilities/Core/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Bearded.Utilities;

public abstract class Singleton<TSelf> where TSelf : Singleton<TSelf>
{
public static TSelf Instance { get; private set; }
public static TSelf? Instance { get; private set; }

protected Singleton()
{
Expand Down
4 changes: 2 additions & 2 deletions Bearded.Utilities/Core/StaticRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public static class StaticRandom
{
#region Threadsafe random
[ThreadStatic]
private static Random random;
private static Random? random;

/// <summary>
/// The thread safe instance of Random used by StaticRandom
/// </summary>
// is internal for use as default random in Linq.Extensions
internal static Random Random => random ?? (random = new Random());
internal static Random Random => random ??= new Random();

/// <summary>
/// Overrides the Random object for the calling thread by one with the given seed.
Expand Down
2 changes: 1 addition & 1 deletion Bearded.Utilities/IO/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private static void ensureListCapacity(List<Entry> list, int neededCapacity)
/// <summary>
/// If RaiseEvents is true, this event is raised every time an event is added to the log.
/// </summary>
public event LogEvent Logged;
public event LogEvent? Logged;

#region settings

Expand Down
16 changes: 13 additions & 3 deletions Bearded.Utilities/Linq/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Bearded.Utilities.Linq;
Expand Down Expand Up @@ -152,8 +153,13 @@ public static int AddSorted<T>(this List<T> list, T item, IComparer<T> comparer)
/// <param name="result">The resulting transformed value.</param>
/// <param name="transform">The function transforming the value into the result.</param>
/// <returns>True if the value was found, false otherwise.</returns>
public static bool TryGetTransformedValue<TKey, TValue, TNewValue>(this Dictionary<TKey, TValue> dictionary, TKey key, out TNewValue result,
Func<TValue, TNewValue> transform)
public static bool TryGetTransformedValue<TKey, TValue, TNewValue>(
this Dictionary<TKey, TValue> dictionary,
TKey key,
[MaybeNullWhen(false)] out TNewValue result,
Func<TValue, TNewValue> transform
)
where TKey : notnull
{
if (dictionary.TryGetValue(key, out var value))
{
Expand All @@ -171,7 +177,9 @@ public static bool TryGetTransformedValue<TKey, TValue, TNewValue>(this Dictiona
/// <param name="dictionary">The dictionary.</param>
/// <param name="other">KeyValuePairs to add.</param>
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> dictionary,
IEnumerable<KeyValuePair<TKey, TValue>> other)
IEnumerable<KeyValuePair<TKey, TValue>> other
)
where TKey : notnull
{
foreach (var pair in other)
{
Expand All @@ -181,13 +189,15 @@ public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> diction

public static TValue? ValueOrNull<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key)
where TValue : class
where TKey : notnull
{
dict.TryGetValue(key, out var value);
return value;
}

public static TValue ValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key)
where TValue : struct
where TKey : notnull
{
dict.TryGetValue(key, out var value);
return value;
Expand Down

0 comments on commit 8baa6be

Please sign in to comment.