com.thematchbox.db.ObjectFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TheMatchBox-Common Show documentation
Show all versions of TheMatchBox-Common Show documentation
This project contains some common utilities used in different projects from theMatchBox.
package com.thematchbox.db;
/* Copyright 2015 theMatchBox
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import java.util.*;
public abstract class ObjectFactory {
public abstract D unmarshall(DBObject o) throws MongoDBException;
public abstract DBObject marshall(D d, MongoDBConnection dbConnection) throws MongoDBException;
public abstract String name();
public T get(DBObject dbObject, String key) {
//noinspection unchecked
return (T) dbObject.get(key);
}
public long getLong(DBObject dbObject, String key) {
return Long.parseLong((String) dbObject.get(key));
}
public void writeLong(BasicDBObject dbObject, String key, long value) {
dbObject.append(key, Long.toString(value));
}
public List> getReferenceList(DBObject dbObject, String key) {
//noinspection unchecked
BasicDBList mongoList = get(dbObject, key);
List> list = new ArrayList<>(mongoList.size());
for (Object o : mongoList) {
//noinspection unchecked
ObjectId objectId = (ObjectId) o;
list.add(new LazyReference(objectId));
}
return list;
}
public BasicDBList marshallReferencesList(List> references) throws MongoDBException {
BasicDBList dbList = new BasicDBList();
for (LazyReference reference : references) {
dbList.add(reference.getObjectId());
}
return dbList;
}
public > E getEnum(DBObject dbObject, String key, Class clazz) {
String name = get(dbObject, key);
return Enum.valueOf(clazz, name);
}
public List unmarshallObjectLists(DBObject dbObject, String key, ObjectFactory factory) throws MongoDBException {
BasicDBList descriptionsList = get(dbObject, key);
List propertyDescriptions = new ArrayList<>(descriptionsList.size());
for (Object o : descriptionsList) {
propertyDescriptions.add(factory.unmarshall((DBObject) o));
}
return propertyDescriptions;
}
public BasicDBList marshallObjectList(List list, MongoDBConnection mongoDBConnection, ObjectFactory factory) throws MongoDBException {
BasicDBList dbList = new BasicDBList();
for (T propertyDescription : list) {
dbList.add(factory.marshall(propertyDescription, mongoDBConnection));
}
return dbList;
}
public Map unmarshallPropertyMap(DBObject dbObject, String key, ObjectFactory factory) throws MongoDBException {
BasicDBObject childObject = get(dbObject, key);
return childObject != null ? unmarshallPropertyMap(childObject, factory) : Collections.emptyMap();
}
public Map unmarshallPropertyMap(DBObject dbObject, ObjectFactory factory) throws MongoDBException {
Map map = new HashMap<>();
for (String key : dbObject.keySet()) {
DBObject childObject = get(dbObject, key);
T value = factory.unmarshall(childObject);
map.put(key, value);
}
return map;
}
public BasicDBObject marshallPropertyMap(Map map, MongoDBConnection mongoDBConnection, ObjectFactory factory) throws MongoDBException {
BasicDBObject dbObject = new BasicDBObject();
for (String key : map.keySet()) {
T value = map.get(key);
dbObject.put(key, factory.marshall(value, mongoDBConnection));
}
return dbObject;
}
public BasicDBObject marshallStringMap(Map map) throws MongoDBException {
BasicDBObject dbObject = new BasicDBObject();
for (String key : map.keySet()) {
dbObject.put(key, map.get(key));
}
return dbObject;
}
public Map unmarshallStringMap(DBObject dbObject) throws MongoDBException {
Map map = new HashMap<>();
for (String key : dbObject.keySet()) {
map.put(key, this.get(dbObject, key));
}
return map;
}
}