• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DISTRIBUTEDDATAMGR_PASTEBOARD_DEDUPLICATE_MEMORY_H
17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_DEDUPLICATE_MEMORY_H
18 
19 #include "pasteboard_linked_list.h"
20 
21 namespace OHOS {
22 namespace MiscServices {
23 
24 template <typename T>
25 class DeduplicateMemory {
26 public:
27     explicit DeduplicateMemory(int64_t expirationMilliSeconds);
28     ~DeduplicateMemory();
29     bool IsDuplicate(const T &data);
30 
31 private:
32     int64_t GetTimestamp();
33     void ClearExpiration();
34     bool FindExist(const T &data);
35 
36     struct MemoryIdentity {
37         int64_t timestamp;
38         const T data;
39     };
40 
41     LinkedList<MemoryIdentity> memory_;
42     int64_t expirationMS_;
43 };
44 
45 template <typename T>
DeduplicateMemory(int64_t expirationMilliSeconds)46 DeduplicateMemory<T>::DeduplicateMemory(int64_t expirationMilliSeconds) : expirationMS_(expirationMilliSeconds)
47 {
48 }
49 
50 template <typename T>
~DeduplicateMemory()51 DeduplicateMemory<T>::~DeduplicateMemory()
52 {
53     memory_.Clear();
54 }
55 
56 template <typename T>
IsDuplicate(const T & data)57 bool DeduplicateMemory<T>::IsDuplicate(const T &data)
58 {
59     ClearExpiration();
60     if (FindExist(data)) {
61         return true;
62     }
63     int64_t timestamp = GetTimestamp();
64     memory_.InsertFront(MemoryIdentity({.timestamp = timestamp, .data = data}));
65     return false;
66 }
67 
68 template <typename T>
FindExist(const T & data)69 bool DeduplicateMemory<T>::FindExist(const T &data)
70 {
71     return memory_.FindExist([data](const MemoryIdentity &identity) {
72         return identity.data == data;
73     });
74 }
75 
76 template <typename T>
GetTimestamp()77 int64_t DeduplicateMemory<T>::GetTimestamp()
78 {
79     return std::chrono::duration_cast<std::chrono::milliseconds>(
80         std::chrono::steady_clock::now().time_since_epoch()).count();
81 }
82 
83 template <typename T>
ClearExpiration()84 void DeduplicateMemory<T>::ClearExpiration()
85 {
86     int64_t timestamp = GetTimestamp();
87     if (timestamp < expirationMS_) {
88         return;
89     }
90     int64_t expirationTimestamp = timestamp - expirationMS_;
91     memory_.RemoveIf([expirationTimestamp](const MemoryIdentity &identity) {
92         return expirationTimestamp > identity.timestamp;
93     });
94 }
95 } // namespace MiscServices
96 } // namespace OHOS
97 
98 #endif // DISTRIBUTEDDATAMGR_PASTEBOARD_DEDUPLICATE_MEMORY_H
99