Search in sources :

Example 1 with BaseCoreInstance

use of org.finos.legend.pure.m3.coreinstance.BaseCoreInstance in project legend-pure by finos.

the class CompiledSupport method getErrorMessageForMatchFunctionBasedOnObjectType.

private static String getErrorMessageForMatchFunctionBasedOnObjectType(Object obj) {
    StringBuilder errorMessage = new StringBuilder();
    if (isTypeOfEnum(obj)) {
        Enum enumVal = (Enum) obj;
        errorMessage.append(enumVal._name()).append(" instanceOf ").append(getEnumClassifierName(enumVal));
        return errorMessage.toString();
    }
    if (obj instanceof BaseCoreInstance) {
        return obj.toString();
    }
    if (isPureGeneratedClass(obj)) {
        String tempTypeName = getPureGeneratedClassName(obj);
        errorMessage.append(tempTypeName).append("Object instanceOf ").append(tempTypeName);
        return errorMessage.toString();
    }
    String primitiveJavaToPureType = JavaPurePrimitiveTypeMapping.getPureM3TypeFromJavaPrimitivesAndDates(obj);
    if (primitiveJavaToPureType != null) {
        errorMessage.append(obj.toString()).append(" instanceOf ").append(primitiveJavaToPureType);
        return errorMessage.toString();
    }
    errorMessage.append(obj.toString()).append(" instanceOf ").append(obj.getClass());
    return errorMessage.toString();
}
Also used : Enum(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum) BaseCoreInstance(org.finos.legend.pure.m3.coreinstance.BaseCoreInstance)

Example 2 with BaseCoreInstance

use of org.finos.legend.pure.m3.coreinstance.BaseCoreInstance in project legend-pure by finos.

the class CompiledProcessorSupport method getClassifier.

@Override
public CoreInstance getClassifier(CoreInstance instance) {
    if (instance instanceof ValCoreInstance) {
        return this.metadataAccessor.getPrimitiveType(((ValCoreInstance) instance).getType());
    }
    // todo: clean this up, seem to have interpreted style core instances in compiled
    if (instance instanceof PrimitiveCoreInstance || instance instanceof BaseCoreInstance) {
        return instance.getClassifier();
    }
    if (instance instanceof Any) {
        Any any = (Any) instance;
        GenericType genericType = any._classifierGenericType();
        if (genericType != null) {
            org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type type = genericType._rawType();
            if (type != null) {
                return type;
            }
        }
        return CompiledSupport.getType(any, this.metadataAccessor);
    }
    throw new PureExecutionException("ERROR unhandled type for value: " + instance + " (instance of " + instance.getClass() + ")");
}
Also used : PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) ValCoreInstance(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.coreinstance.ValCoreInstance) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) BaseCoreInstance(org.finos.legend.pure.m3.coreinstance.BaseCoreInstance) Any(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Any)

Example 3 with BaseCoreInstance

use of org.finos.legend.pure.m3.coreinstance.BaseCoreInstance 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;
}
Also used : ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) Lists(org.eclipse.collections.api.factory.Lists) SetIterable(org.eclipse.collections.api.set.SetIterable) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) Maps(org.eclipse.collections.api.factory.Maps) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) Tuples(org.eclipse.collections.impl.tuple.Tuples) Pair(org.eclipse.collections.api.tuple.Pair) StringIterate(org.eclipse.collections.impl.utility.StringIterate) Path(java.nio.file.Path) Sets(org.eclipse.collections.api.factory.Sets) JavaTools(org.finos.legend.pure.m3.tools.JavaTools) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) ModelRepository(org.finos.legend.pure.m4.ModelRepository) Files(java.nio.file.Files) Collection(java.util.Collection) IOException(java.io.IOException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Paths(java.nio.file.Paths) ListIterable(org.eclipse.collections.api.list.ListIterable) MapIterable(org.eclipse.collections.api.map.MapIterable) PartitionIterable(org.eclipse.collections.api.partition.PartitionIterable) ListIterable(org.eclipse.collections.api.list.ListIterable) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Pair(org.eclipse.collections.api.tuple.Pair)

Aggregations

BaseCoreInstance (org.finos.legend.pure.m3.coreinstance.BaseCoreInstance)2 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Objects (java.util.Objects)1 RichIterable (org.eclipse.collections.api.RichIterable)1 Lists (org.eclipse.collections.api.factory.Lists)1 Maps (org.eclipse.collections.api.factory.Maps)1 Sets (org.eclipse.collections.api.factory.Sets)1 ListIterable (org.eclipse.collections.api.list.ListIterable)1 MutableList (org.eclipse.collections.api.list.MutableList)1 MapIterable (org.eclipse.collections.api.map.MapIterable)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1 PartitionIterable (org.eclipse.collections.api.partition.PartitionIterable)1 MutableSet (org.eclipse.collections.api.set.MutableSet)1 SetIterable (org.eclipse.collections.api.set.SetIterable)1