Skip to content

Commit b1de962

Browse files
authored
Function compatible mapper (helidon-io#1637)
Signed-off-by: Daniel Kec <daniel.kec@oracle.com>
1 parent 542e3ff commit b1de962

14 files changed

Lines changed: 23 additions & 16 deletions

File tree

common/mapper/src/main/java/io/helidon/common/mapper/Mapper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package io.helidon.common.mapper;
1717

18+
import java.util.function.Function;
19+
1820
/**
1921
* A generic and general approach to mapping two types.
2022
* A mapper is unidirectional - from {@code SOURCE} to {@code TARGET}.
@@ -23,12 +25,17 @@
2325
* @param <TARGET> type of the supported target
2426
*/
2527
@FunctionalInterface
26-
public interface Mapper<SOURCE, TARGET> {
28+
public interface Mapper<SOURCE, TARGET> extends Function<SOURCE, TARGET> {
2729
/**
2830
* Map an instance of source type to an instance of target type.
2931
*
3032
* @param source object to map
3133
* @return result of the mapping
3234
*/
3335
TARGET map(SOURCE source);
36+
37+
@Override
38+
default TARGET apply(SOURCE source){
39+
return map(source);
40+
}
3441
}

media/common/src/main/java/io/helidon/media/common/ByteChannelBodyWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Publisher<DataChunk> write(Single<ReadableByteChannel> content, GenericTy
5353
MessageBodyWriterContext context) {
5454

5555
context.contentType(MediaType.APPLICATION_OCTET_STREAM);
56-
return content.flatMap(mapper::map);
56+
return content.flatMap(mapper);
5757
}
5858

5959
/**

media/common/src/main/java/io/helidon/media/common/CharSequenceBodyWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Publisher<DataChunk> write(Single<CharSequence> content, GenericType<? ex
4545
MessageBodyWriterContext context) {
4646

4747
context.contentType(MediaType.TEXT_PLAIN);
48-
return content.flatMap(new CharSequenceToChunks(context.charset())::map);
48+
return content.flatMap(new CharSequenceToChunks(context.charset()));
4949
}
5050

5151
/**

media/common/src/main/java/io/helidon/media/common/ContentReaders.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static Single<byte[]> readBytes(Publisher<DataChunk> chunks) {
6363
* @return Single
6464
*/
6565
public static Single<String> readString(Publisher<DataChunk> chunks, Charset charset) {
66-
return readBytes(chunks).map(new BytesToString(charset)::map);
66+
return readBytes(chunks).map(new BytesToString(charset));
6767
}
6868

6969
/**
@@ -75,7 +75,7 @@ public static Single<String> readString(Publisher<DataChunk> chunks, Charset cha
7575
*/
7676
public static Single<String> readURLEncodedString(Publisher<DataChunk> chunks,
7777
Charset charset) {
78-
return readString(chunks, charset).map(new StringToDecodedString(charset)::map);
78+
return readString(chunks, charset).map(new StringToDecodedString(charset));
7979
}
8080

8181
/**

media/common/src/main/java/io/helidon/media/common/FileBodyWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
5050

5151
@Override
5252
public Publisher<DataChunk> write(Single<File> content, GenericType<? extends File> type, MessageBodyWriterContext context) {
53-
return content.flatMap(new FileToChunks(DEFAULT_RETRY_SCHEMA, context)::map);
53+
return content.flatMap(new FileToChunks(DEFAULT_RETRY_SCHEMA, context));
5454
}
5555

5656
/**

media/common/src/main/java/io/helidon/media/common/MessageBodyWriterContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public <T> Publisher<DataChunk> marshall(Single<T> content, GenericType<T> type,
260260
return applyFilters(Multi.<DataChunk>empty());
261261
}
262262
if (byte[].class.equals(type.rawType())) {
263-
return applyFilters(((Single<byte[]>) content).flatMap(BYTES_MAPPER::map));
263+
return applyFilters(((Single<byte[]>) content).flatMap(BYTES_MAPPER));
264264
}
265265
MessageBodyWriter<T> writer;
266266
if (fallback != null) {

media/common/src/main/java/io/helidon/media/common/PathBodyWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
4949

5050
@Override
5151
public Publisher<DataChunk> write(Single<Path> content, GenericType<? extends Path> type, MessageBodyWriterContext context) {
52-
return content.flatMap(new PathToChunks(DEFAULT_RETRY_SCHEMA, context)::map);
52+
return content.flatMap(new PathToChunks(DEFAULT_RETRY_SCHEMA, context));
5353
}
5454

5555
/**

media/common/src/main/java/io/helidon/media/common/ThrowableBodyWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Publisher<DataChunk> write(Single<Throwable> content,
4949
GenericType<? extends Throwable> type,
5050
MessageBodyWriterContext context) {
5151
context.contentType(MediaType.TEXT_PLAIN);
52-
return content.flatMap(new ThrowableToChunks(context.charset())::map);
52+
return content.flatMap(new ThrowableToChunks(context.charset()));
5353
}
5454

5555
/**

media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonBodyReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public boolean accept(GenericType<?> type, MessageBodyReaderContext context) {
5252
public <U extends Object> Single<U> read(Publisher<DataChunk> publisher,
5353
GenericType<U> type, MessageBodyReaderContext context) {
5454

55-
return ContentReaders.readBytes(publisher).map(new BytesToObject<>(type, objectMapper)::map);
55+
return ContentReaders.readBytes(publisher).map(new BytesToObject<>(type, objectMapper));
5656
}
5757

5858
/**

media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonBodyWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Publisher<DataChunk> write(Single<Object> content, GenericType<? extends
5656

5757
MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
5858
context.contentType(contentType);
59-
return content.flatMap(new ObjectToChunks(objectMapper, context.charset())::map);
59+
return content.flatMap(new ObjectToChunks(objectMapper, context.charset()));
6060
}
6161

6262
/**

0 commit comments

Comments
 (0)