use of org.apache.hadoop.io.AbstractMapWritable in project mongo-hadoop by mongodb.
the class BSONWritable method toBSON.
/**
* Unwrap a (usually Writable) Object, getting back a value suitable for
* putting into a BSONObject. If the given object is not Writable, then
* simply return the Object back.
*
* @param x the Object to turn into BSON.
* @return the BSON representation of the Object.
*/
@SuppressWarnings("unchecked")
public static Object toBSON(final Object x) {
if (x == null) {
return null;
}
if (x instanceof Text) {
return x.toString();
}
if (x instanceof BSONWritable) {
return ((BSONWritable) x).getDoc();
}
if (x instanceof Writable) {
if (x instanceof AbstractMapWritable) {
if (!(x instanceof Map)) {
throw new IllegalArgumentException(String.format("Cannot turn %s into BSON, since it does " + "not implement java.util.Map.", x.getClass().getName()));
}
Map<Writable, Writable> map = (Map<Writable, Writable>) x;
BasicBSONObject bson = new BasicBSONObject();
for (Map.Entry<Writable, Writable> entry : map.entrySet()) {
bson.put(entry.getKey().toString(), toBSON(entry.getValue()));
}
return bson;
}
if (x instanceof ArrayWritable) {
Writable[] o = ((ArrayWritable) x).get();
Object[] a = new Object[o.length];
for (int i = 0; i < o.length; i++) {
a[i] = toBSON(o[i]);
}
return a;
}
if (x instanceof NullWritable) {
return null;
}
if (x instanceof BooleanWritable) {
return ((BooleanWritable) x).get();
}
if (x instanceof BytesWritable) {
return ((BytesWritable) x).getBytes();
}
if (x instanceof ByteWritable) {
return ((ByteWritable) x).get();
}
if (x instanceof DoubleWritable) {
return ((DoubleWritable) x).get();
}
if (x instanceof FloatWritable) {
return ((FloatWritable) x).get();
}
if (x instanceof LongWritable) {
return ((LongWritable) x).get();
}
if (x instanceof IntWritable) {
return ((IntWritable) x).get();
}
// TODO - Support counters
}
return x;
}
Aggregations