-
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.
standardize usage of ConversionExprFactory
- Loading branch information
Showing
9 changed files
with
410 additions
and
46 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
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
47 changes: 47 additions & 0 deletions
47
...re/src/main/java/rapier/core/conversion/expr/BooleanToPrimitiveConversionExprFactory.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 @@ | ||
/*- | ||
* =================================LICENSE_START================================== | ||
* rapier-core | ||
* ====================================SECTION===================================== | ||
* Copyright (C) 2024 - 2025 Andy Boothe | ||
* ====================================SECTION===================================== | ||
* 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. | ||
* ==================================LICENSE_END=================================== | ||
*/ | ||
package rapier.core.conversion.expr; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import java.util.Optional; | ||
import javax.lang.model.type.TypeKind; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Types; | ||
import rapier.core.ConversionExprFactory; | ||
|
||
public class BooleanToPrimitiveConversionExprFactory implements ConversionExprFactory { | ||
private final Types types; | ||
|
||
public BooleanToPrimitiveConversionExprFactory(Types types) { | ||
this.types = requireNonNull(types); | ||
} | ||
|
||
@Override | ||
public Optional<String> generateConversionExpr(TypeMirror targetType, String sourceValue) { | ||
if (targetType.getKind() == TypeKind.BOOLEAN) | ||
return Optional.of(sourceValue + ".booleanValue()"); | ||
return Optional.empty(); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private Types getTypes() { | ||
return types; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...-core/src/main/java/rapier/core/conversion/expr/BooleanToStringConversionExprFactory.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 @@ | ||
/*- | ||
* =================================LICENSE_START================================== | ||
* rapier-core | ||
* ====================================SECTION===================================== | ||
* Copyright (C) 2024 - 2025 Andy Boothe | ||
* ====================================SECTION===================================== | ||
* 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. | ||
* ==================================LICENSE_END=================================== | ||
*/ | ||
package rapier.core.conversion.expr; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import java.util.Optional; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Types; | ||
import rapier.core.ConversionExprFactory; | ||
|
||
public class BooleanToStringConversionExprFactory implements ConversionExprFactory { | ||
private final Types types; | ||
|
||
public BooleanToStringConversionExprFactory(Types types) { | ||
this.types = requireNonNull(types); | ||
} | ||
|
||
@Override | ||
public Optional<String> generateConversionExpr(TypeMirror targetType, String sourceValue) { | ||
if (!targetType.toString().equals("java.lang.String")) | ||
return Optional.empty(); | ||
|
||
return Optional.of(sourceValue + ".toString()"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private Types getTypes() { | ||
return types; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
rapier-core/src/main/java/rapier/core/util/ConversionExprFactories.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,67 @@ | ||
/*- | ||
* =================================LICENSE_START================================== | ||
* rapier-core | ||
* ====================================SECTION===================================== | ||
* Copyright (C) 2024 - 2025 Andy Boothe | ||
* ====================================SECTION===================================== | ||
* 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. | ||
* ==================================LICENSE_END=================================== | ||
*/ | ||
package rapier.core.util; | ||
|
||
import javax.annotation.processing.ProcessingEnvironment; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
import rapier.core.ConversionExprFactory; | ||
import rapier.core.conversion.expr.ConversionExprFactoryChain; | ||
import rapier.core.conversion.expr.ElementwiseListConversionExprFactory; | ||
import rapier.core.conversion.expr.FromStringConversionExprFactory; | ||
import rapier.core.conversion.expr.IdentityConversionExprFactory; | ||
import rapier.core.conversion.expr.SingleArgumentConstructorConversionExprFactory; | ||
import rapier.core.conversion.expr.StringToCharacterConversionExprFactory; | ||
import rapier.core.conversion.expr.StringToPrimitiveConversionExprFactory; | ||
import rapier.core.conversion.expr.ValueOfConversionExprFactory; | ||
|
||
public final class ConversionExprFactories { | ||
private ConversionExprFactories() {} | ||
|
||
public static ConversionExprFactory standardAmbiguousFromStringFactory(ProcessingEnvironment pe) { | ||
final Elements elements = pe.getElementUtils(); | ||
final Types types = pe.getTypeUtils(); | ||
final TypeMirror stringType = elements.getTypeElement(String.class.getCanonicalName()).asType(); | ||
return new ConversionExprFactoryChain(new IdentityConversionExprFactory(types, stringType), | ||
new StringToPrimitiveConversionExprFactory(types), | ||
new StringToCharacterConversionExprFactory(types), | ||
new ValueOfConversionExprFactory(types, stringType), | ||
new FromStringConversionExprFactory(types), | ||
new SingleArgumentConstructorConversionExprFactory(types, stringType)); | ||
} | ||
|
||
public static ConversionExprFactory standardAmbiguousFromListOfStringFactory( | ||
ProcessingEnvironment pe) { | ||
final Elements elements = pe.getElementUtils(); | ||
final Types types = pe.getTypeUtils(); | ||
final TypeMirror stringType = elements.getTypeElement(String.class.getCanonicalName()).asType(); | ||
final TypeMirror listOfStringType = | ||
types.getDeclaredType(elements.getTypeElement("java.util.List"), stringType); | ||
final ConversionExprFactory standardAmbiguousFromStringFactory = | ||
standardAmbiguousFromStringFactory(pe); | ||
return new ConversionExprFactoryChain( | ||
new IdentityConversionExprFactory(types, listOfStringType), | ||
new ValueOfConversionExprFactory(types, listOfStringType), | ||
new SingleArgumentConstructorConversionExprFactory(types, listOfStringType), | ||
new ElementwiseListConversionExprFactory(types, standardAmbiguousFromStringFactory)); | ||
} | ||
|
||
} |
Oops, something went wrong.