コード例 #1
0
ファイル: CommodityBase.cs プロジェクト: taiab/nledger
        public override bool Equals(object obj)
        {
            MemoizedPriceEntry entry = obj as MemoizedPriceEntry;

            if (obj == null)
            {
                return(false);
            }

            return(Start == entry.Start && End == entry.End && Commodity == entry.Commodity);
        }
コード例 #2
0
ファイル: Commodity.cs プロジェクト: vyolbius/nledger
        /// <summary>
        /// Ported from commodity_t::find_price
        /// </summary>
        public virtual PricePoint?FindPrice(Commodity commodity = null, DateTime moment = default(DateTime), DateTime oldest = default(DateTime))
        {
            Logger.Current.Debug("commodity.price.find", () => String.Format("commodity_t::find_price({0})", Symbol));

            Commodity target = null;

            if ((bool)commodity)
            {
                target = commodity;
            }
            else if ((bool)Pool.DefaultCommodity)
            {
                target = Pool.DefaultCommodity;
            }

            if ((bool)target && this == target)
            {
                return(null);
            }

            MemoizedPriceEntry entry = new MemoizedPriceEntry()
            {
                Start = moment, End = oldest, Commodity = commodity
            };

            Logger.Current.Debug("commodity.price.find", () => String.Format("looking for memoized args: {0},{1},{2}",
                                                                             !moment.IsNotADateTime() ? TimesCommon.Current.FormatDateTime(moment) : "NONE",
                                                                             !oldest.IsNotADateTime() ? TimesCommon.Current.FormatDateTime(oldest) : "NONE",
                                                                             commodity != null ? commodity.Symbol : "NONE"));

            PricePoint?memoizedPricePoint;

            if (Base.PriceMap.TryGetValue(entry, out memoizedPricePoint))
            {
                Logger.Current.Debug("commodity.price.find", () => String.Format("found! returning: {0}",
                                                                                 memoizedPricePoint.HasValue ? memoizedPricePoint.Value.Price : (Amount)0));
                return(memoizedPricePoint);
            }

            DateTime when;

            if (!moment.IsNotADateTime())
            {
                when = moment;
            }
            else if (TimesCommon.Current.Epoch.HasValue)
            {
                when = TimesCommon.Current.Epoch.Value;
            }
            else
            {
                when = TimesCommon.Current.CurrentDate;
            }

            if (Base.ValueExpr != null)
            {
                return(FindPriceFromExpr(Base.ValueExpr, commodity, when));
            }

            PricePoint?point = target != null
                ? Pool.CommodityPriceHistory.FindPrice(Referent, target, when, oldest)
                : Pool.CommodityPriceHistory.FindPrice(Referent, when, oldest);

            // Record this price point in the memoization map
            if (Base.PriceMap.Count > CommodityBase.MaxPriceMapSize)
            {
                Logger.Current.Debug("history.find", () => "price map has grown too large, clearing it by half");
                for (int i = 0; i < CommodityBase.MaxPriceMapSize / 2; i++)
                {
                    Base.PriceMap.Remove(Base.PriceMap.First());
                }
            }

            Logger.Current.Debug("history.find", () => String.Format("remembered: {0}", point.HasValue ? point.Value.Price : (Amount)0));
            Base.PriceMap.Add(entry, point);

            return(point);
        }