private TypeCodec maybeCreateCodec()

in src/java/org/apache/cassandra/cql3/functions/types/CodecRegistry.java [786:891]


    private <T> TypeCodec<T> maybeCreateCodec(DataType cqlType, T value)
    {
        checkNotNull(value);

        if ((cqlType == null || cqlType.getName() == LIST) && value instanceof List)
        {
            List list = (List) value;
            if (list.isEmpty())
            {
                DataType elementType =
                (cqlType == null || cqlType.getTypeArguments().isEmpty())
                ? DataType.blob()
                : cqlType.getTypeArguments().get(0);
                return (TypeCodec<T>) TypeCodec.list(findCodec(elementType, (TypeToken) null));
            }
            else
            {
                DataType elementType =
                (cqlType == null || cqlType.getTypeArguments().isEmpty())
                ? null
                : cqlType.getTypeArguments().get(0);
                return (TypeCodec<T>) TypeCodec.list(findCodec(elementType, list.iterator().next()));
            }
        }

        if ((cqlType == null || cqlType.getName() == SET) && value instanceof Set)
        {
            Set set = (Set) value;
            if (set.isEmpty())
            {
                DataType elementType =
                (cqlType == null || cqlType.getTypeArguments().isEmpty())
                ? DataType.blob()
                : cqlType.getTypeArguments().get(0);
                return (TypeCodec<T>) TypeCodec.set(findCodec(elementType, (TypeToken) null));
            }
            else
            {
                DataType elementType =
                (cqlType == null || cqlType.getTypeArguments().isEmpty())
                ? null
                : cqlType.getTypeArguments().get(0);
                return (TypeCodec<T>) TypeCodec.set(findCodec(elementType, set.iterator().next()));
            }
        }

        if ((cqlType == null || cqlType.getName() == MAP) && value instanceof Map)
        {
            Map map = (Map) value;
            if (map.isEmpty())
            {
                DataType keyType =
                (cqlType == null || cqlType.getTypeArguments().size() < 1)
                ? DataType.blob()
                : cqlType.getTypeArguments().get(0);
                DataType valueType =
                (cqlType == null || cqlType.getTypeArguments().size() < 2)
                ? DataType.blob()
                : cqlType.getTypeArguments().get(1);
                return (TypeCodec<T>) TypeCodec.map(
                findCodec(keyType, (TypeToken) null), findCodec(valueType, (TypeToken) null));
            }
            else
            {
                DataType keyType =
                (cqlType == null || cqlType.getTypeArguments().size() < 1)
                ? null
                : cqlType.getTypeArguments().get(0);
                DataType valueType =
                (cqlType == null || cqlType.getTypeArguments().size() < 2)
                ? null
                : cqlType.getTypeArguments().get(1);
                Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
                return (TypeCodec<T>)
                       TypeCodec.map(
                       findCodec(keyType, entry.getKey()), findCodec(valueType, entry.getValue()));
            }
        }

        if ((cqlType == null || cqlType.getName() == VECTOR) && value instanceof List)
        {
            VectorType type = (VectorType) cqlType;
            return (TypeCodec<T>) TypeCodec.vector(type, findCodec(type.getSubtype(), null));
        }

        if ((cqlType == null || cqlType.getName() == TUPLE)
            && value instanceof TupleValue)
        {
            return (TypeCodec<T>)
                   TypeCodec.tuple(cqlType == null ? ((TupleValue) value).getType() : (TupleType) cqlType);
        }

        if ((cqlType == null || cqlType.getName() == UDT) && value instanceof UDTValue)
        {
            return (TypeCodec<T>)
                   TypeCodec.userType(cqlType == null ? ((UDTValue) value).getType() : (UserType) cqlType);
        }

        if ((cqlType instanceof DataType.CustomType)
            && value instanceof ByteBuffer)
        {
            return (TypeCodec<T>) TypeCodec.custom((DataType.CustomType) cqlType);
        }

        return null;
    }