Skip to content

Commit 2e1c2c5

Browse files
Extend Metadata interface (#1215)
1 parent 5de1c92 commit 2e1c2c5

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

legend-pure-runtime/legend-pure-runtime-java-engine-compiled/src/main/java/org/finos/legend/pure/runtime/java/compiled/execution/CompiledProcessorSupport.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import org.finos.legend.pure.runtime.java.compiled.metadata.Metadata;
6161
import org.finos.legend.pure.runtime.java.compiled.metadata.MetadataAccessor;
6262
import org.finos.legend.pure.runtime.java.compiled.metadata.MetadataHolder;
63-
import org.finos.legend.pure.runtime.java.compiled.metadata.MetadataPelt;
6463

6564
import java.lang.reflect.Method;
6665

@@ -287,9 +286,9 @@ public CoreInstance newGenericType(SourceInformation sourceInformation, CoreInst
287286
@Override
288287
public CoreInstance package_getByUserPath(String path)
289288
{
290-
if (this.metadata instanceof MetadataPelt)
289+
if (this.metadata.supportsElementByPath())
291290
{
292-
return ((MetadataPelt) this.metadata).getElementByPath(path);
291+
return this.metadata.getElementByPath(path);
293292
}
294293

295294
// Check top level elements
@@ -370,9 +369,9 @@ public CoreInstance package_getByUserPath(String path)
370369
@Override
371370
public CoreInstance repository_getTopLevel(String name)
372371
{
373-
if ((this.metadata instanceof MetadataPelt) && GraphTools.isTopLevelName(name))
372+
if (this.metadata.supportsElementByPath() && GraphTools.isTopLevelName(name))
374373
{
375-
return ((MetadataPelt) this.metadata).getElementByPath(name);
374+
return this.metadata.getElementByPath(name);
376375
}
377376

378377
if (M3Paths.Root.equals(name))
@@ -505,8 +504,8 @@ public CoreInstance getClassifier(CoreInstance instance)
505504
{
506505
if (instance instanceof ValCoreInstance)
507506
{
508-
return (this.metadata instanceof MetadataPelt) ?
509-
((MetadataPelt) this.metadata).getElementByPath(((ValCoreInstance) instance).getType()) :
507+
return this.metadata.supportsElementByPath() ?
508+
this.metadata.getElementByPath(((ValCoreInstance) instance).getType()) :
510509
this.metadataAccessor.getPrimitiveType(((ValCoreInstance) instance).getType());
511510
}
512511

@@ -549,21 +548,17 @@ public boolean type_subTypeOf(CoreInstance type, CoreInstance possibleSuperType)
549548
@Override
550549
public CoreInstance type_BottomType()
551550
{
552-
if (this.metadata instanceof MetadataPelt)
553-
{
554-
return ((MetadataPelt) this.metadata).getElementByPath(M3Paths.Nil);
555-
}
556-
return this.metadataAccessor.getBottomType();
551+
return this.metadata.supportsElementByPath() ?
552+
this.metadata.getElementByPath(M3Paths.Nil) :
553+
this.metadataAccessor.getBottomType();
557554
}
558555

559556
@Override
560557
public CoreInstance type_TopType()
561558
{
562-
if (this.metadata instanceof MetadataPelt)
563-
{
564-
return ((MetadataPelt) this.metadata).getElementByPath(M3Paths.Any);
565-
}
566-
return this.metadataAccessor.getTopType();
559+
return this.metadata.supportsElementByPath() ?
560+
this.metadata.getElementByPath(M3Paths.Any) :
561+
this.metadataAccessor.getTopType();
567562
}
568563

569564
public Metadata getMetadata()

legend-pure-runtime/legend-pure-runtime-java-engine-compiled/src/main/java/org/finos/legend/pure/runtime/java/compiled/metadata/Metadata.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,59 @@ default RichIterable<CoreInstance> getClassifierInstances(String classifier)
3636
}
3737

3838
CoreInstance getEnum(String enumerationName, String enumName);
39+
40+
/**
41+
* <p>Return whether the metadata supports accessing an element by its package path.</p>
42+
*
43+
* <p>If true, then {@link #hasElement} and {@link #getElementByPath} can be used to access elements by their
44+
* package path. Otherwise, those methods will throw {@link UnsupportedOperationException}.</p>
45+
*
46+
* @return whether element by path is supported
47+
* @see #hasElement
48+
* @see #getElementByPath
49+
*/
50+
default boolean supportsElementByPath()
51+
{
52+
return false;
53+
}
54+
55+
/**
56+
* <p>Return whether the metadata has an element with the given package path.</p>
57+
*
58+
* <p>For a given path, {@link #getElementByPath} will return the element if and only if this method returns true.
59+
* Otherwise, it will return null.</p>
60+
*
61+
* <p>Use {@link #supportsElementByPath} to check if the method is supported on this metadata instance. If not,
62+
* an {@link UnsupportedOperationException} will be thrown.</p>
63+
*
64+
* @param path package path
65+
* @return whether there is an element with the given path
66+
* @throws UnsupportedOperationException if element by path access is not supported
67+
* @see #supportsElementByPath
68+
* @see #getElementByPath
69+
*/
70+
default boolean hasElement(String path)
71+
{
72+
return getElementByPath(path) != null;
73+
}
74+
75+
/**
76+
* <p>Get an element by its package path. Returns null if there is no such element</p>
77+
*
78+
* <p>This method will return the element if and only if {@link #hasElement} returns true. Otherwise, it will return
79+
* null.</p>
80+
*
81+
* <p>Use {@link #supportsElementByPath} to check if the method is supported on this metadata instance. If not,
82+
* an {@link UnsupportedOperationException} will be thrown.</p>
83+
*
84+
* @param path package path of the element
85+
* @return element with the given path, or null if there is no such element
86+
* @throws UnsupportedOperationException if element by path access is not supported
87+
* @see #supportsElementByPath
88+
* @see #hasElement
89+
*/
90+
default CoreInstance getElementByPath(String path)
91+
{
92+
throw new UnsupportedOperationException(getClass().getName() + " does not support element by path");
93+
}
3994
}

legend-pure-runtime/legend-pure-runtime-java-engine-compiled/src/main/java/org/finos/legend/pure/runtime/java/compiled/metadata/MetadataPelt.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,19 @@ public CoreInstance getInstance(String id)
141141
return this.instanceCache.getIfAbsentPutWithKey(id, this.refIdResolver::resolveReference);
142142
}
143143

144-
/**
145-
* Return whether the metadata has an element with the given package path.
146-
*
147-
* @param path package path
148-
* @return whether there is an element with the given path
149-
*/
144+
@Override
145+
public boolean supportsElementByPath()
146+
{
147+
return true;
148+
}
149+
150+
@Override
150151
public boolean hasElement(String path)
151152
{
152153
return this.elementLoader.elementExists(path);
153154
}
154155

155-
/**
156-
* Get an element by its package path. Returns null if there is no such element.
157-
*
158-
* @param path package path of the element
159-
* @return element with the given path, or null if there is no such element
160-
*/
156+
@Override
161157
public CoreInstance getElementByPath(String path)
162158
{
163159
return this.elementLoader.loadElement(path);

0 commit comments

Comments
 (0)