Skip to content

Commit

Permalink
Clean up Java Code Generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredebelen committed Dec 31, 2024
1 parent d0791ad commit d061a3a
Show file tree
Hide file tree
Showing 29 changed files with 564 additions and 596 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ function meta::external::language::java::generation::essential::engineConvention
->addProvidedType(meta::pure::executionPlan::platformBinding::legendJava::PureDate, javaDate())
->addProvidedType(meta::pure::functions::date::DurationUnit, javaDurationUnit())
->addProvidedType(meta::pure::functions::date::DayOfWeek, javaDayOfWeek())

->meta::external::language::java::generation::grammar::boolean::registerBooleanLibrary()
->meta::external::language::java::generation::grammar::collection::registerCollectionsLibrary(true)
->meta::external::language::java::generation::grammar::lang::registerLangLibrary()
->meta::external::language::java::generation::grammar::math::registerMathsLibrary(true)
->meta::external::language::java::generation::grammar::string::registerStringLibrary()

->meta::external::language::java::generation::essential::boolean::registerBooleanLibrary()
->meta::external::language::java::generation::essential::collection::registerCollectionsLibrary(true)
->meta::external::language::java::generation::essential::pureDate::registerPureDateLibrary()
->meta::external::language::java::generation::essential::lang::registerLangLibrary()
->meta::external::language::java::generation::essential::assert::registerAssertLibrary()
->meta::external::language::java::generation::essential::math::registerMathsLibrary(true)
->meta::external::language::java::generation::essential::meta::registerMetaLibrary()
->meta::external::language::java::generation::essential::multiplicity::registerMultiplicityLibrary()
->meta::external::language::java::generation::essential::string::registerStringLibrary();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2020 Goldman Sachs
//
// 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.

import meta::external::language::java::factory::*;
import meta::external::language::java::metamodel::*;
import meta::external::language::java::metamodel::project::*;
import meta::external::language::java::transform::*;
import meta::pure::executionPlan::platformBinding::legendJava::*;
import meta::external::language::java::generation::essential::boolean::*;

function meta::external::language::java::generation::essential::boolean::registerBooleanLibrary(conventions: Conventions[1]): Conventions[1]
{
let library = $conventions->className(Library);

let lib = newConventionsLibrary()
->addFunctionCoders([
fc2(is_Any_1__Any_1__Boolean_1_, {ctx,left,right | $left->j_eq($right)})
]);

$conventions->registerLibrary($lib);
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,59 +34,45 @@ function meta::external::language::java::generation::essential::collection::regi
->usesJavaPackage('java.util.stream')
->usesBoolean(if($boxPrimitives, |javaBooleanBoxed(), |javaBoolean()), javaBooleanBoxed(), {b:Boolean[1] | j_boolean($b)})
->addFunctionCoders([
fc2(at_T_MANY__Integer_1__T_1_, {ctx,collection,index | $library->j_invoke('at', [$collection, $index], if($collection.type->isJavaList(), |$collection.type->elementTypeOfJavaList(), |$collection.type))}),
fc2(indexOf_T_MANY__T_1__Integer_1_, {ctx,collection,obj | $library->j_invoke('indexOf', [$collection, $obj], if($collection.type->isJavaList(), |$collection.type->elementTypeOfJavaList(), |$collection.type))}),
fc1(first_T_MANY__T_$0_1$_, {ctx,collection | first($collection, $library)}),
fc (head_T_MANY__T_$0_1$_, fcAlias( first_T_MANY__T_$0_1$_)),
fc2(exists_T_MANY__Function_1__Boolean_1_, {ctx,collection,func | $collection->j_streamOf()->js_anyMatch($func)}),

fc1(range_Integer_1__Integer_MANY_, {ctx,stop | javaLongStream()->j_invoke('range', [j_long(0), $stop])->j_invoke('boxed', [])}),
fc2(range_Integer_1__Integer_1__Integer_MANY_, {ctx,start,stop | javaLongStream()->j_invoke('range', [$start, $stop])->j_invoke('boxed', [])}),
fc3(range_Integer_1__Integer_1__Integer_1__Integer_MANY_, {ctx,start,stop,step | $library->j_invoke('range', [$start, $stop, $step], javaStream(javaLongBoxed()))}),
fc2(forAll_T_MANY__Function_1__Boolean_1_, {ctx,collection,func | $collection->j_streamOf()->js_allMatch($func)}),

fc (add_T_MANY__T_1__T_$1_MANY$_, fcAlias( concatenate_T_MANY__T_MANY__T_MANY_)),
fc2(concatenate_T_MANY__T_MANY__T_MANY_, {ctx,collection1,collection2 | javaStream()->j_invoke('concat', [$collection1->j_streamOf(), $collection2->j_streamOf()], javaStream($ctx.returnType()->elementType()))}),
fc2(contains_Any_MANY__Any_1__Boolean_1_, {ctx,collection,obj | if($collection.type->isJavaList(), |$collection->j_invoke('contains', $obj), |javaObjects()->j_invoke('equals', [$collection, $obj]))}),
fc3(contains_Z_MANY__Z_1__Function_1__Boolean_1_, {ctx,collection,obj,eqlf | $library->j_invoke('contains', [$collection, $obj, $eqlf], javaBoolean())}),

fc1(removeDuplicates_T_MANY__T_MANY_, {ctx,collection | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, j_null(), j_null()], $collection.type), | j_null($ctx.returnType->elementType()))}),
fc2(removeDuplicates_T_MANY__Function_1__T_MANY_, {ctx,collection,eqlf | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, j_null(), $eqlf ], $collection.type), | j_null($ctx.returnType->elementType()))}),
fc2(removeDuplicatesBy_T_MANY__Function_1__T_MANY_, {ctx,collection,keyf | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, $keyf, j_null()], $collection.type), | j_null($ctx.returnType->elementType()))}),
fc3(removeDuplicates_T_MANY__Function_$0_1$__Function_$0_1$__T_MANY_, {ctx,collection,keyf,eqlf | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, $keyf, $eqlf ], $collection.type), | j_null($ctx.returnType->elementType()))}),
fc2(at_T_MANY__Integer_1__T_1_, {ctx,collection,index | $library->j_invoke('at', [$collection, $index], if($collection.type->isJavaList(), |$collection.type->elementTypeOfJavaList(), |$collection.type))}),
fc2(indexOf_T_MANY__T_1__Integer_1_, {ctx,collection,obj | $library->j_invoke('indexOf', [$collection, $obj], if($collection.type->isJavaList(), |$collection.type->elementTypeOfJavaList(), |$collection.type))}),

fc3(fold_T_MANY__Function_1__V_m__V_m_, {ctx,collection,acc,identity | javaFold($ctx, $collection, $acc, $identity, $library) }),

fc1(init_T_MANY__T_MANY_, {ctx,collection | if($collection.type->isJavaList(), |$library->j_invoke('init', $collection, $collection.type), | j_null($ctx.returnType->elementType())) }),
fc1(tail_T_MANY__T_MANY_, {ctx,collection | $collection->j_streamOf()->js_skip(j_long(1))}),
fc1(last_T_MANY__T_$0_1$_, {ctx,collection | last($collection, $library)}),
fc1(reverse_T_m__T_m_, {ctx,collection | if($collection.type->isJavaList(), | $library->j_invoke('reverse', $collection, $collection.type), | $collection) }),

fc1(sort_T_m__T_m_, {ctx,collection | if($collection.type->isJavaList(), | $library->j_invoke('sort', $collection, $collection.type), | $collection) }),
fc2(sort_T_m__Function_$0_1$__T_m_, {ctx,collection,comp | if($collection.type->isJavaList(), | sortComp($ctx, $collection, $comp, $library), | $collection) }),
fc3(sort_T_m__Function_$0_1$__Function_$0_1$__T_m_, {ctx,collection,key,comp | if($collection.type->isJavaList(), | sortKeyComp($collection, $key, $comp, $library), | $collection) }),
fc3(fold_T_MANY__Function_1__V_m__V_m_, {ctx,collection,acc,identity | javaFold($ctx, $collection, $acc, $identity, $library) }),

fc2(drop_T_MANY__Integer_1__T_MANY_, {ctx,collection,limit | $ctx.param('collection')->j_streamOf()->js_skip(pinToZero($limit))}),
fc2(filter_T_MANY__Function_1__T_MANY_, {ctx,collection,func | $collection->j_streamOf()->js_filter($func)}),
fc2(map_T_MANY__Function_1__V_MANY_, {ctx,collection,func | $collection->j_streamOf()->js_map($func)}),
fc (map_T_m__Function_1__V_m_, fcAlias( map_T_MANY__Function_1__V_MANY_)),
fc (map_T_$0_1$__Function_1__V_$0_1$_, fcAlias( map_T_MANY__Function_1__V_MANY_)),
fc (head_T_MANY__T_$0_1$_, fcAlias( first_T_MANY__T_$0_1$_)),
fc1(init_T_MANY__T_MANY_, {ctx,collection | if($collection.type->isJavaList(), |$library->j_invoke('init', $collection, $collection.type), | j_null($ctx.returnType->elementType())) }),
fc1(last_T_MANY__T_$0_1$_, {ctx,collection | last($collection, $library)}),
fc3(slice_T_MANY__Integer_1__Integer_1__T_MANY_, {ctx,collection,limit1,limit2 | $collection->j_streamOf()->js_skip(pinToZero($limit1))->js_limit($limit2->j_minus(pinToZero($limit1)))}),
fc1(tail_T_MANY__T_MANY_, {ctx,collection | $collection->j_streamOf()->js_skip(j_long(1))}),
fc2(take_T_MANY__Integer_1__T_MANY_, {ctx,collection,limit | $collection->j_streamOf()->js_limit($limit)}),

fc2(contains_Any_MANY__Any_1__Boolean_1_, {ctx,collection,obj | if($collection.type->isJavaList(), |$collection->j_invoke('contains', $obj), |javaObjects()->j_invoke('equals', [$collection, $obj]))}),
fc3(contains_Z_MANY__Z_1__Function_1__Boolean_1_, {ctx,collection,obj,eqlf | $library->j_invoke('contains', [$collection, $obj, $eqlf], javaBoolean())}),
fc (add_T_MANY__T_1__T_$1_MANY$_, fcAlias( concatenate_T_MANY__T_MANY__T_MANY_)),
fc2(concatenate_T_MANY__T_MANY__T_MANY_, {ctx,collection1,collection2 | javaStream()->j_invoke('concat', [$collection1->j_streamOf(), $collection2->j_streamOf()], javaStream($ctx.returnType()->elementType()))}),

fc2(exists_T_MANY__Function_1__Boolean_1_, {ctx,collection,func | $collection->j_streamOf()->js_anyMatch($func)}),
fc2(forAll_T_MANY__Function_1__Boolean_1_, {ctx,collection,func | $collection->j_streamOf()->js_allMatch($func)}),
fc1(isEmpty_Any_MANY__Boolean_1_, {ctx,collection | if($collection.type->isJavaList(), |$collection->j_invoke('isEmpty', []), |$collection->j_cast($collection.type->toBoxed())->j_eq(j_null()))}),
fc1(isNotEmpty_Any_MANY__Boolean_1_, {ctx,collection | if($collection.type->isJavaList(), | j_not($collection->j_invoke('isEmpty', [])), | $collection->j_cast($collection.type->toBoxed())->j_ne(j_null()))}),
fc1(removeDuplicates_T_MANY__T_MANY_, {ctx,collection | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, j_null(), j_null()], $collection.type), | j_null($ctx.returnType->elementType()))}),
fc2(removeDuplicates_T_MANY__Function_1__T_MANY_, {ctx,collection,eqlf | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, j_null(), $eqlf ], $collection.type), | j_null($ctx.returnType->elementType()))}),

fc1(size_Any_MANY__Integer_1_, {ctx,collection | $collection->j_streamOf()->js_count()})
fc2(removeDuplicatesBy_T_MANY__Function_1__T_MANY_, {ctx,collection,keyf | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, $keyf, j_null()], $collection.type), | j_null($ctx.returnType->elementType()))}),
fc3(removeDuplicates_T_MANY__Function_$0_1$__Function_$0_1$__T_MANY_, {ctx,collection,keyf,eqlf | if($collection.type->isJavaList(), |$library->j_invoke('removeDuplicates', [$collection, $keyf, $eqlf ], $collection.type), | j_null($ctx.returnType->elementType()))})
]);

$conventions->registerLibrary($lib);
}

function meta::external::language::java::generation::essential::collection::first(collection:Code[1], library:meta::external::language::java::metamodel::Class[1]) : Code[1]
{
$library->j_invoke('first', $collection, if($collection.type->isJavaList(), |$collection.type->elementTypeOfJavaList(), |$collection.type))
}

function meta::external::language::java::generation::essential::collection::last(collection:Code[1], library:meta::external::language::java::metamodel::Class[1]) : Code[1]
{
if($collection.type->isJavaList(), | j_conditional($collection->j_invoke('isEmpty', []), j_null($collection.type->elementType()) ,$collection->j_invoke('get', $collection->j_invoke('size', [])->j_minus(j_int(1)))), | $collection )
Expand Down
Loading

0 comments on commit d061a3a

Please sign in to comment.