Skip to content

Commit 05275db

Browse files
committed
Do not deprecate monoid/ord/semigroup constructors for non java 8 users.
1 parent 5f73cac commit 05275db

File tree

5 files changed

+83
-48
lines changed

5 files changed

+83
-48
lines changed

core/src/main/java/fj/Equal.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,28 @@ public boolean equal(B b1, B b2) {
135135
/**
136136
* Constructs an equal instance from the given function.
137137
*
138+
* Java 8+ users: use {@link #equalDef(Definition)} instead.
139+
*
138140
* @param f The function to construct the equal with.
139141
* @return An equal instance from the given function.
140142
*/
141143
public static <A> Equal<A> equal(final F<A, F<A, Boolean>> f) {
142144
return new Equal<>(f::f);
143145
}
144146

147+
148+
/**
149+
* Constructs an equal instance from the given function.
150+
*
151+
* Java 8+ users: use {@link #equalDef(AltDefinition)} instead.
152+
*
153+
* @param f The function to construct the equal with.
154+
* @return An equal instance from the given function.
155+
*/
156+
public static <A> Equal<A> equal(final F2<A, A, Boolean> f) {
157+
return equalDef(f::f);
158+
}
159+
145160
/**
146161
* Constructs an equal instance from the given definition.
147162
*

core/src/main/java/fj/Monoid.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public final class Monoid<A> {
3838

3939
private final Definition<A> def;
4040

41+
/**
42+
* Primitives functions of Monoid: minimal definition and overridable methods.
43+
*/
4144
public interface Definition<A> extends Semigroup.Definition<A> {
4245

4346
A empty();
@@ -91,6 +94,10 @@ public Definition<A> dual() {
9194
}
9295
}
9396

97+
98+
/**
99+
* Primitives functions of Monoid: alternative minimal definition and overridable methods.
100+
*/
94101
public interface AltDefinition<A> extends Definition<A> {
95102

96103
@Override
@@ -423,13 +430,12 @@ public static <A> Monoid<A> monoidDef(final Semigroup.AltDefinition<A> s, final
423430
/**
424431
* Constructs a monoid from the given sum function and zero value, which must follow the monoidal
425432
* laws.
426-
* @deprecated since 4.7. Use {@link #monoidDef(Semigroup.Definition, Object)} instead.
433+
* Java 8+ users: use {@link #monoidDef(Semigroup.Definition, Object)} instead.
427434
*
428435
* @param sum The sum function for the monoid.
429436
* @param zero The zero for the monoid.
430437
* @return A monoid instance that uses the given sun function and zero value.
431438
*/
432-
@Deprecated
433439
public static <A> Monoid<A> monoid(final F<A, F<A, A>> sum, final A zero) {
434440
return new Monoid<>(new AltDefinition<A>() {
435441
@Override
@@ -447,13 +453,13 @@ public A empty() {
447453
/**
448454
* Constructs a monoid from the given sum function and zero value, which must follow the monoidal
449455
* laws.
450-
* @deprecated since 4.7. Use {@link #monoidDef(Semigroup.Definition, Object)} instead.
456+
*
457+
* Java 8+ users: use {@link #monoidDef(Semigroup.Definition, Object)} instead.
451458
*
452459
* @param sum The sum function for the monoid.
453460
* @param zero The zero for the monoid.
454461
* @return A monoid instance that uses the given sun function and zero value.
455462
*/
456-
@Deprecated
457463
public static <A> Monoid<A> monoid(final F2<A, A, A> sum, final A zero) {
458464
return new Monoid<>(new Definition<A>() {
459465
@Override

core/src/main/java/fj/Ord.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,27 @@ public final Ord<A> reverse() {
290290
/**
291291
* Returns an order instance that uses the given equality test and ordering function.
292292
*
293-
* @deprecated since 4.7. Use {@link #ordDef(Definition)}.
293+
* Java 8+ users: use {@link #ordDef(Definition)} instead.
294+
*
294295
* @param f The order function.
295296
* @return An order instance.
296297
*/
297-
@Deprecated
298298
public static <A> Ord<A> ord(final F<A, F<A, Ordering>> f) {
299299
return new Ord<>(f::f);
300300
}
301301

302+
/**
303+
* Returns an order instance that uses the given equality test and ordering function.
304+
*
305+
* Java 8+ users: use {@link #ordDef(AltDefinition)} instead.
306+
*
307+
* @param f The order function.
308+
* @return An order instance.
309+
*/
310+
public static <A> Ord<A> ord(final F2<A, A, Ordering> f) {
311+
return ordDef(f::f);
312+
}
313+
302314
/**
303315
* Returns an order instance that uses the given minimal equality test and ordering definition.
304316
*

core/src/main/java/fj/Semigroup.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
*/
3232
public final class Semigroup<A> {
3333

34+
/**
35+
* Primitives functions of Semigroup: minimal definition and overridable methods.
36+
*/
3437
public interface Definition<A> {
3538

3639
A append(A a1, A a2);
@@ -79,6 +82,9 @@ public A multiply1p(int n, A a) {
7982
}
8083
}
8184

85+
/**
86+
* Primitives functions of Semigroup: alternative minimal definition and overridable methods.
87+
*/
8288
public interface AltDefinition<A> extends Definition<A> {
8389
@Override
8490
F<A, A> prepend(A a);
@@ -285,24 +291,22 @@ public static <A> Semigroup<A> semigroupDef(final AltDefinition<A> def) {
285291

286292
/**
287293
* Constructs a semigroup from the given function.
288-
* @deprecated since 4.7. Use {@link #semigroupDef(Definition)}.
294+
* Java 8+ users: use {@link #semigroupDef(AltDefinition)} instead.
289295
*
290296
* @param sum The function to construct this semigroup with.
291297
* @return A semigroup from the given function.
292298
*/
293-
@Deprecated
294299
public static <A> Semigroup<A> semigroup(final F<A, F<A, A>> sum) {
295300
return semigroupDef(sum::f);
296301
}
297302

298303
/**
299304
* Constructs a semigroup from the given function.
300-
* @deprecated since 4.7. Use {@link #semigroupDef(Definition)}.
305+
* Java 8+ users: use {@link #semigroupDef(Definition)} instead.
301306
*
302307
* @param sum The function to construct this semigroup with.
303308
* @return A semigroup from the given function.
304309
*/
305-
@Deprecated
306310
public static <A> Semigroup<A> semigroup(final F2<A, A, A> sum) {
307311
return new Semigroup<>(sum::f);
308312
}

demo/src/main/java/fj/demo/concurrent/WordCount.java

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package fj.demo.concurrent;
22

3-
import static fj.Monoid.monoid;
4-
import static fj.Monoid.monoidDef;
5-
import static fj.control.parallel.ParModule.parModule;
6-
import static fj.data.List.nil;
7-
import static java.util.concurrent.Executors.newFixedThreadPool;
3+
import fj.F;
4+
import fj.Monoid;
5+
import fj.P;
6+
import fj.P1;
7+
import fj.P2;
8+
import fj.control.parallel.ParModule;
9+
import fj.control.parallel.Promise;
10+
import fj.control.parallel.Strategy;
11+
import fj.data.IOFunctions;
12+
import fj.data.Iteratee.Input;
13+
import fj.data.Iteratee.IterV;
14+
import fj.data.List;
15+
import fj.data.Option;
816

917
import java.io.BufferedWriter;
1018
import java.io.File;
@@ -14,35 +22,25 @@
1422
import java.io.Reader;
1523
import java.io.StringWriter;
1624
import java.io.Writer;
17-
import java.nio.charset.Charset;
1825
import java.util.Collections;
1926
import java.util.HashMap;
2027
import java.util.Map;
2128
import java.util.concurrent.ExecutorService;
2229

23-
import fj.F;
24-
import fj.Monoid;
25-
import fj.P;
26-
import fj.P1;
27-
import fj.P2;
28-
import fj.Unit;
29-
import fj.control.parallel.ParModule;
30-
import fj.control.parallel.Promise;
31-
import fj.control.parallel.Strategy;
32-
import fj.data.*;
33-
import fj.data.Iteratee.Input;
34-
import fj.data.Iteratee.IterV;
35-
import fj.function.Effect1;
30+
import static fj.Monoid.monoid;
31+
import static fj.control.parallel.ParModule.parModule;
32+
import static fj.data.List.nil;
33+
import static java.util.concurrent.Executors.newFixedThreadPool;
3634

3735
/**
3836
* Reads words and their counts from files ({@link #getWordsAndCountsFromFiles} in a single thread
3937
* and {@link #getWordsAndCountsFromFilesInParallel} in multiple threads). The files are created
4038
* initially and populated with some sample content.
41-
*
39+
*
4240
* @author Martin Grotzke
4341
*/
4442
public class WordCount {
45-
43+
4644
// Integers.add.f(1) caused an SOE...
4745
private static final F<Integer,Integer> addOne = a -> a.intValue() + 1;
4846

@@ -55,23 +53,23 @@ private static <K, V> Map<K, V> update(Map<K, V> map, K key, F<V, V> valueFuncti
5553
map.put(key, valueFunction.f(value));
5654
return map;
5755
}
58-
56+
5957
private static final F<String, Map<String, Integer>> fileNameToWordsAndCountsWithCharChunkIteratee = fileName -> {
6058
try {
6159
return IOFunctions.enumFileCharChunks(new File(fileName), Option.none(), wordCountsFromCharChunks()).run().run();
6260
} catch (final IOException e) {
6361
throw new RuntimeException(e);
6462
}
6563
};
66-
64+
6765
private static final F<String, Map<String, Integer>> fileNameToWordsAndCountsWithCharChunk2Iteratee = fileName -> {
6866
try {
6967
return IOFunctions.enumFileChars(new File(fileName), Option.none(), wordCountsFromChars()).run().run();
7068
} catch (final IOException e) {
7169
throw new RuntimeException(e);
7270
}
7371
};
74-
72+
7573
private static final F<String, Map<String, Integer>> fileNameToWordsAndCountsWithCharIteratee = fileName -> {
7674
try {
7775
return IOFunctions.enumFileChars(new File(fileName), Option.none(), wordCountsFromChars()).run().run();
@@ -181,7 +179,7 @@ public static void main(String[] args) throws IOException {
181179
final Map<String, Integer> expectedWordsAndCounts = result._2();
182180
long avgSize = fileNames.foldLeft((a, file) -> a.longValue() + new File(file).length(), 0l) / fileNames.length();
183181
System.out.println("Processing " + numFiles + " files with ~"+numSharedWords+" words and an avg size of " + avgSize + " bytes.");
184-
182+
185183
// warmup
186184
for(int i = 0; i < 1; i++) {
187185
// getWordsAndCountsFromFiles(fileNames.take(1)).size();
@@ -211,7 +209,7 @@ public static void main(String[] args) throws IOException {
211209
assertTrue(wordsAndCountsFromFiles != null);
212210
assertEquals(wordsAndCountsFromFiles.size(), numFiles + numSharedWords);
213211
assertEquals(wordsAndCountsFromFiles, expectedWordsAndCounts);
214-
212+
215213
System.gc();
216214

217215
// get word counts sequentially / single threaded \w iteratee
@@ -223,7 +221,7 @@ public static void main(String[] args) throws IOException {
223221
assertEquals(wordsAndCountsFromFiles, expectedWordsAndCounts);
224222

225223
System.gc();
226-
224+
227225
// get word counts sequentially / single threaded \w iteratee
228226
start = System.currentTimeMillis();
229227
wordsAndCountsFromFiles = getWordsAndCountsFromFilesWithIteratee(fileNames, fileNameToWordsAndCountsWithCharChunk2Iteratee);
@@ -233,7 +231,7 @@ public static void main(String[] args) throws IOException {
233231
assertEquals(wordsAndCountsFromFiles, expectedWordsAndCounts);
234232

235233
System.gc();
236-
234+
237235
// start = System.currentTimeMillis();
238236
// wordsAndCountsFromFiles = getWordsAndCountsFromFilesInParallel(fileNames, fileNameToWordsAndCounts, 8);
239237
// System.out.println("Getting word counts in 8 threads took " + (System.currentTimeMillis() - start) + " ms.");
@@ -249,14 +247,14 @@ public static void main(String[] args) throws IOException {
249247
assertEquals(wordsAndCountsFromFiles, expectedWordsAndCounts);
250248

251249
System.gc();
252-
250+
253251
start = System.currentTimeMillis();
254252
wordsAndCountsFromFiles = getWordsAndCountsFromFilesInParallel(fileNames, fileNameToWordsAndCountsWithCharChunkIteratee, 32);
255253
System.out.println("Getting word counts in 32 threads with char chunk iteratee took " + (System.currentTimeMillis() - start) + " ms.");
256254
assertTrue(wordsAndCountsFromFiles != null);
257255
assertEquals(wordsAndCountsFromFiles.size(), numFiles + numSharedWords);
258256
assertEquals(wordsAndCountsFromFiles, expectedWordsAndCounts);
259-
257+
260258
// we have tmpfiles, but still want to be sure not to leave rubbish
261259
fileNames.foreachDoEffect(a -> new File(a).delete());
262260
}
@@ -286,13 +284,13 @@ private static P2<List<String>, Map<String, Integer>> writeSampleFiles(
286284
}
287285
return P.p(fileNames, expectedWordsAndCounts);
288286
}
289-
287+
290288
public static Map<String, Integer> getWordsAndCountsFromFilesWithIteratee(final List<String> fileNames,
291289
final F<String, Map<String, Integer>> fileNameToWordsAndCountsWithIteratee) {
292290
final List<Map<String, Integer>> maps = fileNames.map(fileNameToWordsAndCountsWithIteratee);
293291
return maps.foldLeft(WordCount::plus, new HashMap<String, Integer>());
294292
}
295-
293+
296294
public static Map<String, Integer> getWordsAndCountsFromFilesInParallel(
297295
final List<String> fileNames, final F<String, Map<String, Integer>> fileNameToWordsAndCounts, int numThreads) {
298296
final ExecutorService pool = newFixedThreadPool(numThreads);
@@ -305,14 +303,14 @@ public static Map<String, Integer> getWordsAndCountsFromFilesInParallel(
305303

306304
return result;
307305
}
308-
306+
309307
// Read documents and extract words and word counts of documents
310308
public static Promise<Map<String, Integer>> getWordsAndCountsFromFiles(
311309
final List<String> fileNames, final F<String, Map<String, Integer>> fileNameToWordsAndCounts, final ParModule m) {
312-
final Monoid<Map<String, Integer>> monoid = monoidDef(WordCount::plus, Collections.emptyMap());
310+
final Monoid<Map<String, Integer>> monoid = monoid(WordCount::plus, Collections.emptyMap());
313311
return m.parFoldMap(fileNames, fileNameToWordsAndCounts, monoid);
314312
}
315-
313+
316314
private static Map<String, Integer> plus(Map<String, Integer> a, Map<String, Integer> b) {
317315
final Map<String, Integer> result = new HashMap<>(a);
318316
for(Map.Entry<String, Integer> entry : b.entrySet()) {
@@ -321,7 +319,7 @@ private static Map<String, Integer> plus(Map<String, Integer> a, Map<String, Int
321319
}
322320
return result;
323321
}
324-
322+
325323
@SuppressWarnings("unused")
326324
private static String readFileToString(File file) throws IOException {
327325
Reader reader = null;
@@ -342,13 +340,13 @@ private static void copy(Reader reader, Writer writer) throws IOException {
342340
writer.write(buffer, 0, n);
343341
}
344342
}
345-
343+
346344
static void assertTrue(boolean condition) {
347345
if (!condition) {
348346
throw new AssertionError();
349347
}
350348
}
351-
349+
352350
static void assertEquals(Object actual, Object expected) {
353351
if (!expected.equals(actual)) {
354352
throw new IllegalArgumentException("Not equals. Expected: " + expected + ", actual: " + actual);

0 commit comments

Comments
 (0)