|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.bson.codecs.kotlin |
| 17 | + |
| 18 | +import java.lang.reflect.ParameterizedType |
| 19 | +import java.lang.reflect.Type |
| 20 | +import kotlin.reflect.KClass |
| 21 | +import org.bson.BsonReader |
| 22 | +import org.bson.BsonType |
| 23 | +import org.bson.BsonWriter |
| 24 | +import org.bson.codecs.Codec |
| 25 | +import org.bson.codecs.DecoderContext |
| 26 | +import org.bson.codecs.EncoderContext |
| 27 | +import org.bson.codecs.configuration.CodecRegistry |
| 28 | + |
| 29 | +@Suppress("UNCHECKED_CAST") |
| 30 | +internal data class ArrayCodec<R : Any, V>(private val kClass: KClass<R>, private val codec: Codec<V?>) : Codec<R> { |
| 31 | + |
| 32 | + companion object { |
| 33 | + internal fun <R : Any> create( |
| 34 | + kClass: KClass<R>, |
| 35 | + typeArguments: List<Type>, |
| 36 | + codecRegistry: CodecRegistry |
| 37 | + ): Codec<R> { |
| 38 | + assert(kClass.javaObjectType.isArray) { "$kClass must be an array type" } |
| 39 | + val (valueClass, nestedTypes) = |
| 40 | + if (typeArguments.isEmpty()) { |
| 41 | + Pair(kClass.java.componentType.kotlin.javaObjectType as Class<Any>, emptyList()) |
| 42 | + } else { |
| 43 | + // Unroll the actual class and any type arguments |
| 44 | + when (val pType = typeArguments[0]) { |
| 45 | + is Class<*> -> Pair(pType as Class<Any>, emptyList()) |
| 46 | + is ParameterizedType -> Pair(pType.rawType as Class<Any>, pType.actualTypeArguments.toList()) |
| 47 | + else -> Pair(Object::class.java as Class<Any>, emptyList()) |
| 48 | + } |
| 49 | + } |
| 50 | + val codec = |
| 51 | + if (nestedTypes.isEmpty()) codecRegistry.get(valueClass) else codecRegistry.get(valueClass, nestedTypes) |
| 52 | + return ArrayCodec(kClass, codec) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private val isPrimitiveArray = kClass.java.componentType != kClass.java.componentType.kotlin.javaObjectType |
| 57 | + |
| 58 | + override fun encode(writer: BsonWriter, arrayValue: R, encoderContext: EncoderContext) { |
| 59 | + writer.writeStartArray() |
| 60 | + |
| 61 | + boxed(arrayValue).forEach { |
| 62 | + if (it == null) writer.writeNull() else encoderContext.encodeWithChildContext(codec, writer, it) |
| 63 | + } |
| 64 | + |
| 65 | + writer.writeEndArray() |
| 66 | + } |
| 67 | + |
| 68 | + override fun getEncoderClass(): Class<R> = kClass.java |
| 69 | + |
| 70 | + override fun decode(reader: BsonReader, decoderContext: DecoderContext): R { |
| 71 | + reader.readStartArray() |
| 72 | + val data = ArrayList<V?>() |
| 73 | + while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { |
| 74 | + if (reader.currentBsonType == BsonType.NULL) { |
| 75 | + reader.readNull() |
| 76 | + data.add(null) |
| 77 | + } else { |
| 78 | + data.add(decoderContext.decodeWithChildContext(codec, reader)) |
| 79 | + } |
| 80 | + } |
| 81 | + reader.readEndArray() |
| 82 | + return unboxed(data) |
| 83 | + } |
| 84 | + |
| 85 | + fun boxed(arrayValue: R): Iterable<V?> { |
| 86 | + val boxedValue = |
| 87 | + if (!isPrimitiveArray) { |
| 88 | + (arrayValue as Array<V?>).asIterable() |
| 89 | + } else if (arrayValue is BooleanArray) { |
| 90 | + arrayValue.asIterable() |
| 91 | + } else if (arrayValue is ByteArray) { |
| 92 | + arrayValue.asIterable() |
| 93 | + } else if (arrayValue is CharArray) { |
| 94 | + arrayValue.asIterable() |
| 95 | + } else if (arrayValue is DoubleArray) { |
| 96 | + arrayValue.asIterable() |
| 97 | + } else if (arrayValue is FloatArray) { |
| 98 | + arrayValue.asIterable() |
| 99 | + } else if (arrayValue is IntArray) { |
| 100 | + arrayValue.asIterable() |
| 101 | + } else if (arrayValue is LongArray) { |
| 102 | + arrayValue.asIterable() |
| 103 | + } else if (arrayValue is ShortArray) { |
| 104 | + arrayValue.asIterable() |
| 105 | + } else { |
| 106 | + throw IllegalArgumentException("Unsupported array type ${arrayValue.javaClass}") |
| 107 | + } |
| 108 | + return boxedValue as Iterable<V?> |
| 109 | + } |
| 110 | + |
| 111 | + private fun unboxed(data: ArrayList<V?>): R { |
| 112 | + return when (kClass) { |
| 113 | + BooleanArray::class -> (data as ArrayList<Boolean>).toBooleanArray() as R |
| 114 | + ByteArray::class -> (data as ArrayList<Byte>).toByteArray() as R |
| 115 | + CharArray::class -> (data as ArrayList<Char>).toCharArray() as R |
| 116 | + DoubleArray::class -> (data as ArrayList<Double>).toDoubleArray() as R |
| 117 | + FloatArray::class -> (data as ArrayList<Float>).toFloatArray() as R |
| 118 | + IntArray::class -> (data as ArrayList<Int>).toIntArray() as R |
| 119 | + LongArray::class -> (data as ArrayList<Long>).toLongArray() as R |
| 120 | + ShortArray::class -> (data as ArrayList<Short>).toShortArray() as R |
| 121 | + else -> data.toArray(arrayOfNulls(data.size)) as R |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private fun arrayOfNulls(size: Int): Array<V?> { |
| 126 | + return java.lang.reflect.Array.newInstance(codec.encoderClass, size) as Array<V?> |
| 127 | + } |
| 128 | +} |
0 commit comments