Advertisement
Dori_mon

Collection.class Lior Noob

Jul 9th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package com.dorilahav.elusive.data;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.mongodb.client.MongoCollection;
  5. import com.mongodb.client.MongoDatabase;
  6. import lombok.experimental.Delegate;
  7. import org.mongojack.JacksonCodecRegistry;
  8. import org.mongojack.internal.MongoJackModule;
  9.  
  10. public class Collection<T> {
  11.  
  12.     private final Class<T>
  13.             clazz;
  14.  
  15.     @Delegate
  16.     protected MongoCollection<T>
  17.             collection;
  18.  
  19.     protected JacksonCodecRegistry
  20.             jacksonCodecRegistry;
  21.  
  22.     public Collection(MongoDatabase database, ObjectMapper mapper, Class<T> type, String collectionName) {
  23.         this.clazz = type;
  24.         jacksonCodecRegistry = new JacksonCodecRegistry(mapper);
  25.         jacksonCodecRegistry.addCodecForClass(clazz);
  26.         this.collection = database.getCollection(collectionName).withDocumentClass(clazz).withCodecRegistry(jacksonCodecRegistry);
  27.     }
  28.  
  29.     public Collection(MongoDatabase database, Class<T> type, String collectionName) {
  30.         this(database, MongoJackModule.configure(new ObjectMapper()), type, collectionName);
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement