use of org.finos.legend.pure.m4.coreinstance.indexing.IndexSpecification in project legend-pure by finos.
the class DatabaseProcessor method breadthFirstFilterSearch.
private static CoreInstance breadthFirstFilterSearch(Queue<Database> databases, String filterName, ProcessorSupport processorSupport) {
IndexSpecification<String> indexSpec = IndexSpecifications.getPropertyValueNameIndexSpec(M3Properties.name);
MutableSet<Database> visitedDBs = Sets.mutable.empty();
while (!databases.isEmpty()) {
Database database = databases.remove();
if (visitedDBs.add(database)) {
CoreInstance filter = database.getValueInValueForMetaPropertyToManyByIDIndex(M2RelationalProperties.filters, indexSpec, filterName);
if (filter != null) {
return filter;
}
ListIterable<? extends Database> includes = (ListIterable<? extends Database>) ImportStub.withImportStubByPasses(database._includesCoreInstance().toList(), processorSupport);
Iterate.addAllIterable(includes, databases);
}
}
return null;
}
use of org.finos.legend.pure.m4.coreinstance.indexing.IndexSpecification in project legend-pure by finos.
the class M3ToJavaGenerator method createClass.
private String createClass(final CoreInstance instance, String javaPackage, MutableSet<CoreInstance> properties, MutableSet<CoreInstance> propertiesFromAssociations, MutableSet<CoreInstance> qualifiedProperties, final Imports imports, final MutableMap<String, CoreInstance> propertyOwners) {
imports.addImports("org.finos.legend.pure.m3.coreinstance.helper.PrimitiveHelper", "org.finos.legend.pure.m3.coreinstance.helper.*");
final String interfaceName = getInterfaceName(instance);
final String className = getClassName(instance);
String typeParams = getTypeParams(instance, false);
String typeParamsWithExtendsCoreInstance = getTypeParams(instance, true);
final CoreInstance classGenericType = getClassGenericType(instance);
ListIterable<String> paths = getUserObjectPathForPackageableElement(instance, true);
String pathString = paths.makeString("\"", "\",\"", "\"");
String systemPathForPackageableElement = paths.makeString("::");
PartitionIterable<CoreInstance> partition = properties.partition(M3ToJavaGenerator::isToOne);
RichIterable<CoreInstance> toOneProperties = partition.getSelected();
RichIterable<CoreInstance> toManyProperties = partition.getRejected();
RichIterable<CoreInstance> mandatoryToOneProps = toOneProperties.select(M3ToJavaGenerator::isMandatoryProperty);
RichIterable<Pair<String, String>> typesForMandatoryProps = buildMandatoryProperties(classGenericType, mandatoryToOneProps, imports).toSortedSetBy(Pair::getOne);
MutableList<String> mandatoryTypes = Lists.mutable.of();
MutableList<String> mandatoryProps = Lists.mutable.of();
for (Pair<String, String> pair : typesForMandatoryProps) {
mandatoryTypes.add(pair.getTwo() + " " + pair.getOne());
mandatoryProps.add(pair.getOne());
}
String mandatoryTypeString = mandatoryTypes.isEmpty() ? "" : mandatoryTypes.makeString(", ", ", ", "");
String mandatoryPropertyNames = mandatoryProps.isEmpty() ? "" : mandatoryProps.makeString(", ", ", ", "");
final String maybeFullyQualifiedName = imports.shouldFullyQualify(javaPackage + "." + interfaceName) ? javaPackage + "." + interfaceName : interfaceName;
String interfaceNamePlusTypeParams = maybeFullyQualifiedName + (typeParamsWithExtendsCoreInstance.isEmpty() ? "" : typeParamsWithExtendsCoreInstance);
boolean isEnum = "Root::meta::pure::metamodel::type::Enum".equals(systemPathForPackageableElement);
String value = "\n" + "package " + javaPackage + ";\n" + "\n" + "import org.eclipse.collections.api.RichIterable;\n" + "import org.eclipse.collections.api.block.predicate.Predicate;\n" + "import org.eclipse.collections.api.list.ListIterable;\n" + "import org.eclipse.collections.api.list.MutableList;\n" + "import org.eclipse.collections.api.map.MapIterable;\n" + "import org.eclipse.collections.api.set.SetIterable;\n" + "import org.eclipse.collections.impl.block.factory.Functions;\n" + "import org.eclipse.collections.impl.factory.Lists;\n" + "import org.eclipse.collections.impl.factory.Sets;\n" + "import org.eclipse.collections.impl.map.mutable.UnifiedMap;\n" + "import org.finos.legend.pure.m3.coreinstance.BaseCoreInstance;\n" + "import org.finos.legend.pure.m3.coreinstance.BaseM3CoreInstanceFactory;\n" + "import org.finos.legend.pure.m4.exception.PureCompilationException;\n" + imports.toImportString() + "\n" + "import org.finos.legend.pure.m4.coreinstance.compileState.CompileState;\n" + "import org.finos.legend.pure.m4.coreinstance.compileState.CompileStateSet;\n" + "import org.finos.legend.pure.m4.coreinstance.CoreInstance;\n" + "import org.finos.legend.pure.m4.coreinstance.factory.CoreInstanceFactory;\n" + "import org.finos.legend.pure.m4.ModelRepository;\n" + "import org.finos.legend.pure.m4.transaction.ModelRepositoryTransaction;\n" + "import org.finos.legend.pure.m4.coreinstance.AbstractCoreInstanceMutableState;\n" + "import org.finos.legend.pure.m4.coreinstance.indexing.IndexSpecification;\n" + "import org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance;\n" + "import org.finos.legend.pure.m4.coreinstance.simple.Values;\n" + "import org.finos.legend.pure.m4.coreinstance.simple.OneValueException;\n" + getPrimitiveImports() + "\n" + "\n" + "public class " + className + " extends BaseCoreInstance implements " + maybeFullyQualifiedName + typeParamsWithExtendsCoreInstance + (isEnum ? " ,Comparable<" + javaPackage + "." + interfaceName + ">" : "") + ", CoreInstanceFactory\n" + "{\n" + createClassFactory(className, getUserObjectPathForPackageableElement(instance, false).makeString("::")) + "\n" + " // TODO: These should be static\n" + properties.collect(property -> {
String propertyName = property.getName();
CoreInstance propertyOwner = propertyOwners.get(propertyName);
ListIterable<String> propertyOwnerPath = getUserObjectPathForPackageableElement(propertyOwner, true);
return " private CoreInstance " + propertyName + "Key;\n" + (propertyOwner == instance || "Association".equals(propertyOwner.getClassifier().getName()) ? " public static final ListIterable<String> " + createPropertyKeyName(propertyName) + " = Lists.immutable.of(" + propertyOwnerPath.makeString("\"", "\",\"children\",\"", "\"") + ",\"properties\",\"" + propertyName + "\");\n" : "");
}).makeString("") + "\n" + "\n" + " private _State state;\n" + " private static final SetIterable<String> keys = Sets.immutable.with(" + properties.collect(CoreInstance::getName, FastList.newList()).sortThis().makeString("\"", "\",\"", "\"") + ");\n" + "\n" + createClassConstructors(className) + "\n" + " public void commit(ModelRepositoryTransaction transaction)\n" + " {\n" + " this.state = (_State)transaction.getState(this);\n" + " }\n" + "\n" + "\n" + " public void rollback(ModelRepositoryTransaction transaction)\n" + " {\n" + " }\n" + "\n" + " @Override\n" + " public void addCompileState(CompileState state)\n" + " {\n" + " this.prepareForWrite();\n" + " this.getState().addCompileState(state);\n" + " }\n" + "\n" + " @Override\n" + " public void removeCompileState(CompileState state)\n" + " {\n" + " this.prepareForWrite();\n" + " this.getState().removeCompileState(state);\n" + " }\n" + "\n" + " @Override\n" + " public boolean hasCompileState(CompileState state)\n" + " {\n" + " return this.getState().hasCompileState(state);\n" + " }\n" + "\n" + " @Override\n" + " public CompileStateSet getCompileStates()\n" + " {\n" + " return this.getState().getCompileStates();\n" + " }\n" + "\n" + " @Override\n" + " public void setCompileStatesFrom(CompileStateSet states)\n" + " {\n" + " this.getState().setCompileStatesFrom(states);\n" + " }\n" + "\n" + " public void prepareForWrite()\n" + " {\n" + " ModelRepositoryTransaction transaction = this.getRepository().getTransaction();\n" + " if (transaction != null && !transaction.isRegistered(this))\n" + " {\n" + " transaction.registerModified(this, this.state.copy());\n" + " }\n" + " }\n" + "\n" + " private _State getState()\n" + " {\n" + " ModelRepositoryTransaction transaction = this.getRepository().getTransaction();\n" + " if ((transaction != null) && transaction.isOpen())\n" + " {\n" + " _State transactionState = (_State)transaction.getState(this);\n" + " if (transactionState != null)\n" + " {\n" + " return transactionState;\n" + " }\n" + " }\n" + " return this.state;\n" + " }\n" + "\n" + " @Override\n" + " public CoreInstance getValueForMetaPropertyToOne(String keyName)\n" + " {\n" + " switch (keyName)\n" + " {\n" + toOneProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ";\n" + " }\n").makeString("") + toManyProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return getOneValueFromToManyPropertyValues(keyName, this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ");\n" + " }\n").makeString("") + " default:\n" + " {\n" + " return null;\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public ListIterable<? extends CoreInstance> getValueForMetaPropertyToMany(String keyName)\n" + " {\n" + " switch (keyName)\n" + " {\n" + toManyProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return getValuesFromToManyPropertyValues(this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ");\n" + " }\n").makeString("") + toOneProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return getValuesFromToOnePropertyValue(this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ");\n" + " }\n").makeString("") + " default:\n" + " {\n" + " return Lists.immutable.empty();\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public <K> CoreInstance getValueInValueForMetaPropertyToManyByIDIndex(String keyName, IndexSpecification<K> indexSpec, K keyInIndex)\n" + " {\n" + " switch (keyName)\n" + " {\n" + toManyProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return getValueByIDIndexFromToManyPropertyValues(keyName, this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ", indexSpec, keyInIndex);\n" + " }\n").makeString("") + toOneProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return getValueByIDIndexFromToOnePropertyValue(this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ", indexSpec, keyInIndex);\n" + " }\n").makeString("") + " default:\n" + " {\n" + " return null;\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public <K> ListIterable<? extends CoreInstance> getValueInValueForMetaPropertyToManyByIndex(String keyName, IndexSpecification<K> indexSpec, K keyInIndex)\n" + " {\n" + " switch (keyName)\n" + " {\n" + toManyProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return getValuesByIndexFromToManyPropertyValues(this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ", indexSpec, keyInIndex);\n" + " }\n").makeString("") + toOneProperties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return getValuesByIndexFromToOnePropertyValue(this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ", indexSpec, keyInIndex);\n" + " }\n").makeString("") + " default:\n" + " {\n" + " return Lists.immutable.empty();\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public boolean isValueDefinedForKey(String keyName)\n" + " {\n" + " switch (keyName)\n" + " {\n" + properties.collect(property -> " case \"" + property.getName() + "\":\n" + " {\n" + " return this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + " != null;\n" + " }\n").makeString("") + " default:\n" + " {\n" + " return false;\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public void modifyValueForToManyMetaProperty(String key, int offset, CoreInstance value)\n" + " {\n" + " this.prepareForWrite();\n" + " _State state = this.getState();\n" + " switch (key)\n" + " {\n" + toManyProperties.collect(property -> {
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " " + getPropertyTypeInternal(property, propertyReturnGenericType, imports, false, false, isPlatformClass(property)) + " values = state." + propertyName + ";\n" + " if (values == null)\n" + " {\n" + " throw new IllegalArgumentException(\"Cannot modify value at offset \" + offset + \" for property '\" + key + \"'\");\n" + " }\n" + " values.setValue(offset, " + (isAnyOrNilTypeProperty(propertyReturnGenericType) ? "value" : typeConversionInSetter(property, propertyReturnGenericType, imports, "value", true, true, true)) + ");\n" + " break;\n" + " }\n";
}).makeString("") + toOneProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " if (offset != 0)\n" + " {\n" + " throw new IllegalArgumentException(\"Cannot modify value at offset \" + offset + \" for to-one property '\" + key + \"'\");\n" + " }\n" + " synchronized (state)\n" + " {\n" + " state." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + " = " + typeConversionInSetter(property, propertyReturnGenericType, imports, "value", false, true, true) + ";\n" + " }\n" + " break;\n" + " }\n";
}).makeString("") + " default:\n" + " {\n" + " throw new IllegalArgumentException(\"Unknown property '\" + key + \"'\");\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public void removeValueForMetaPropertyToMany(String keyName, CoreInstance valueToRemove)\n" + " {\n" + " this.prepareForWrite();\n" + " _State state = this.getState();\n" + " switch (keyName)\n" + " {\n" + toManyProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " " + getPropertyTypeInternal(property, propertyReturnGenericType, imports, false, false, isPlatformClass(property)) + " values = state." + propertyName + ";\n" + " if (values != null)\n" + " {\n" + " values.removeValue(" + typeConversionInSetter(property, propertyReturnGenericType, imports, "valueToRemove", true, true, true) + ");\n" + " }\n" + " break;\n" + " }\n";
}).makeString("") + toOneProperties.collect(property -> {
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " synchronized (state)\n" + " {\n" + " CoreInstance current = state." + propertyName + ";\n" + " if ((current != null) && current.equals(valueToRemove))\n" + " {\n" + " state." + propertyName + " = null;\n" + " }\n" + " }\n" + " break;\n" + " }\n";
}).makeString("") + " default:\n" + " {\n" + " throw new IllegalArgumentException(\"Unknown property '\" + keyName + \"'\");\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public void setKeyValues(ListIterable<String> key, ListIterable<? extends CoreInstance> newValues)\n" + " {\n" + " String keyName = key.getLast();\n" + " if (newValues == null)\n" + " {\n" + " removeProperty(keyName);\n" + " return;\n" + " }\n" + " this.prepareForWrite();\n" + " _State state = this.getState();\n" + " switch (keyName)\n" + " {\n" + toManyProperties.collect(property -> {
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
String expression = "newValues";
CoreInstance propertyGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
if (!isAnyOrNilTypeProperty(propertyGenericType) && !isStubType(property, propertyGenericType)) {
expression = "(" + expression + " == null) ? null : " + typeConversionInSetter(property, propertyGenericType, imports, expression, true, false, true);
}
return " case \"" + property.getName() + "\":\n" + " {\n" + " state.init_" + propertyName + "().setValues(" + expression + ");\n" + " break;\n" + " }\n";
}).makeString("") + toOneProperties.collect(property -> {
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " if (newValues.size() > 1)\n" + " {\n" + " throw new IllegalArgumentException(\"Cannot set \" + newValues.size() + \" values for to-one property '\" + keyName + \"'\");\n" + " }\n" + " synchronized (state)\n" + " {\n" + " state." + propertyName + " = " + typeConversionInSetter(property, propertyReturnGenericType, imports, "newValues.getFirst()", false, true, true) + ";\n" + " }\n" + " break;\n" + " }\n";
}).makeString("") + " default:\n" + " {\n" + " throw new IllegalArgumentException(\"Unknown property '\" + keyName + \"'\");\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public void addKeyValue(ListIterable<String> key, CoreInstance value)\n" + " {\n" + " String keyName = key.getLast();\n" + " this.prepareForWrite();\n" + " _State state = this.getState();\n" + " switch (keyName)\n" + " {\n" + toManyProperties.collect(property -> {
CoreInstance propertyGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " state.init_" + propertyName + "().addValue(" + typeConversionInSetter(property, propertyGenericType, imports, "value", true, true, true) + ");\n" + " break;\n" + " }\n";
}).makeString("") + toOneProperties.collect(property -> {
CoreInstance propertyGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " synchronized (state)\n" + " {\n" + " if (state." + propertyName + " != null)\n" + " {\n" + " throw new IllegalArgumentException(\"Cannot add value for to-one property '\" + keyName + \"': value already present\");\n" + " }\n" + " state." + propertyName + " = " + typeConversionInSetter(property, propertyGenericType, imports, "value", false, true, true) + ";\n" + " }\n" + " break;\n" + " }\n";
}).makeString("") + " default:\n" + " {\n" + " throw new IllegalArgumentException(\"Unknown property '\" + keyName + \"'\");\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public void removeProperty(String keyName)\n" + " {\n" + " this.prepareForWrite();\n" + " _State state = this.getState();\n" + " synchronized (state)\n" + " {\n" + " switch (keyName)\n" + " {\n" + properties.collect(property -> {
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " case \"" + property.getName() + "\":\n" + " {\n" + " state." + propertyName + " = null;\n" + " break;\n" + " }\n";
}).makeString("") + " default:\n" + " {\n" + " throw new IllegalArgumentException(\"Unknown property '\" + keyName + \"'\");\n" + " }\n" + " }\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public ListIterable<String> getRealKeyByName(String name)\n" + " {\n" + " ListIterable<String> realKeys = null;\n" + (properties.isEmpty() ? "" : " switch (name)\n" + " {\n" + properties.collect(property -> {
String propertyName = property.getName();
CoreInstance propertyOwner = propertyOwners.get(propertyName);
return " case \"" + propertyName + "\":\n" + " realKeys = " + createPropertyKeyNameReference(propertyName, propertyOwner, instance) + ";\n" + " break;\n";
}).makeString("") + "\n" + " default:\n" + " throw new RuntimeException(\"Unsupported key: \" + name);\n" + " }\n") + " return realKeys;\n" + " }\n" + "\n" + " @Override\n" + " public CoreInstance getKeyByName(String name)\n" + " {\n" + " CoreInstance key = null;\n" + "\n" + (properties.isEmpty() ? "" : " switch (name)\n" + " {\n" + properties.collect(property -> {
String propertyName = property.getName();
return " case \"" + propertyName + "\":\n" + " if (this." + propertyName + "Key == null)\n" + " {\n" + " this." + propertyName + "Key = this.getRepository().resolve(" + createPropertyKeyNameReference(propertyName, propertyOwners.get(propertyName), instance) + ");\n" + " }\n" + " key = this." + propertyName + "Key;\n" + " break;\n";
}).makeString("") + " default:\n" + " throw new RuntimeException(\"Unsupported key: \" + name);\n" + " }\n" + "\n") + " return key;\n" + " }\n" + "\n" + " @Override\n" + " public RichIterable<String> getKeys()\n" + " {\n" + " MutableList<String> result = Lists.mutable.of();\n" + " for (String key: this.keys)\n" + " {\n" + " if (isValueDefinedForKey(key))\n" + " {\n" + " result.add(key);\n" + " }\n" + " }\n" + " return result;\n" + " }\n" + "\n" + properties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createClassPropertyGetter(interfaceName, property, propertyReturnGenericType, imports);
}).makeString("") + "\n" + properties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createClassPropertySetter(interfaceName, property, propertyReturnGenericType, imports, maybeFullyQualifiedName, "");
}).makeString("") + "\n" + toManyProperties.collect(property -> {
CoreInstance propertyGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createClassSetPropertyValueAt(property, propertyGenericType, imports);
}).makeString("") + propertiesFromAssociations.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createPropertyReverse(property, propertyReturnGenericType, imports, false, false) + createPropertyReverse(property, propertyReturnGenericType, imports, true, false);
}).makeString("") + "\n" + qualifiedProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createClassQualifiedPropertyGetter(property, propertyReturnGenericType, imports);
}).makeString("") + " private static class _State extends AbstractCoreInstanceMutableState\n" + " {\n" + toOneProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String type = getPropertyTypeInternal(property, propertyReturnGenericType, imports, isToOne(property), false, isPlatformClass(property));
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " private " + type + " " + propertyName + ";\n";
}).makeString("") + toManyProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String type = getPropertyTypeInternal(property, propertyReturnGenericType, imports, isToOne(property), false, isPlatformClass(property));
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " private " + type + " " + propertyName + ";\n";
}).makeString("") + toManyProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String propertyTypeInternal = getPropertyTypeInternal(property, propertyReturnGenericType, imports, false, false, isPlatformClass(property));
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " private " + propertyTypeInternal + " init_" + propertyName + "()\n" + " {\n" + " " + propertyTypeInternal + " values = this." + propertyName + ";\n" + " if (values == null)\n" + " {\n" + " synchronized (this)\n" + " {\n" + " if (this." + propertyName + " == null)\n" + " {\n" + " this." + propertyName + " = newToManyPropertyValues();\n" + " }\n" + " values = this." + propertyName + ";\n" + " }\n" + " }\n" + " return values;\n" + " }\n";
}).makeString("\n", "\n", "\n") + " private _State copy()\n" + " {\n" + " synchronized (this)\n" + " {\n" + " final _State copy = new _State();\n" + toOneProperties.collect(property -> {
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " if (this." + propertyName + " != null)\n" + " {\n" + " copy." + propertyName + " = this." + propertyName + ";\n" + " }\n";
}).makeString("") + toManyProperties.collect(property -> {
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
return " if (this." + propertyName + " != null)\n" + " {\n" + " copy." + propertyName + " = this." + propertyName + ".copy();\n" + " }\n";
}).makeString("") + "\n" + " copy.setCompileStateBitSet(getCompileStateBitSet());\n" + " return copy;\n" + " }\n" + " }\n" + "\n" + " }\n" + " public static " + className + " createPersistent(ModelRepository repository, String name, org.finos.legend.pure.m4.coreinstance.SourceInformation sourceInformation" + mandatoryTypeString + ")\n" + " {\n" + " CoreInstance classifier = getUserPath(Lists.immutable.with(" + pathString + "), repository);\n" + " " + className + " instance = name == null ? (" + className + ")repository.newAnonymousCoreInstance(sourceInformation, classifier, true, FACTORY)\n" + " : (" + className + ")repository.newCoreInstance(name, classifier, sourceInformation, FACTORY);\n" + (mandatoryToOneProps.isEmpty() ? "" : " instance.prepareForWrite();\n" + " _State state = instance.getState();\n") + mandatoryToOneProps.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String propertyName = getPropertyNameAsValidJavaIdentifierSwitchName(property);
if (isPrimitiveTypeProperty(propertyReturnGenericType)) {
String type = Objects.requireNonNull(getTypeFromGenericType(propertyReturnGenericType)).getName();
return " state." + propertyName + " = " + propertyName + " == null ? null : repository.new" + type + "CoreInstance" + (ModelRepository.STRING_TYPE_NAME.equals(type) ? "_cached(" : "(") + propertyName + ");\n";
} else {
return " state." + propertyName + " = " + propertyName + ";\n";
}
}).makeString("") + " return instance;\n" + " }\n" + " public static " + className + " createPersistent(ModelRepository repository, String name" + mandatoryTypeString + ")\n" + " {\n" + " return createPersistent(repository, name, null" + mandatoryPropertyNames + ");\n" + " }\n" + " public static " + className + " createPersistent(ModelRepository repository, org.finos.legend.pure.m4.coreinstance.SourceInformation sourceInformation" + mandatoryTypeString + ")\n" + " {\n" + " return createPersistent(repository, null, sourceInformation" + mandatoryPropertyNames + ");\n" + " }\n" + " public static " + className + " createPersistent(ModelRepository repository" + mandatoryTypeString + ")\n" + " {\n" + " return createPersistent(repository, null, null" + mandatoryPropertyNames + ");\n" + " }\n" + " private static CoreInstance getUserPath(ListIterable<String> path, ModelRepository repository)\n" + " {\n" + " // TODO - this is inefficient do it once, should we use Context?\n" + " if (path.isEmpty())\n" + " {\n" + " return null;\n" + " }\n" + "\n" + " CoreInstance child = repository.getTopLevel(path.get(0));\n" + " for (String childName : path.asLazy().drop(1))\n" + " {\n" + " child = ((PackageInstance)child).getValueInValueForMetaPropertyToManyByIndex(\"children\", org.finos.legend.pure.m4.coreinstance.indexing.IndexSpecifications.getCoreInstanceNameIndexSpec(), childName).getFirst();\n" + " }\n" + " return child;\n" + " }\n" + "\n" + " @Override\n" + " public boolean supports(CoreInstance classifier)\n" + " {\n" + " return FACTORY.supports(classifier);\n" + " }\n" + "\n" + " @Override\n" + " public CoreInstance createCoreInstance(String name, int internalSyntheticId, org.finos.legend.pure.m4.coreinstance.SourceInformation sourceInformation, CoreInstance classifier, ModelRepository repository, boolean persistent)\n" + " {\n" + " return FACTORY.createCoreInstance(name, internalSyntheticId, sourceInformation, classifier, repository, persistent);\n" + " }\n" + "\n" + createClassCopyMethod(className, interfaceNamePlusTypeParams) + createClassPackageableElement(systemPathForPackageableElement) + (isEnum ? enumToStringAndCompareOverrides(javaPackage + "." + interfaceName) : "") + "}\n";
return value;
}
Aggregations