Skip to content

Commit

Permalink
Merge pull request #4 from Steanky/patch
Browse files Browse the repository at this point in the history
0.6.0
  • Loading branch information
Steanky authored Sep 5, 2022
2 parents f16d834 + 7e0b7b0 commit d44acd3
Show file tree
Hide file tree
Showing 24 changed files with 624 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "com.github.steanky"
version = "0.5.0"
version = "0.6.0"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.steanky.element.core.context.ElementContext;
import com.github.steanky.element.core.dependency.DependencyProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Creates an element from some data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.steanky.element.core;

import com.github.steanky.element.core.context.ContextManager;
import com.github.steanky.ethylene.core.processor.ConfigProcessor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.github.steanky.element.core.context.ContextManager;

import java.util.Objects;

Expand All @@ -20,24 +20,6 @@ public interface ElementInspector {
*/
@NotNull Information inspect(final @NotNull Class<?> elementClass);

/**
* Represents information about an element class.
*/
record Information(@Nullable ConfigProcessor<?> processor, @NotNull ElementFactory<?, ?> factory, @NotNull CachePreference cachePreference) {
/**
* Represents information extracted from an Element Model-compliant class.
*
* @param processor the processor used to process the data associated with the class, which may be null if there
* is no data required to construct the class
* @param factory the factory used to create instances of the class, which may never be null
* @param cachePreference how this element class prefers to be cached when it is instantiated
*/
public Information {
Objects.requireNonNull(factory);
Objects.requireNonNull(cachePreference);
}
}

/**
* Describes how element objects prefer to be cached.
*/
Expand All @@ -58,4 +40,23 @@ enum CachePreference {
*/
NO_CACHE
}

/**
* Represents information about an element class.
*/
record Information(@Nullable ConfigProcessor<?> processor, @NotNull ElementFactory<?, ?> factory,
@NotNull CachePreference cachePreference) {
/**
* Represents information extracted from an Element Model-compliant class.
*
* @param processor the processor used to process the data associated with the class, which may be null if
* there is no data required to construct the class
* @param factory the factory used to create instances of the class, which may never be null
* @param cachePreference how this element class prefers to be cached when it is instantiated
*/
public Information {
Objects.requireNonNull(factory);
Objects.requireNonNull(cachePreference);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public @interface Cache {
/**
* Whether this element object should always be cached. Defaults to true.
*
* @return true if this element should be cached, false otherwise
*/
boolean value() default true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* If the element object referred to by this path should be cached or not. If true, the element object for this path
* will only be created once, and the same instance will be shared across all cache-enabled dependencies. Defaults
* to {@code true}.
*
* @return true if this element object should be cached, false otherwise
*/
boolean cache() default true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void registerElementClass(final @NotNull Class<?> elementClass) {

//don't bother to register if we are cache-unspecified
if (preference != ElementInspector.CachePreference.UNSPECIFIED) {
elementContextSource.cacheRegistry().register(elementKey,
preference == ElementInspector.CachePreference.CACHE);
elementContextSource.cacheRegistry()
.register(elementKey, preference == ElementInspector.CachePreference.CACHE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
* Basic implementation of {@link ElementContext}.
*/
public class BasicElementContext implements ElementContext {
private record DataInfo(Object data, Key type) {}

private final Registry<ConfigProcessor<?>> processorRegistry;
private final Registry<ElementFactory<?, ?>> factoryRegistry;
private final Registry<Boolean> cacheRegistry;
private final PathSplitter pathSplitter;
private final DataLocator dataLocator;
private final KeyExtractor typeKeyExtractor;
private final ConfigNode rootNode;

private final Map<String, DataInfo> dataObjects;
private final Map<String, Object> elementObjects;

/**
* Creates a new instance of this class.
*
Expand All @@ -43,16 +39,16 @@ private record DataInfo(Object data, Key type) {}
* @param factoryRegistry the Registry used to hold references to {@link ElementFactory} instances needed to
* construct element objects
* @param cacheRegistry the Registry used to determine if element types request caching or not
* @param pathSplitter the {@link PathSplitter} used to split path keys
* @param pathSplitter the {@link PathSplitter} used to split path keys
* @param dataLocator the {@link DataLocator} implementation used to locate data objects from identifiers
* @param typeKeyExtractor the {@link KeyExtractor} implementation used to extract type keys from nodes
* @param rootNode the {@link ConfigNode} used as the root (may contain additional element data)
*/
public BasicElementContext(final @NotNull Registry<ConfigProcessor<?>> processorRegistry,
final @NotNull Registry<ElementFactory<?, ?>> factoryRegistry,
final @NotNull Registry<Boolean> cacheRegistry,
final @NotNull PathSplitter pathSplitter, final @NotNull DataLocator dataLocator,
final @NotNull KeyExtractor typeKeyExtractor, final @NotNull ConfigNode rootNode) {
final @NotNull Registry<Boolean> cacheRegistry, final @NotNull PathSplitter pathSplitter,
final @NotNull DataLocator dataLocator, final @NotNull KeyExtractor typeKeyExtractor,
final @NotNull ConfigNode rootNode) {
this.processorRegistry = Objects.requireNonNull(processorRegistry);
this.factoryRegistry = Objects.requireNonNull(factoryRegistry);
this.cacheRegistry = Objects.requireNonNull(cacheRegistry);
Expand All @@ -65,30 +61,19 @@ public BasicElementContext(final @NotNull Registry<ConfigProcessor<?>> processor
this.elementObjects = new HashMap<>(4);
}

@Override
public <TElement> @NotNull TElement provide(@Nullable String path, @NotNull DependencyProvider dependencyProvider) {
return provideInternal(path, dependencyProvider, false);
}

@Override
public <TElement> @NotNull TElement provideAndCache(final @Nullable String path,
final @NotNull DependencyProvider dependencyProvider) {
return provideInternal(path, dependencyProvider, true);
}

@SuppressWarnings("unchecked")
private <TElement> @NotNull TElement provideInternal(final @Nullable String path,
final @NotNull DependencyProvider dependencyProvider, final boolean provideCache) {
@Override
public <TElement> @NotNull TElement provide(@Nullable String path, @NotNull DependencyProvider dependencyProvider,
final boolean cache) {
final boolean cacheElement;

final ConfigNode dataNode = dataLocator.locate(rootNode, path);
final Key objectType = typeKeyExtractor.extractKey(dataNode);

if (cacheRegistry.contains(objectType)) {
cacheElement = cacheRegistry.lookup(objectType);
}
else {
cacheElement = provideCache;
} else {
cacheElement = cache;
}

final String normalizedPath = path == null ? null : pathSplitter.normalize(path);
Expand All @@ -101,8 +86,7 @@ public BasicElementContext(final @NotNull Registry<ConfigProcessor<?>> processor
final DataInfo dataInfo;
if (dataObjects.containsKey(normalizedPath)) {
dataInfo = dataObjects.get(normalizedPath);
}
else {
} else {
try {
Object data = processorRegistry.contains(objectType) ?
processorRegistry.lookup(objectType).dataFromElement(dataNode) : null;
Expand All @@ -113,8 +97,8 @@ public BasicElementContext(final @NotNull Registry<ConfigProcessor<?>> processor
}
}

final TElement element = (TElement) ((ElementFactory<Object, Object>) factoryRegistry.lookup(dataInfo.type))
.make(dataInfo.data, this, dependencyProvider);
final TElement element = (TElement) ((ElementFactory<Object, Object>) factoryRegistry.lookup(
dataInfo.type)).make(dataInfo.data, this, dependencyProvider);

if (cacheElement) {
elementObjects.put(normalizedPath, element);
Expand All @@ -128,6 +112,13 @@ public BasicElementContext(final @NotNull Registry<ConfigProcessor<?>> processor
return rootNode;
}

@Override
public @NotNull PathSplitter pathSplitter() {
return pathSplitter;
}

private record DataInfo(Object data, Key type) {}

/**
* Basic implementation of {@link ElementContext.Source}.
*/
Expand All @@ -148,17 +139,16 @@ public static class Source implements ElementContext.Source {
* source, used for referencing {@link ElementFactory} objects
* @param cacheRegistry the Registry passed to all BasicElementContext instances created by this source,
* used to determine whether element objects should be cached.
* @param pathSplitter the {@link PathSplitter} used to split path keys
* @param pathSplitter the {@link PathSplitter} used to split path keys
* @param dataLocator the {@link DataLocator} passed to all BasicDataContext instances created by this
* source
* @param keyExtractor the {@link KeyExtractor} passed to all BasicDataContext instances created by this
* source
*/
public Source(final @NotNull Registry<ConfigProcessor<?>> processorRegistry,
final @NotNull Registry<ElementFactory<?, ?>> factoryRegistry,
final @NotNull Registry<Boolean> cacheRegistry,
final @NotNull PathSplitter pathSplitter, final @NotNull DataLocator dataLocator,
final @NotNull KeyExtractor keyExtractor) {
final @NotNull Registry<Boolean> cacheRegistry, final @NotNull PathSplitter pathSplitter,
final @NotNull DataLocator dataLocator, final @NotNull KeyExtractor keyExtractor) {
this.processorRegistry = Objects.requireNonNull(processorRegistry);
this.factoryRegistry = Objects.requireNonNull(factoryRegistry);
this.cacheRegistry = Objects.requireNonNull(cacheRegistry);
Expand Down
Loading

0 comments on commit d44acd3

Please sign in to comment.