File indexing completed on 2024-05-12 16:42:33

0001 /*
0002     SPDX-FileCopyrightText: 2011 Fernando Vilas <fvilas@iname.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef MYMONEYBALANCECACHE_H
0007 #define MYMONEYBALANCECACHE_H
0008 
0009 // ----------------------------------------------------------------------------
0010 // QT Includes
0011 #include <QDate>
0012 #include <QHash>
0013 #include <QMap>
0014 #include <QString>
0015 
0016 // ----------------------------------------------------------------------------
0017 // KDE Includes
0018 
0019 // ----------------------------------------------------------------------------
0020 // Project Includes
0021 
0022 #include "mymoneymoney.h"
0023 
0024 /**
0025  * This class associates a @ref MyMoneyMoney object with a QDate. It is
0026  * essentially a std::pair. The object is invalid if the QDate is invalid
0027  * and the balance is @ref MyMoneyMoney::minValue. A call to isValid
0028  * should be used in most cases before using an object.
0029  */
0030 class KMM_MYMONEY_EXPORT MyMoneyBalanceCacheItem
0031 {
0032 public:
0033 
0034     /**
0035      * The object should be instantiated with a balance and a date.
0036      *
0037      * @param balance a MyMoneyMoney object that holds the balance at the
0038      * end of the day
0039      * @param date the date for the object
0040      */
0041     MyMoneyBalanceCacheItem(const MyMoneyMoney& balance, const QDate& date);
0042 
0043     /**
0044      * Accessor function for the balance as a MyMoneyMoney object.
0045      *
0046      * @return the balance in the object
0047      */
0048     const MyMoneyMoney& balance() const;
0049 
0050     /**
0051      * Accessor function for the date as a QDate object.
0052      *
0053      * @return the date in the object
0054      */
0055     const QDate& date() const;
0056 
0057     /**
0058      * Check the validity of the object
0059      *
0060      * @return true if the date is valid, false otherwise
0061      */
0062     bool isValid() const;
0063 
0064 private:
0065     MyMoneyMoney m_balance;
0066     QDate m_date;
0067 };
0068 
0069 /**
0070  * This class provides a balance cache. It holds balances by account and date,
0071  * so the most recent balance could be obtained to reduce the number of
0072  * transactions required to calculate a balance for a later date. This class
0073  * is intended to be used at the @ref MyMoneyFile layer.
0074  */
0075 class KMM_MYMONEY_EXPORT MyMoneyBalanceCache
0076 {
0077 public:
0078 
0079     /**
0080      * Remove all balances from the cache
0081      */
0082     void clear();
0083 
0084     /**
0085      * Remove all balances associated with a given account from the cache
0086      */
0087     void clear(const QString& accountId);
0088 
0089     /**
0090      * Remove all balances on or after the date associated with an account
0091      * from the cache
0092      */
0093     void clear(const QString& accountId, const QDate& date);
0094 
0095     /**
0096      * @return true if there are no balances in the cache, otherwise false
0097      */
0098     bool isEmpty() const;
0099 
0100     /**
0101      * This function returns the size of the cache.
0102      *
0103      * @note  It has linear complexity O(num of accounts), so do not
0104      * call it in a loop if there are many accounts stored in it or
0105      * performance is an issue.
0106      *
0107      * @return the number of balances currently in the cache
0108      */
0109     int size() const;
0110 
0111     /**
0112      * This function retrieves the balance from the cache. The balance is
0113      * invalid if the cache does not contain a balance on that date.
0114      *
0115      * @param accountId the account id to use to find the balance
0116      * @param date the date of the balance to retrieve
0117      *
0118      * @return the balance of the account at the end of the date given
0119      */
0120     MyMoneyBalanceCacheItem balance(const QString& accountId, const QDate& date) const;
0121 
0122     /**
0123      * This function retrieves the balance from the cache having a date less
0124      * than or equal to the date given. The balance is invalid if the cache
0125      * does not contain a balance on or before that date for the account given
0126      *
0127      * @param accountId the account id to use to find the balance
0128      * @param date the latest date of the balance to retrieve
0129      *
0130      * @return the balance of the account at the end the nearest day on or
0131      * before the date provided
0132      */
0133     MyMoneyBalanceCacheItem mostRecentBalance(const QString& accountId, const QDate& date) const;
0134 
0135     /**
0136      * This function inserts the balance into the cache for the account on
0137      * the date provided. If a balance already exists for that account and
0138      * date, it is updated to the one provided.
0139      *
0140      * @param accountId the account id
0141      * @param date the date of the balance
0142      * @param balance the balance of the account at the end the day
0143      */
0144     void insert(const QString& accountId, const QDate& date, const MyMoneyMoney& balance);
0145 
0146 private:
0147     typedef QHash<QString, QMap<QDate, MyMoneyMoney> > BalanceCacheType;
0148     BalanceCacheType m_cache;
0149 };
0150 
0151 #endif