-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add when(Object o) method matching interface
- Loading branch information
Showing
8 changed files
with
166 additions
and
25 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
generator/src/main/java/com/leacox/motif/cases/PrimitiveCasesGenerator.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,58 @@ | ||
/* | ||
* Copyright (C) 2015 John Leacox | ||
* | ||
* 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 | ||
* | ||
* https://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 com.leacox.motif.cases; | ||
|
||
import com.leacox.motif.generate.CasesGenerator; | ||
import com.leacox.motif.generate.Match1MethodSpec; | ||
import com.leacox.motif.tuple.Tuple1; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.JavaFile; | ||
import com.squareup.javapoet.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeName; | ||
import com.squareup.javapoet.TypeVariableName; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author John Leacox | ||
*/ | ||
final class PrimitiveCasesGenerator{ | ||
private PrimitiveCasesGenerator() { | ||
} | ||
|
||
public static void main(String[] args) { | ||
TypeName A = TypeVariableName.get("A"); | ||
TypeName t = ParameterizedTypeName.get(ClassName.get(Tuple1.class), A); | ||
|
||
Match1MethodSpec byteMatch = Match1MethodSpec.builder() | ||
.withName("aByte").withSummaryJavadoc("Matches a byte.\n") | ||
.withMatchExtractor(PrimitiveFieldExtractor.class, Byte.class).withParamA(TypeName.get(byte.class), "b").build(); | ||
|
||
JavaFile primitiveCasesFile = CasesGenerator.newBuilder( | ||
"com.leacox.motif.cases", "PrimitiveCases", t) | ||
.addFileComment(Copyright.COPYRIGHT_NOTICE) | ||
.addJavadoc("Motif cases for matching a primitive types.\n") | ||
.addMatch1Method(byteMatch) | ||
.build().generate(); | ||
|
||
try { | ||
primitiveCasesFile.writeTo(System.out); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
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
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
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
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
37 changes: 37 additions & 0 deletions
37
motif/src/main/java/com/leacox/motif/matching/IdentityFieldExtractor.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,37 @@ | ||
/* | ||
* Copyright (C) 2015 John Leacox | ||
* | ||
* 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 | ||
* | ||
* https://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 com.leacox.motif.matching; | ||
|
||
import com.leacox.motif.extract.FieldExtractor; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author John Leacox | ||
*/ | ||
final class IdentityFieldExtractor<T> implements FieldExtractor<T> { | ||
@Override | ||
public Optional<List<Object>> unapply(T value) { | ||
return Optional.of(Collections.singletonList(value)); | ||
} | ||
|
||
@Override | ||
public Class<?> getExtractorClass() { | ||
return Object.class; | ||
} | ||
} |
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
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