Skip to content

Commit 10ef4fe

Browse files
wip
1 parent bd87ed5 commit 10ef4fe

9 files changed

+172
-0
lines changed

src/Tests/Test_Estimator.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Tests;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import YASL.CEstimator;
7+
import YASL.CExactCollector;
8+
import YASL.CExactCounter;
9+
import YASL.IEstimator;
10+
11+
public class Test_Estimator {
12+
@Test
13+
public void simplest() {
14+
IEstimator<Integer> est = new CEstimator<Integer>( //
15+
new CExactCounter<>(), //
16+
new CExactCollector<>() //
17+
);
18+
for (int i = 1; i <= 4; i++) {
19+
for (int j = 0; j < i; j++) {
20+
est.add(i);
21+
}
22+
}
23+
24+
Assert.assertEquals( //
25+
"4 -> 4.0, 3 -> 3.0, 2 -> 2.0, 1 -> 1.0", //
26+
est.estimate().toString() //
27+
);
28+
}
29+
}

src/YASL/CEstimatedItems.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package YASL;
2+
3+
import java.util.Collection;
4+
5+
public class CEstimatedItems<T> {
6+
private final Collection<CEstimationFor<T>> _estimation;
7+
8+
public CEstimatedItems(Collection<CEstimationFor<T>> estimation) {
9+
this._estimation = estimation;
10+
}
11+
12+
@Override
13+
public String toString() {
14+
StringBuilder res = new StringBuilder();
15+
String del = "";
16+
for (CEstimationFor<T> item : _estimation) {
17+
res.append(del);
18+
res.append(item.toString());
19+
del = ", ";
20+
}
21+
return res.toString();
22+
}
23+
}

src/YASL/CEstimationFor.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package YASL;
2+
3+
public class CEstimationFor<T> implements Comparable<CEstimationFor<T>> {
4+
public final T Item;
5+
public final double Frequence;
6+
7+
public CEstimationFor(T item, double frequence) {
8+
Item = item;
9+
Frequence = frequence;
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return Item.toString() + " -> " + Frequence;
15+
}
16+
17+
@Override
18+
public int compareTo(CEstimationFor<T> o) {
19+
final double delta = o.Frequence - Frequence;
20+
if (0 < delta)
21+
return 1;
22+
if (delta < 0)
23+
return -1;
24+
return 0;
25+
}
26+
27+
}

src/YASL/CEstimator.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package YASL;
2+
3+
public class CEstimator<T> implements IEstimator<T> {
4+
private final IItemsCounter<T> _counter;
5+
private final IEstimationCollector<T> _collector;
6+
7+
public CEstimator( //
8+
IItemsCounter<T> counter, //
9+
IEstimationCollector<T> est //
10+
) {
11+
this._counter = counter;
12+
this._collector = est;
13+
}
14+
15+
@Override
16+
public void add(T item, long count) {
17+
final long cnt = _counter.put(item, count);
18+
_collector.put(item, cnt);
19+
}
20+
21+
@Override
22+
public CEstimatedItems<T> estimate() {
23+
return _collector.collect();
24+
}
25+
}

src/YASL/CExactCollector.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package YASL;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
6+
7+
public class CExactCollector<T> implements IEstimationCollector<T> {
8+
9+
private Map<T, Long> _storage = new HashMap<>();
10+
11+
@Override
12+
public void put(T item, long cnt) {
13+
_storage.put(item, cnt);
14+
}
15+
16+
@Override
17+
public CEstimatedItems<T> collect() {
18+
return new CEstimatedItems<>( //
19+
_storage.entrySet().stream() //
20+
.map(e -> new CEstimationFor<>(e.getKey(), e.getValue())) //
21+
.sorted() //
22+
.collect(Collectors.toList()) //
23+
);
24+
}
25+
}

src/YASL/CExactCounter.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package YASL;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/*
7+
* Just for testing.
8+
* Don't use in real applications, unless you know what you are doing.
9+
*/
10+
11+
public class CExactCounter<T> implements IItemsCounter<T> {
12+
13+
private final Map<T, Long> _counters = new HashMap<>();
14+
15+
@Override
16+
public long put(T item, long count) {
17+
Long cnt = _counters.get(item);
18+
cnt = count + ((null == cnt) ? 0 : cnt);
19+
_counters.put(item, cnt);
20+
return cnt;
21+
}
22+
}

src/YASL/IEstimationCollector.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package YASL;
2+
3+
public interface IEstimationCollector<T> {
4+
public void put(T item, long cnt);
5+
public CEstimatedItems<T> collect();
6+
}

src/YASL/IEstimator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package YASL;
2+
3+
public interface IEstimator<T> {
4+
public void add(T item, long count);
5+
public CEstimatedItems<T> estimate();
6+
7+
public default void add(T item) {
8+
add(item, 1);
9+
}
10+
}

src/YASL/IItemsCounter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package YASL;
2+
3+
public interface IItemsCounter<T> {
4+
public long put(T item, long count);
5+
}

0 commit comments

Comments
 (0)