This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2: ADTMirror: a Service + SPI to do ADT reflexion
- Loading branch information
Showing
7 changed files
with
121 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.primeval.reflex.service.adt; | ||
|
||
public abstract class ADTInfo { | ||
|
||
public abstract Class<?> rootType(); | ||
|
||
public abstract String typeName(Class<?> clazz); | ||
|
||
public abstract Class<?> classFor(String typeName); | ||
|
||
public abstract String selectorName(); | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/io/primeval/reflex/service/adt/ADTMirror.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.primeval.reflex.service.adt; | ||
|
||
import java.util.Optional; | ||
|
||
public interface ADTMirror { | ||
|
||
Optional<ADTInfo> getInfo(Class<?> raw); | ||
|
||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/io/primeval/reflex/service/adt/internal/ADTMirrorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.primeval.reflex.service.adt.internal; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
import org.osgi.service.component.annotations.ReferencePolicyOption; | ||
|
||
import io.primeval.reflex.service.adt.ADTInfo; | ||
import io.primeval.reflex.service.adt.ADTMirror; | ||
import io.primeval.reflex.service.adt.spi.ADTMirrorProvider; | ||
|
||
@Component(immediate = true) | ||
public final class ADTMirrorImpl implements ADTMirror { | ||
|
||
public final List<ADTMirrorProvider> providers = new CopyOnWriteArrayList<>(); | ||
|
||
// Switch to evicting cache... need primeval-cache :-) | ||
// Cache only ADTTypes, and make ADT checking fast! | ||
private final Map<Class<?>, ADTInfo> adtCache = new ConcurrentHashMap<>(); | ||
|
||
// A bloomfilter-like structure would be neat to keep failed ADT-checks without using too much mem | ||
|
||
// Right now excluding java.* types, should make this configurable? | ||
|
||
@Override | ||
public Optional<ADTInfo> getInfo(Class<?> raw) { | ||
if (raw.getName().startsWith("java.")) { | ||
return Optional.empty(); | ||
} | ||
return Optional.ofNullable(adtCache.computeIfAbsent(raw, k -> computeADTInfo(k))); | ||
} | ||
|
||
private ADTInfo computeADTInfo(Class<?> raw) { | ||
for (ADTMirrorProvider prov : providers) { | ||
if (prov.isADT(raw)) { | ||
ADTInfo info = computeADTInfoForProvider(prov, raw); | ||
return info; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private ADTInfo computeADTInfoForProvider(ADTMirrorProvider prov, Class<?> raw) { | ||
return prov.getInfo(raw, | ||
k -> adtCache.computeIfAbsent(k, k2 -> computeADTInfoForProvider(prov, k2))); | ||
} | ||
|
||
// Static policy to invalidate cache when a new provider joins! | ||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.STATIC, policyOption = ReferencePolicyOption.GREEDY) | ||
public void addADTMirrorProvider(ADTMirrorProvider adtMirrorProvider) { | ||
providers.add(adtMirrorProvider); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/io/primeval/reflex/service/adt/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@Version("1.0") | ||
package io.primeval.reflex.service.adt; | ||
|
||
import org.osgi.annotation.versioning.Version; | ||
|
12 changes: 12 additions & 0 deletions
12
src/main/java/io/primeval/reflex/service/adt/spi/ADTMirrorProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.primeval.reflex.service.adt.spi; | ||
|
||
import java.util.function.Function; | ||
|
||
import io.primeval.reflex.service.adt.ADTInfo; | ||
|
||
public interface ADTMirrorProvider { | ||
|
||
ADTInfo getInfo(Class<?> raw, Function<Class<?>, ADTInfo> callback); | ||
|
||
boolean isADT(Class<?> raw); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/io/primeval/reflex/service/adt/spi/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@Version("1.0") | ||
package io.primeval.reflex.service.adt.spi; | ||
|
||
import org.osgi.annotation.versioning.Version; | ||
|