forked from mapstruct/mapstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapstruct#962 add conversions between Java 8 Stream and the different…
… Java Collection and Iterable types
- Loading branch information
Showing
8 changed files
with
557 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
.../src/main/java/org/mapstruct/ap/internal/conversion/JavaStreamToCollectionConversion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.ap.internal.conversion; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.NavigableSet; | ||
import java.util.Set; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
import org.mapstruct.ap.internal.model.common.ConversionContext; | ||
import org.mapstruct.ap.internal.model.common.Type; | ||
import org.mapstruct.ap.internal.model.common.TypeFactory; | ||
import org.mapstruct.ap.internal.util.Collections; | ||
import org.mapstruct.ap.internal.util.JavaStreamConstants; | ||
|
||
/** | ||
* Conversion between {@link java.util.stream.Stream} and java utils: {@link Set}, {@link java.util.List}, | ||
* {@link java.util.Collection}. | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
public class JavaStreamToCollectionConversion extends SimpleConversion { | ||
|
||
@Override | ||
protected String getToExpression(ConversionContext conversionContext) { | ||
return "<SOURCE>.collect( Collectors." + collector( conversionContext ) + " )"; | ||
} | ||
|
||
private String collector(ConversionContext conversionContext) { | ||
Type targetType = conversionContext.getTargetType(); | ||
TypeFactory typeFactory = conversionContext.getTypeFactory(); | ||
String collector = ""; | ||
// we must do erasure as JDK compiler does not work without it | ||
//TODO is this a bug in the Java compiler or not? | ||
if ( typeFactory.getType( Iterable.class ).isAssignableTo( targetType.erasure() ) | ||
|| typeFactory.getType( Collection.class ).isAssignableTo( targetType.erasure() ) | ||
|| typeFactory.getType( List.class ).isAssignableTo( targetType.erasure() ) ) { | ||
collector = "toList()"; | ||
} | ||
else if ( typeFactory.getType( Set.class ).isAssignableTo( targetType.erasure() ) ) { | ||
collector = "toSet()"; | ||
} | ||
else if ( acceptsTreeSet( conversionContext ) ) { | ||
collector = "toCollection( TreeSet::new )"; | ||
} | ||
|
||
//TODO what if we register wrong. In the end it won't compile, but should we throw an exception? | ||
return collector; | ||
} | ||
|
||
@Override | ||
protected String getFromExpression(ConversionContext conversionContext) { | ||
return "<SOURCE>.stream()"; | ||
} | ||
|
||
private boolean acceptsTreeSet(ConversionContext conversionContext) { | ||
TypeFactory typeFactory = conversionContext.getTypeFactory(); | ||
Type targetType = conversionContext.getTargetType(); | ||
return typeFactory.getType( SortedSet.class ).isAssignableTo( targetType.erasure() ) | ||
|| typeFactory.getType( NavigableSet.class ).isAssignableTo( targetType.erasure() ); | ||
} | ||
|
||
|
||
@Override | ||
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) { | ||
Set<Type> types = Collections.asSet( | ||
conversionContext.getTypeFactory().getType( JavaStreamConstants.COLLECTORS_FQN ) | ||
); | ||
if ( acceptsTreeSet( conversionContext ) ) { | ||
types.add( conversionContext.getTypeFactory().getType( TreeSet.class ) ); | ||
} | ||
return types; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...or/src/main/java/org/mapstruct/ap/internal/conversion/JavaStreamToIterableConversion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.ap.internal.conversion; | ||
|
||
import java.util.Set; | ||
|
||
import org.mapstruct.ap.internal.model.common.ConversionContext; | ||
import org.mapstruct.ap.internal.model.common.Type; | ||
import org.mapstruct.ap.internal.util.Collections; | ||
import org.mapstruct.ap.internal.util.JavaStreamConstants; | ||
|
||
/** | ||
* Conversion between {@link java.util.stream.Stream} and java util {@link Iterable}. | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
public class JavaStreamToIterableConversion extends JavaStreamToCollectionConversion { | ||
|
||
@Override | ||
protected String getFromExpression(ConversionContext conversionContext) { | ||
return "StreamSupport.stream( <SOURCE>.spliterator(), false )"; | ||
} | ||
|
||
|
||
@Override | ||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) { | ||
return Collections.asSet( | ||
conversionContext.getTypeFactory().getType( JavaStreamConstants.STREAM_SUPPORT_FQN ) | ||
); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
processor/src/main/java/org/mapstruct/ap/internal/util/JavaStreamConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.ap.internal.util; | ||
|
||
/** | ||
* Helper holding Java Stream full qualified class names for conversion registration | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
public final class JavaStreamConstants { | ||
|
||
public static final String STREAM_FQN = "java.util.stream.Stream"; | ||
public static final String COLLECTORS_FQN = "java.util.stream.Collectors"; | ||
public static final String STREAM_SUPPORT_FQN = "java.util.stream.StreamSupport"; | ||
|
||
|
||
private JavaStreamConstants() { | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
processor/src/test/java/org/mapstruct/ap/test/conversion/java8stream/Source.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.ap.test.conversion.java8stream; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class Source { | ||
|
||
|
||
private List<Integer> ints; | ||
|
||
private Stream<Integer> stream; | ||
|
||
private Stream<String> stringStream; | ||
|
||
private Stream<String> stringCollection; | ||
|
||
private Stream<Integer> integerSet; | ||
|
||
private Stream<Integer> integerIterable; | ||
|
||
private Stream<Integer> sortedSet; | ||
|
||
private Stream<Integer> navigableSet; | ||
|
||
public List<Integer> getInts() { | ||
return ints; | ||
} | ||
|
||
public void setInts(List<Integer> ints) { | ||
this.ints = ints; | ||
} | ||
|
||
public Stream<Integer> getStream() { | ||
return stream; | ||
} | ||
|
||
public void setStream(Stream<Integer> stream) { | ||
this.stream = stream; | ||
} | ||
|
||
public Stream<String> getStringStream() { | ||
return stringStream; | ||
} | ||
|
||
public void setStringStream(Stream<String> stringStream) { | ||
this.stringStream = stringStream; | ||
} | ||
|
||
public Stream<String> getStringCollection() { | ||
return stringCollection; | ||
} | ||
|
||
public void setStringCollection(Stream<String> stringCollection) { | ||
this.stringCollection = stringCollection; | ||
} | ||
|
||
public Stream<Integer> getIntegerSet() { | ||
return integerSet; | ||
} | ||
|
||
public void setIntegerSet(Stream<Integer> integerSet) { | ||
this.integerSet = integerSet; | ||
} | ||
|
||
public Stream<Integer> getIntegerIterable() { | ||
return integerIterable; | ||
} | ||
|
||
public void setIntegerIterable(Stream<Integer> integerIterable) { | ||
this.integerIterable = integerIterable; | ||
} | ||
|
||
public Stream<Integer> getSortedSet() { | ||
return sortedSet; | ||
} | ||
|
||
public void setSortedSet(Stream<Integer> sortedSet) { | ||
this.sortedSet = sortedSet; | ||
} | ||
|
||
public Stream<Integer> getNavigableSet() { | ||
return navigableSet; | ||
} | ||
|
||
public void setNavigableSet(Stream<Integer> navigableSet) { | ||
this.navigableSet = navigableSet; | ||
} | ||
} |
Oops, something went wrong.