Skip to content

Commit 2b8a80f

Browse files
committed
Add event processing API
* The EventDataHandler is a class intended for some heavy concurrent event operations (like writing to database or file). * The IEventDataFactory interface is intended for adding additional information to the event (like system info or timestamp) and also for performing event filtering. * The IEventDataCache interface is intended for event cashing, for example it can merge the EventData with same signature by summing count. * It also determines the order of EventData processing. For example it can retrive data based on the time of there signature registration, or it can retrive data with the biggest count first. DEVSIX-1958
1 parent b13db36 commit 2b8a80f

File tree

10 files changed

+914
-0
lines changed

10 files changed

+914
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2018 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel.counter.data;
45+
46+
/**
47+
* The data that class that contains event signature (in simple cases it can be just event type)
48+
* and count.
49+
* Is used in {@link EventDataHandler} for adding some additional information to the event before processing
50+
* and merging same events by increasing count.
51+
*
52+
* @param <T> the signature type
53+
*/
54+
public class EventData<T> {
55+
56+
private final T signature;
57+
private long count;
58+
59+
public EventData(T signature) {
60+
this(signature, 1);
61+
}
62+
63+
public EventData(T signature, long count) {
64+
this.signature = signature;
65+
this.count = count;
66+
}
67+
68+
/**
69+
* The signature that identifies this data.
70+
*
71+
* @return data signature
72+
*/
73+
public final T getSignature() {
74+
return signature;
75+
}
76+
77+
/**
78+
* Number of data instances with the same signature that where merged.
79+
*
80+
* @return data count
81+
*/
82+
public final long getCount() {
83+
return count;
84+
}
85+
86+
protected void mergeWith(EventData<T> data) {
87+
this.count += data.getCount();
88+
}
89+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2018 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel.counter.data;
45+
46+
import java.util.ArrayList;
47+
import java.util.Comparator;
48+
import java.util.HashMap;
49+
import java.util.List;
50+
import java.util.Map;
51+
import java.util.Set;
52+
import java.util.TreeSet;
53+
54+
/**
55+
* Comparator-based implementation of {@link IEventDataCache}.
56+
* Merges data with the same signature by increasing its count.
57+
* Retrieve the smallest element based on comparator.
58+
*
59+
* Not thread safe.
60+
*
61+
* @param <T> the data signature type
62+
* @param <V> the data type
63+
*/
64+
public class EventDataCacheComparatorBased<T, V extends EventData<T>> implements IEventDataCache<T, V> {
65+
66+
private Map<T, V> map = new HashMap<>();
67+
private Set<V> orderedCache;
68+
69+
public EventDataCacheComparatorBased(Comparator<V> comparator) {
70+
orderedCache = new TreeSet<>(comparator);
71+
}
72+
73+
@Override
74+
public void put(V data) {
75+
if (data != null) {
76+
V old = map.put(data.getSignature(), data);
77+
if (old != null) {
78+
orderedCache.remove(old);
79+
data.mergeWith(old);
80+
orderedCache.add(data);
81+
} else {
82+
orderedCache.add(data);
83+
}
84+
}
85+
}
86+
87+
@Override
88+
public V retrieveNext() {
89+
for (V data : orderedCache) {
90+
if (data != null) {
91+
map.remove(data.getSignature());
92+
orderedCache.remove(data);
93+
return data;
94+
}
95+
}
96+
return null;
97+
}
98+
99+
@Override
100+
public List<V> clear() {
101+
ArrayList<V> result = new ArrayList<>(map.values());
102+
map.clear();
103+
orderedCache.clear();
104+
return result;
105+
}
106+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2018 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel.counter.data;
45+
46+
import java.util.ArrayList;
47+
import java.util.HashMap;
48+
import java.util.LinkedList;
49+
import java.util.List;
50+
import java.util.Map;
51+
52+
/**
53+
* Queue-based implementation of {@link IEventDataCache}.
54+
* Merges data with the same signature by increasing its count.
55+
* Will retrieve the first elements by the time of its signature registration.
56+
*
57+
* Not thread safe.
58+
*
59+
* @param <T> the data signature type
60+
* @param <V> the data type
61+
*/
62+
public class EventDataCacheQueueBased<T, V extends EventData<T>> implements IEventDataCache<T, V> {
63+
64+
private Map<T, V> map = new HashMap<>();
65+
private LinkedList<T> signatureQueue = new LinkedList<>();
66+
67+
@Override
68+
public void put(V data) {
69+
if (data != null) {
70+
V old = map.put(data.getSignature(), data);
71+
if (old != null) {
72+
data.mergeWith(old);
73+
} else {
74+
signatureQueue.addLast(data.getSignature());
75+
}
76+
}
77+
}
78+
79+
@Override
80+
public V retrieveNext() {
81+
if (!signatureQueue.isEmpty()) {
82+
return map.remove(signatureQueue.pollFirst());
83+
}
84+
return null;
85+
}
86+
87+
@Override
88+
public List<V> clear() {
89+
ArrayList<V> result = new ArrayList<>(map.values());
90+
map.clear();
91+
signatureQueue.clear();
92+
return result;
93+
}
94+
}

0 commit comments

Comments
 (0)