All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anwen.mongo.toolkit.ObjectIdUtil Maven / Gradle / Ivy

There is a newer version: 2.1.6.1
Show newest version
package com.anwen.mongo.toolkit;

import org.bson.types.ObjectId;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

public class ObjectIdUtil {

    /**
     * 转换单个ObjectId
     */
    public static  Object convertObjectId(T id) {
        if (id == null) {
            return null;
        }
        String strId = String.valueOf(id);
        if (ObjectId.isValid(strId)) {
            return new ObjectId(strId);
        }
        return id;
    }

    /**
     * 批量转换ObjectId
     */
    public static Collection convertObjectId(Collection ids) {
        if (ids == null) {
            return Collections.emptyList();
        }
        return ids.stream().map(ObjectIdUtil::convertObjectId).collect(Collectors.toList());
    }
}