Skip to content

Add qualifier support to firebase components. #3180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firebase-common/firebase-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ android {
}

dependencies {
// TODO(vkryachko): have sdks depend on components directly once components are released.
implementation project(':firebase-annotations')
implementation project(':firebase-components')
implementation 'com.google.android.gms:play-services-basement:18.1.0'
implementation "com.google.android.gms:play-services-tasks:18.0.1"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public final class Component<T> {
}

private final String name;
private final Set<Class<? super T>> providedInterfaces;
private final Set<Qualified<? super T>> providedInterfaces;
private final Set<Dependency> dependencies;
private final @Instantiation int instantiation;
private final @ComponentType int type;
Expand All @@ -89,7 +89,7 @@ public final class Component<T> {

private Component(
@Nullable String name,
Set<Class<? super T>> providedInterfaces,
Set<Qualified<? super T>> providedInterfaces,
Set<Dependency> dependencies,
@Instantiation int instantiation,
@ComponentType int type,
Expand Down Expand Up @@ -119,7 +119,7 @@ public String getName() {
*
* <p>Note: T conforms to all of these interfaces.
*/
public Set<Class<? super T>> getProvidedInterfaces() {
public Set<Qualified<? super T>> getProvidedInterfaces() {
return providedInterfaces;
}

Expand Down Expand Up @@ -202,6 +202,18 @@ public static <T> Component.Builder<T> builder(
return new Builder<>(anInterface, additionalInterfaces);
}

/** Returns a Component<T> builder. */
public static <T> Component.Builder<T> builder(Qualified<T> anInterface) {
return new Builder<>(anInterface);
}

/** Returns a Component<T> builder. */
@SafeVarargs
public static <T> Component.Builder<T> builder(
Qualified<T> anInterface, Qualified<? super T>... additionalInterfaces) {
return new Builder<>(anInterface, additionalInterfaces);
}

/**
* Wraps a value in a {@link Component} with no dependencies.
*
Expand All @@ -219,6 +231,13 @@ public static <T> Component<T> of(
return builder(anInterface, additionalInterfaces).factory((args) -> value).build();
}

/** Wraps a value in a {@link Component} with no dependencies. */
@SafeVarargs
public static <T> Component<T> of(
T value, Qualified<T> anInterface, Qualified<? super T>... additionalInterfaces) {
return builder(anInterface, additionalInterfaces).factory((args) -> value).build();
}

/**
* Provides a builder for a {@link Set}-multibinding {@link Component}.
*
Expand All @@ -229,6 +248,16 @@ public static <T> Component.Builder<T> intoSetBuilder(Class<T> anInterface) {
return builder(anInterface).intoSet();
}

/**
* Provides a builder for a {@link Set}-multibinding {@link Component}.
*
* <p>Such components can be requested by dependents via {@link ComponentContainer#setOf(Class)} *
* or {@link ComponentContainer#setOfProvider(Class)}.
*/
public static <T> Component.Builder<T> intoSetBuilder(Qualified<T> anInterface) {
return builder(anInterface).intoSet();
}

/**
* Wraps a value in a {@link Set}-multibinding {@link Component} with no dependencies. *
*
Expand All @@ -239,22 +268,42 @@ public static <T> Component<T> intoSet(T value, Class<T> anInterface) {
return intoSetBuilder(anInterface).factory(c -> value).build();
}

/**
* Wraps a value in a {@link Set}-multibinding {@link Component} with no dependencies. *
*
* <p>Such components can be requested by dependents via {@link ComponentContainer#setOf(Class)} *
* or {@link ComponentContainer#setOfProvider(Class)}.
*/
public static <T> Component<T> intoSet(T value, Qualified<T> anInterface) {
return intoSetBuilder(anInterface).factory(c -> value).build();
}

/** FirebaseComponent builder. */
public static class Builder<T> {
private String name = null;
private final Set<Class<? super T>> providedInterfaces = new HashSet<>();
private final Set<Qualified<? super T>> providedInterfaces = new HashSet<>();
private final Set<Dependency> dependencies = new HashSet<>();
private @Instantiation int instantiation = Instantiation.LAZY;
private @ComponentType int type = ComponentType.VALUE;
private ComponentFactory<T> factory;
private Set<Class<?>> publishedEvents = new HashSet<>();
private final Set<Class<?>> publishedEvents = new HashSet<>();

@SafeVarargs
private Builder(Class<T> anInterface, Class<? super T>... additionalInterfaces) {
Preconditions.checkNotNull(anInterface, "Null interface");
providedInterfaces.add(anInterface);
providedInterfaces.add(Qualified.unqualified(anInterface));
for (Class<? super T> iface : additionalInterfaces) {
Preconditions.checkNotNull(iface, "Null interface");
providedInterfaces.add(Qualified.unqualified(iface));
}
}

@SafeVarargs
private Builder(Qualified<T> anInterface, Qualified<? super T>... additionalInterfaces) {
Preconditions.checkNotNull(anInterface, "Null interface");
providedInterfaces.add(anInterface);
for (Qualified<? super T> iface : additionalInterfaces) {
Preconditions.checkNotNull(iface, "Null interface");
}
Collections.addAll(providedInterfaces, additionalInterfaces);
}
Expand Down Expand Up @@ -301,7 +350,7 @@ private Builder<T> setInstantiation(@Instantiation int instantiation) {
return this;
}

private void validateInterface(Class<?> anInterface) {
private void validateInterface(Qualified<?> anInterface) {
Preconditions.checkArgument(
!providedInterfaces.contains(anInterface),
"Components are not allowed to depend on interfaces they themselves provide.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,41 @@

/** Provides a means to retrieve instances of requested classes/interfaces. */
public interface ComponentContainer {
<T> T get(Class<T> anInterface);
default <T> T get(Class<T> anInterface) {
return get(Qualified.unqualified(anInterface));
}

<T> Provider<T> getProvider(Class<T> anInterface);
default <T> Provider<T> getProvider(Class<T> anInterface) {
return getProvider(Qualified.unqualified(anInterface));
}

<T> Deferred<T> getDeferred(Class<T> anInterface);
default <T> Deferred<T> getDeferred(Class<T> anInterface) {
return getDeferred(Qualified.unqualified(anInterface));
}

<T> Set<T> setOf(Class<T> anInterface);
default <T> Set<T> setOf(Class<T> anInterface) {
return setOf(Qualified.unqualified(anInterface));
}

<T> Provider<Set<T>> setOfProvider(Class<T> anInterface);
default <T> Provider<Set<T>> setOfProvider(Class<T> anInterface) {
return setOfProvider(Qualified.unqualified(anInterface));
}

default <T> T get(Qualified<T> anInterface) {
Provider<T> provider = getProvider(anInterface);
if (provider == null) {
return null;
}
return provider.get();
}

<T> Provider<T> getProvider(Qualified<T> anInterface);

<T> Deferred<T> getDeferred(Qualified<T> anInterface);

default <T> Set<T> setOf(Qualified<T> anInterface) {
return setOfProvider(anInterface).get();
}

<T> Provider<Set<T>> setOfProvider(Qualified<T> anInterface);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
* <p>Does {@link Component} dependency resolution and provides access to resolved {@link
* Component}s via {@link #get(Class)} method.
*/
public class ComponentRuntime extends AbstractComponentContainer implements ComponentLoader {
public class ComponentRuntime implements ComponentContainer, ComponentLoader {
private static final Provider<Set<Object>> EMPTY_PROVIDER = Collections::emptySet;
private final Map<Component<?>, Provider<?>> components = new HashMap<>();
private final Map<Class<?>, Provider<?>> lazyInstanceMap = new HashMap<>();
private final Map<Class<?>, LazySet<?>> lazySetMap = new HashMap<>();
private final Map<Qualified<?>, Provider<?>> lazyInstanceMap = new HashMap<>();
private final Map<Qualified<?>, LazySet<?>> lazySetMap = new HashMap<>();
private final List<Provider<ComponentRegistrar>> unprocessedRegistrarProviders;
private final EventBus eventBus;
private final AtomicReference<Boolean> eagerComponentsInitializedWith = new AtomicReference<>();
Expand Down Expand Up @@ -184,7 +184,7 @@ private List<Runnable> processInstanceComponents(List<Component<?>> componentsTo
}

Provider<?> provider = components.get(component);
for (Class<?> anInterface : component.getProvidedInterfaces()) {
for (Qualified<?> anInterface : component.getProvidedInterfaces()) {
if (!lazyInstanceMap.containsKey(anInterface)) {
lazyInstanceMap.put(anInterface, provider);
} else {
Expand All @@ -203,7 +203,7 @@ private List<Runnable> processInstanceComponents(List<Component<?>> componentsTo
/** Populates lazySetMap to make set components available for consumption via set dependencies. */
private List<Runnable> processSetComponents() {
ArrayList<Runnable> runnables = new ArrayList<>();
Map<Class<?>, Set<Provider<?>>> setIndex = new HashMap<>();
Map<Qualified<?>, Set<Provider<?>>> setIndex = new HashMap<>();
for (Map.Entry<Component<?>, Provider<?>> entry : components.entrySet()) {
Component<?> component = entry.getKey();

Expand All @@ -214,15 +214,15 @@ private List<Runnable> processSetComponents() {

Provider<?> provider = entry.getValue();

for (Class<?> anInterface : component.getProvidedInterfaces()) {
for (Qualified<?> anInterface : component.getProvidedInterfaces()) {
if (!setIndex.containsKey(anInterface)) {
setIndex.put(anInterface, new HashSet<>());
}
setIndex.get(anInterface).add(provider);
}
}

for (Map.Entry<Class<?>, Set<Provider<?>>> entry : setIndex.entrySet()) {
for (Map.Entry<Qualified<?>, Set<Provider<?>>> entry : setIndex.entrySet()) {
if (!lazySetMap.containsKey(entry.getKey())) {
lazySetMap.put(entry.getKey(), LazySet.fromCollection(entry.getValue()));
} else {
Expand All @@ -240,13 +240,13 @@ private List<Runnable> processSetComponents() {

@Override
@SuppressWarnings("unchecked")
public synchronized <T> Provider<T> getProvider(Class<T> anInterface) {
public synchronized <T> Provider<T> getProvider(Qualified<T> anInterface) {
Preconditions.checkNotNull(anInterface, "Null interface requested.");
return (Provider<T>) lazyInstanceMap.get(anInterface);
}

@Override
public <T> Deferred<T> getDeferred(Class<T> anInterface) {
public <T> Deferred<T> getDeferred(Qualified<T> anInterface) {
Provider<T> provider = getProvider(anInterface);
if (provider == null) {
return OptionalProvider.empty();
Expand All @@ -259,7 +259,7 @@ public <T> Deferred<T> getDeferred(Class<T> anInterface) {

@Override
@SuppressWarnings("unchecked")
public synchronized <T> Provider<Set<T>> setOfProvider(Class<T> anInterface) {
public synchronized <T> Provider<Set<T>> setOfProvider(Qualified<T> anInterface) {
LazySet<?> provider = lazySetMap.get(anInterface);
if (provider != null) {
return (Provider<Set<T>>) (Provider<?>) provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
/** Cycle detector for the {@link Component} dependency graph. */
class CycleDetector {
private static class Dep {
private final Class<?> anInterface;
private final Qualified<?> anInterface;
private final boolean set;

private Dep(Class<?> anInterface, boolean set) {
private Dep(Qualified<?> anInterface, boolean set) {
this.anInterface = anInterface;
this.set = set;
}
Expand Down Expand Up @@ -135,7 +135,7 @@ private static Set<ComponentNode> toGraph(List<Component<?>> components) {
Map<Dep, Set<ComponentNode>> componentIndex = new HashMap<>(components.size());
for (Component<?> component : components) {
ComponentNode node = new ComponentNode(component);
for (Class<?> anInterface : component.getProvidedInterfaces()) {
for (Qualified<?> anInterface : component.getProvidedInterfaces()) {
Dep cmp = new Dep(anInterface, !component.isValue());
if (!componentIndex.containsKey(cmp)) {
componentIndex.put(cmp, new HashSet<>());
Expand Down
Loading