File indexing completed on 2024-05-19 05:07:14

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