Skip to content

Commit

Permalink
[GH-291] Fixing SonarQube issues classified as bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
amaggiulli committed Nov 14, 2024
1 parent 83edcb5 commit 41a5180
Show file tree
Hide file tree
Showing 18 changed files with 34 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/QLNet/Handle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace QLNet
pointer is relinked to another observable, the change will be propagated to all the copies.
<tt>registerAsObserver</tt> is not needed since C# does automatic garbage collection */

public class Handle<T> where T : IObservable
public class Handle<T> where T : class, IObservable
{
protected Link link_;

Expand All @@ -41,7 +41,7 @@ public Handle(T h, bool registerAsObserver)
public T currentLink() { return link; }

// this one is instead of c++ -> and () operators overload
public static implicit operator T(Handle<T> ImpliedObject) { return ImpliedObject.link; }
public static implicit operator T(Handle<T> impliedObject) { return impliedObject.link; }

public T link
{
Expand Down Expand Up @@ -153,7 +153,7 @@ protected void notifyObservers()
/*! An instance of this class can be relinked so that it points to another observable. The change will be propagated to all
handles that were created as copies of such instance. */

public class RelinkableHandle<T> : Handle<T> where T : IObservable
public class RelinkableHandle<T> : Handle<T> where T : class, IObservable
{
public RelinkableHandle() : base(default(T), true) { }

Expand Down
8 changes: 4 additions & 4 deletions src/QLNet/Math/Interpolations/MixedInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ interpolation over the second part. */
}

public class MixedInterpolationImpl<Interpolator1, Interpolator2> : Interpolation.templateImpl
where Interpolator1 : IInterpolationFactory, new ()
where Interpolator2 : IInterpolationFactory, new ()
where Interpolator1 : class, IInterpolationFactory, new ()
where Interpolator2 : class, IInterpolationFactory, new ()
{
public MixedInterpolationImpl(List<double> xBegin, int xEnd,
List<double> yBegin, int n,
Behavior behavior = Behavior.ShareRanges,
Interpolator1 factory1 = default(Interpolator1),
Interpolator2 factory2 = default(Interpolator2))
: base(xBegin, xEnd, yBegin,
Math.Max(factory1 == null ? (factory1 = new Interpolator1()).requiredPoints : factory1.requiredPoints,
factory2 == null ? (factory2 = new Interpolator2()).requiredPoints : factory2.requiredPoints))
Math.Max(factory1?.requiredPoints ?? (factory1 = new Interpolator1()).requiredPoints,
factory2?.requiredPoints ?? (factory2 = new Interpolator2()).requiredPoints))

{
n_ = n;
Expand Down
6 changes: 3 additions & 3 deletions src/QLNet/Math/Interpolations/SABRInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Vector inverse(Vector y, List<bool> b, List < double? > c, double d)
Vector x = new Vector(4);
x[0] = y[0] < 25.0 + eps1() ? Math.Sqrt(Math.Max(eps1(), y[0]) - eps1())
: (y[0] - eps1() + 25.0) / 10.0;
x[1] = y[1] == 0.0 ? 0.0 : Math.Sqrt(-Math.Log(y[1]));
x[1] = y[1].IsEqual(0.0) ? 0.0 : Math.Sqrt(-Math.Log(y[1]));
x[2] = y[2] < 25.0 + eps1() ? Math.Sqrt(y[2] - eps1())
: (y[2] - eps1() + 25.0) / 10.0;
x[3] = Math.Asin(y[3] / eps2());
Expand All @@ -123,7 +123,7 @@ public Vector direct(Vector x, List<bool> b, List < double? > c, double d)
y[0] = Math.Abs(x[0]) < 5.0
? x[0] * x[0] + eps1()
: (10.0 * Math.Abs(x[0]) - 25.0) + eps1();
y[1] = Math.Abs(x[1]) < Math.Sqrt(-Math.Log(eps1())) && x[1] != 0.0
y[1] = Math.Abs(x[1]) < Math.Sqrt(-Math.Log(eps1())) && x[1].IsNotEqual(0.0)
? Math.Exp(-(x[1] * x[1]))
: (volatilityType_ == VolatilityType.ShiftedLognormal ? eps1() : 0.0);
y[2] = Math.Abs(x[2]) < 5.0
Expand All @@ -142,7 +142,7 @@ public IWrapper instance(double t, double forward, List < double? > param, List
}
public double weight(double strike, double forward, double stdDev, List < double? > addParams)
{
if (Convert.ToDouble(addParams[1]) == 0.0)
if (Convert.ToDouble(addParams[1]).IsEqual(0.0))
return Utils.blackFormulaStdDevDerivative(strike, forward, stdDev, 1.0, addParams[0].Value);
else
return Utils.bachelierBlackFormulaStdDevDerivative(strike, forward, stdDev, 1.0);
Expand Down
2 changes: 1 addition & 1 deletion src/QLNet/Math/randomnumbers/RNGTraits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface IRSG
}

// random number traits
public class GenericPseudoRandom<URNG, IC> : IRSG where URNG : IRNGTraits, new () where IC : IValue, new ()
public class GenericPseudoRandom<URNG, IC> : IRSG where URNG : IRNGTraits, new () where IC : class, IValue, new ()
{
// data
private static IC icInstance_ = FastActivator<IC>.Create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void rollbackImpl(ref object o, double from, double to, int steps, IStep
double dt = (from - to) / steps, t = from;
evolver_.setStep(dt);

if (!stoppingTimes_.empty() && stoppingTimes_.Last() == from)
if (!stoppingTimes_.empty() && stoppingTimes_.Last().IsEqual(from))
{
if (condition != null)
condition.applyTo(o, from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public Vector solve_splitting(Vector r, double a, double b = 1.0)
{
List<int> coordinates = iter.coordinates();
Utils.QL_REQUIRE(coordinates[direction_] != 0
|| lower_[iter.index()] == 0, () => "removing non zero entry!");
|| lower_[iter.index()].IsEqual(0), () => "removing non zero entry!");
Utils.QL_REQUIRE(coordinates[direction_] != layout.dim()[direction_] - 1
|| upper_[iter.index()] == 0, () => "removing non zero entry!");
|| upper_[iter.index()].IsEqual(0), () => "removing non zero entry!");
}

Vector retVal = new Vector(r.size()), tmp = new Vector(r.size());
Expand All @@ -142,7 +142,7 @@ public Vector solve_splitting(Vector r, double a, double b = 1.0)
// changed to fit for the triple band operator.
int rim1 = reverseIndex_[0];
double bet = 1.0 / (a * diag_[rim1] + b);
Utils.QL_REQUIRE(bet != 0.0, () => "division by zero");
Utils.QL_REQUIRE(bet.IsNotEqual(0.0), () => "division by zero");
retVal[reverseIndex_[0]] = r[rim1] * bet;

for (int j = 1; j <= layout.size() - 1; j++)
Expand All @@ -151,7 +151,7 @@ public Vector solve_splitting(Vector r, double a, double b = 1.0)
tmp[j] = a * upper_[rim1] * bet;

bet = b + a * (diag_[ri] - tmp[j] * lower_[ri]);
Utils.QL_REQUIRE(bet != 0.0, () => "division by zero"); //QL_ENSURE
Utils.QL_REQUIRE(bet.IsNotEqual(0.0), () => "division by zero"); //QL_ENSURE
bet = 1.0 / bet;

retVal[ri] = (r[ri] - a * lower_[ri] * retVal[rim1]) * bet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public IMixedScheme factory(object L, object bcs, object[] additionalInputs = nu
public void step(ref object a, double t, double theta = 1.0)
{
Utils.QL_REQUIRE(t - dt_ > -1e-8, () => "a step towards negative time given");
if (theta_ != 1.0)
if (theta_.IsNotEqual(1.0))
explicit_.step(ref a, t, 1.0 - theta_);

if (theta_ != 0.0)
if (theta_.IsNotEqual(0.0))
implicit_.step(ref a, t, theta_);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FdmSnapshotCondition(double t)

public void applyTo(object o, double t)
{
if (t == t_)
if (t.IsEqual(t_))
values_ = (Vector)o;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ protected double avgInnerValueCalc(FdmLinearOpIterator iter, double t)
double retVal;
try
{
double acc
= ((f(a) != 0.0 || f(b) != 0.0) ? (f(a) + f(b)) * 5e-5 : 1e-4);
double acc = ((f(a).IsNotEqual(0.0) || f(b).IsNotEqual(0.0)) ? (f(a) + f(b)) * 5e-5 : 1e-4);
retVal = new SimpsonIntegral(acc, 8).value(f, a, b) / (b - a);
}
catch
Expand Down
2 changes: 1 addition & 1 deletion src/QLNet/Pricingengines/Bond/TreeCallableBondEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void calculateWithSpread(double s)
lattice = model_.link.tree(timeGrid);
}

if (s != 0.0)
if (s.IsNotEqual(0.0))
{
var sr = lattice as OneFactorModel.ShortRateTree;
Utils.QL_REQUIRE(sr != null, () => "Spread is not supported for trees other than OneFactorModel");
Expand Down
2 changes: 1 addition & 1 deletion src/QLNet/Pricingengines/GenericModelEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GenericModelEngine<ModelType, ArgumentsType, ResultsType>
: GenericEngine<ArgumentsType, ResultsType>
where ArgumentsType : IPricingEngineArguments, new ()
where ResultsType : IPricingEngineResults, new ()
where ModelType : IObservable
where ModelType : class,IObservable
{
public GenericModelEngine() { }
public GenericModelEngine(Handle<ModelType> model)
Expand Down
2 changes: 1 addition & 1 deletion src/QLNet/Pricingengines/vanilla/FDMultiPeriodEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public override void calculate(IPricingEngineResults r)
if (firstDateIsZero)
executeIntermediateStep(0);

results.value = prices_.valueAtCenter();
results!.value = prices_.valueAtCenter();
results.delta = prices_.firstDerivativeAtCenter();
results.gamma = prices_.secondDerivativeAtCenter();
results.additionalResults["priceCurve"] = prices_;
Expand Down
1 change: 1 addition & 0 deletions src/QLNet/Termstructures/Bootstraphelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum Choice
* class to ensure consistancy between the algorithms used during bootstrapping
and later instrument pricing. This is not yet fully enforced in the available rate helpers. */
public class BootstrapHelper<TS> : IObservable, IObserver
where TS : class
{
protected Handle<Quote> quote_;
protected TS termStructure_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ under the terms of the QLNet license. You should have received a
namespace QLNet
{
public class InterpolatedHazardRateCurve<Interpolator> : HazardRateStructure, InterpolatedCurve
where Interpolator : IInterpolationFactory, new ()
where Interpolator : class, IInterpolationFactory, new ()
{
public InterpolatedHazardRateCurve(List<Date> dates, List<double> hazardRates, DayCounter dayCounter, Calendar cal = null,
List<Handle<Quote>> jumps = null, List<Date> jumpDates = null, Interpolator interpolator = default(Interpolator))
Expand Down
3 changes: 2 additions & 1 deletion src/QLNet/Termstructures/Curve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ under the terms of the QLNet license. You should have received a
namespace QLNet
{
public interface Curve<T> : ITraits<T>, InterpolatedCurve
where T : class
{
#region ITraits

Expand All @@ -44,4 +45,4 @@ public interface Curve<T> : ITraits<T>, InterpolatedCurve
double initialValue();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ public PiecewiseYoYInflationCurve()

public class PiecewiseYoYInflationCurve<Interpolator, Bootstrap, Traits> : PiecewiseYoYInflationCurve
where Traits : ITraits<YoYInflationTermStructure>, new ()
where Interpolator : IInterpolationFactory, new ()
where Bootstrap : IBootStrap<PiecewiseYoYInflationCurve>, new ()
where Interpolator : class, IInterpolationFactory, new ()
where Bootstrap : class, IBootStrap<PiecewiseYoYInflationCurve>, new ()
{

public PiecewiseYoYInflationCurve(Date referenceDate,
Expand Down Expand Up @@ -288,7 +288,7 @@ public override Dictionary<Date, double> nodes()

// Allows for optional 3rd generic parameter defaulted to IterativeBootstrap
public class PiecewiseYoYInflationCurve<Interpolator> : PiecewiseYoYInflationCurve<Interpolator, IterativeBootstrapForYoYInflation, YoYInflationTraits>
where Interpolator : IInterpolationFactory, new ()
where Interpolator : class, IInterpolationFactory, new ()
{
public PiecewiseYoYInflationCurve(Date referenceDate,
Calendar calendar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public PiecewiseZeroInflationCurve()

public class PiecewiseZeroInflationCurve<Interpolator, Bootstrap, Traits> : PiecewiseZeroInflationCurve
where Traits : ITraits<ZeroInflationTermStructure>, new ()
where Interpolator : IInterpolationFactory, new ()
where Bootstrap : IBootStrap<PiecewiseZeroInflationCurve>, new ()
where Interpolator : class, IInterpolationFactory, new ()
where Bootstrap : class, IBootStrap<PiecewiseZeroInflationCurve>, new ()
{

public PiecewiseZeroInflationCurve(Date referenceDate,
Expand Down Expand Up @@ -296,7 +296,7 @@ public override Dictionary<Date, double> nodes()

// Allows for optional 3rd generic parameter defaulted to IterativeBootstrap
public class PiecewiseZeroInflationCurve<Interpolator> : PiecewiseZeroInflationCurve<Interpolator, IterativeBootstrapForInflation, ZeroInflationTraits>
where Interpolator : IInterpolationFactory, new ()
where Interpolator : class, IInterpolationFactory, new ()
{
public PiecewiseZeroInflationCurve(Date referenceDate,
Calendar calendar,
Expand Down
2 changes: 1 addition & 1 deletion src/QLNet/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static MethodInfo GetMethodInfo(Object t, String function, Type[] types

// this is a redefined collection class to emulate array-type behaviour at initialisation
// if T is a class then the list is initilized with default constructors instead of null
public class InitializedList<T> : List<T> where T : new ()
public class InitializedList<T> : List<T> where T : new ()
{
public InitializedList() : base() { }
public InitializedList(int size) : base(size)
Expand Down

0 comments on commit 41a5180

Please sign in to comment.