org.springframework.util.CollectionUtils.findCommonElementType()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(4.3k)|赞(0)|评价(0)|浏览(115)

本文整理了Java中org.springframework.util.CollectionUtils.findCommonElementType()方法的一些代码示例,展示了CollectionUtils.findCommonElementType()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CollectionUtils.findCommonElementType()方法的具体详情如下:
包路径:org.springframework.util.CollectionUtils
类名称:CollectionUtils
方法名:findCommonElementType

CollectionUtils.findCommonElementType介绍

[英]Find the common element type of the given Collection, if any.
[中]查找给定集合的公共元素类型(如果有)。

代码示例

代码示例来源:origin: spring-projects/spring-data-mongodb

/**
 * Obtain a {@link EncodableValue} given {@code value}.
 *
 * @param value the value to encode, may be {@literal null}.
 * @return the {@link EncodableValue} for {@code value}.
 */
@SuppressWarnings("unchecked")
public static EncodableValue create(@Nullable Object value) {
  if (value instanceof byte[]) {
    return new BinaryValue((byte[]) value);
  }
  if (value instanceof UUID) {
    return new UuidValue((UUID) value);
  }
  if (value instanceof Collection) {
    Collection<?> collection = (Collection<?>) value;
    Class<?> commonElement = CollectionUtils.findCommonElementType(collection);
    if (commonElement != null) {
      if (UUID.class.isAssignableFrom(commonElement)) {
        return new UuidCollection((Collection<UUID>) value);
      }
      if (byte[].class.isAssignableFrom(commonElement)) {
        return new BinaryCollectionValue((Collection<byte[]>) value);
      }
    }
  }
  return new ObjectValue(value);
}

代码示例来源:origin: spring-projects/spring-integration

protected void verifyResultCollectionConsistsOfMessages(Collection<?> elements) {
  Class<?> commonElementType = CollectionUtils.findCommonElementType(elements);
  Assert.isAssignable(Message.class, commonElementType,
      "The expected collection of Messages contains non-Message element: " + commonElementType);
}

代码示例来源:origin: spring-projects/spring-data-rest

/**
 * Returns the type of the expression target based on the given root.
 *
 * @param root must not be {@literal null}.
 * @return
 */
public Class<?> getType(Object root) {
  Assert.notNull(root, "Root object must not be null!");
  try {
    return expression.getValueType(CONTEXT, root);
  } catch (SpelEvaluationException o_O) {
    if (!SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS.equals(o_O.getMessageCode())) {
      throw o_O;
    }
    Object collectionOrArray = getParent().getValue(root);
    if (Collection.class.isInstance(collectionOrArray)) {
      return CollectionUtils.findCommonElementType(Collection.class.cast(collectionOrArray));
    }
  }
  throw new IllegalArgumentException(String.format("Cannot obtain type for path %s on %s!", path, root));
}

代码示例来源:origin: org.springframework.data/spring-data-mongodb

/**
 * Obtain a {@link EncodableValue} given {@code value}.
 *
 * @param value the value to encode, may be {@literal null}.
 * @return the {@link EncodableValue} for {@code value}.
 */
@SuppressWarnings("unchecked")
public static EncodableValue create(@Nullable Object value) {
  if (value instanceof byte[]) {
    return new BinaryValue((byte[]) value);
  }
  if (value instanceof UUID) {
    return new UuidValue((UUID) value);
  }
  if (value instanceof Collection) {
    Collection<?> collection = (Collection<?>) value;
    Class<?> commonElement = CollectionUtils.findCommonElementType(collection);
    if (commonElement != null) {
      if (UUID.class.isAssignableFrom(commonElement)) {
        return new UuidCollection((Collection<UUID>) value);
      }
      if (byte[].class.isAssignableFrom(commonElement)) {
        return new BinaryCollectionValue((Collection<byte[]>) value);
      }
    }
  }
  return new ObjectValue(value);
}

代码示例来源:origin: org.springframework.integration/spring-integration-core

protected void verifyResultCollectionConsistsOfMessages(Collection<?> elements) {
  Class<?> commonElementType = CollectionUtils.findCommonElementType(elements);
  Assert.isAssignable(Message.class, commonElementType,
      "The expected collection of Messages contains non-Message element: " + commonElementType);
}

代码示例来源:origin: org.springframework.data/spring-data-rest-webmvc

/**
 * Returns the type of the expression target based on the given root.
 *
 * @param root must not be {@literal null}.
 * @return
 */
public Class<?> getType(Object root) {
  Assert.notNull(root, "Root object must not be null!");
  try {
    return expression.getValueType(CONTEXT, root);
  } catch (SpelEvaluationException o_O) {
    if (!SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS.equals(o_O.getMessageCode())) {
      throw o_O;
    }
    Object collectionOrArray = getParent().getValue(root);
    if (Collection.class.isInstance(collectionOrArray)) {
      return CollectionUtils.findCommonElementType(Collection.class.cast(collectionOrArray));
    }
  }
  throw new IllegalArgumentException(String.format("Cannot obtain type for path %s on %s!", path, root));
}

相关文章