Schema elementType = null;
for (Object element : (Collection<?>)datum) {
if (elementType == null) {
elementType = induce(element);
} else if (!elementType.equals(induce(element))) {
throw new AvroTypeException("No mixed type arrays.");
}
}
if (elementType == null) {
throw new AvroTypeException("Empty array: "+datum);
}
return Schema.createArray(elementType);
} else if (datum instanceof Map) {
@SuppressWarnings(value="unchecked")
Map<Object,Object> map = (Map<Object,Object>)datum;
Schema value = null;
for (Map.Entry<Object,Object> entry : map.entrySet()) {
if (value == null) {
value = induce(entry.getValue());
} else if (!value.equals(induce(entry.getValue()))) {
throw new AvroTypeException("No mixed type map values.");
}
}
if (value == null) {
throw new AvroTypeException("Empty map: "+datum);
}
return Schema.createMap(value);
} else if (datum instanceof GenericFixed) {
return Schema.createFixed(null, null, null,
((GenericFixed)datum).bytes().length);
}
else if (datum instanceof CharSequence) return Schema.create(Type.STRING);
else if (datum instanceof ByteBuffer) return Schema.create(Type.BYTES);
else if (datum instanceof Integer) return Schema.create(Type.INT);
else if (datum instanceof Long) return Schema.create(Type.LONG);
else if (datum instanceof Float) return Schema.create(Type.FLOAT);
else if (datum instanceof Double) return Schema.create(Type.DOUBLE);
else if (datum instanceof Boolean) return Schema.create(Type.BOOLEAN);
else if (datum == null) return Schema.create(Type.NULL);
else throw new AvroTypeException("Can't create schema for: "+datum);
}