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)); } }
/** * Creates a {@code CodecRegistry} from the provided list of {@code Codec} instances. * * <p>This registry can then be used alongside other registries. Typically used when adding extra codecs to existing codecs with the * {@link #fromRegistries(CodecRegistry...)} )} helper.</p> * * @param codecs the {@code Codec} to create a registry for * @return a {@code CodecRegistry} for the given list of {@code Codec} instances. */ public static CodecRegistry fromCodecs(final List<? extends Codec<?>> codecs) { return fromProviders(new MapOfCodecsProvider(codecs)); }
@Override public <T> Codec<T> get(final Class<T> clazz) { return get(new ChildCodecRegistry<T>(this, clazz)); }
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry registry, final PropertyCodecRegistry propertyCodecRegistry, final DiscriminatorLookup discriminatorLookup, final ConcurrentMap<ClassModel<?>, Codec<?>> codecCache, final boolean specialized) { this.classModel = classModel; this.registry = fromRegistries(fromCodecs(this), registry); this.discriminatorLookup = discriminatorLookup; this.codecCache = codecCache; this.propertyCodecRegistry = propertyCodecRegistry; this.specialized = specialized; specialize(); }
static <P> Codec<BsonDocument> create(final Decoder<P> decoder, final List<String> fieldsContainingPayload) { CodecRegistry registry = fromProviders(new CommandResultCodecProvider<P>(decoder, fieldsContainingPayload)); return registry.get(BsonDocument.class); }
private Codec<T> getWrapped() { if (wrapped == null) { wrapped = registry.get(clazz); } return wrapped; } }
/** * Creates a {@code CodecRegistry} from the provided list of {@code Codec} instances. * * <p>This registry can then be used alongside other registries. Typically used when adding extra codecs to existing codecs with the * {@link #fromRegistries(CodecRegistry...)} )} helper.</p> * * @param codecs the {@code Codec} to create a registry for * @return a {@code CodecRegistry} for the given list of {@code Codec} instances. */ public static CodecRegistry fromCodecs(final Codec<?>... codecs) { return fromCodecs(asList(codecs)); }
/** * Creates a new instance with a default codec registry that uses the {@link BsonValueCodecProvider}. */ public BsonValueCodec() { this(fromProviders(new BsonValueCodecProvider())); }
@SuppressWarnings("unchecked") public <T> Codec<T> getOrThrow(final Class<T> clazz) { if (codecCache.containsKey(clazz)) { Optional<? extends Codec<?>> optionalCodec = codecCache.get(clazz); if (!optionalCodec.isEmpty()) { return (Codec<T>) optionalCodec.get(); } } throw new CodecConfigurationException(format("Can't find a codec for %s.", clazz)); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) <T> Codec<T> get(final ChildCodecRegistry context) { if (!codecCache.containsKey(context.getCodecClass())) { for (CodecProvider provider : codecProviders) { Codec<T> codec = provider.get(context.getCodecClass(), context); if (codec != null) { codecCache.put(context.getCodecClass(), codec); return codec; } } codecCache.put(context.getCodecClass(), null); } return codecCache.getOrThrow(context.getCodecClass()); }
@Override public NamedCoordinateReferenceSystem decode(final BsonReader reader, final DecoderContext decoderContext) { CoordinateReferenceSystem crs = decodeCoordinateReferenceSystem(reader); if (crs == null || !(crs instanceof NamedCoordinateReferenceSystem)) { throw new CodecConfigurationException("Invalid NamedCoordinateReferenceSystem."); } return (NamedCoordinateReferenceSystem) crs; } }
@SuppressWarnings({"unchecked", "rawtypes"}) public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) { for (CodecProvider provider : codecProviders) { Codec<T> codec = provider.get(clazz, registry); if (codec != null) { return codec; } } return null; }
@SuppressWarnings("unchecked") public static <T> Optional<T> of(final T it) { if (it == null) { return (Optional<T>) Optional.NONE; } else { return new Some<T>(it); } }
public boolean isDefined() { return !isEmpty(); }
/** * 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 <T> Codec<T> get(final Class<T> clazz, final CodecRegistry outerRregistry) { try { return innerRegistry.get(clazz); } catch (CodecConfigurationException e) { return null; } } };
@SuppressWarnings("unchecked") // hopefully correct @Override public <T> Codec<T> get(Class<T> clazz) { if (!Somebody.class.isAssignableFrom(clazz)) { throw new CodecConfigurationException("Not supported " + clazz); } return (Codec<T>) this; } }
private Codec<TDocument> getCodec() { return codecRegistry.get(documentClass); }
/** * Create a new instance * * @param registry the codec registry */ public GridFSFileCodec(final CodecRegistry registry) { this.documentCodec = notNull("DocumentCodec", notNull("registry", registry).get(Document.class)); this.bsonDocumentCodec = notNull("BsonDocumentCodec", registry.get(BsonDocument.class)); }