/** * Creates a {@code CodecRegistry} from the provided list of {@code CodecProvider} instances. * * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A * requires the construction of a {@code Codec} for class B, and vice versa.</p> * * @param providers the codec provider * @return a {@code CodecRegistry} with the ordered list of {@code CodecProvider} instances. The registry is also guaranteed to be an * instance of {code CodecProvider}, so that when one is passed to {@link #fromRegistries(CodecRegistry...)} or {@link * #fromRegistries(java.util.List)} it will be treated as a {@code CodecProvider} and properly resolve any dependencies between * registries. */ public static CodecRegistry fromProviders(final List<? extends CodecProvider> providers) { return new ProvidersCodecRegistry(providers); }
@Override public int hashCode() { int result = parent != null ? parent.hashCode() : 0; result = 31 * result + registry.hashCode(); result = 31 * result + codecClass.hashCode(); return result; } }
@Override public <T> Codec<T> get(final Class<T> clazz) { return get(new ChildCodecRegistry<T>(this, clazz)); }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ChildCodecRegistry<?> that = (ChildCodecRegistry) o; if (!codecClass.equals(that.codecClass)) { return false; } if (parent != null ? !parent.equals(that.parent) : that.parent != null) { return false; } if (!registry.equals(that.registry)) { return false; } return true; }
public <U> Codec<U> get(final Class<U> clazz) { if (hasCycles(clazz)) { return new LazyCodec<U>(registry, clazz); } else { return registry.get(new ChildCodecRegistry<U>(this, clazz)); } }
/** * A {@code CodecRegistry} that combines the given {@code CodecRegistry} instances into a single registry. * * <p>The registries are checked in order until one returns a {@code Codec} for the requested {@code Class}.</p> * * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A * requires the construction of a {@code Codec} for class B, and vice versa.</p> * <p>Any of the given registries that also implement {@code CodecProvider} will be treated as a {@code CodecProvider} instead of a * {@code CodecRegistry}, which will ensure proper resolution of any dependencies between registries.</p> * * @param registries the preferred registry for {@code Codec} lookups * * @return a {@code CodecRegistry} that combines the list of {@code CodecRegistry} instances into a single one */ public static CodecRegistry fromRegistries(final List<? extends CodecRegistry> registries) { List<CodecProvider> providers = new ArrayList<CodecProvider>(); for (CodecRegistry registry : registries) { providers.add(providerFromRegistry(registry)); } return new ProvidersCodecRegistry(providers); }