本文整理了Java中org.springframework.util.CollectionUtils
类的一些代码示例,展示了CollectionUtils
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CollectionUtils
类的具体详情如下:
包路径:org.springframework.util.CollectionUtils
类名称:CollectionUtils
[英]Miscellaneous collection utility methods. Mainly for internal use within the framework.
[中]杂项收集实用程序方法。主要用于框架内的内部使用。
代码示例来源:origin: spring-projects/spring-framework
private void resolveResourceLocations() {
if (CollectionUtils.isEmpty(this.locationValues)) {
return;
}
else if (!CollectionUtils.isEmpty(this.locations)) {
throw new IllegalArgumentException("Please set either Resource-based \"locations\" or " +
"String-based \"locationValues\", but not both.");
}
Assert.notNull(this.resourceLoader,
"ResourceLoader is required when \"locationValues\" are configured.");
for (String location : this.locationValues) {
Resource resource = this.resourceLoader.getResource(location);
this.locations.add(resource);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public void printValue(String label, @Nullable Object value) {
if (value != null && value.getClass().isArray()) {
value = CollectionUtils.arrayToList(value);
}
writer.println(String.format("%17s = %s", label, value));
}
});
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the environment properties used to construct the {@code JMXConnectorServer}
* as {@code java.util.Properties} (String key/value pairs).
*/
public void setEnvironment(@Nullable Properties environment) {
CollectionUtils.mergePropertiesIntoMap(environment, this.environment);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Find a single value of one of the given types in the given Collection:
* searching the Collection for a value of the first type, then
* searching for a value of the second type, etc.
* @param collection the collection to search
* @param types the types to look for, in prioritized order
* @return a value of one of the given types found if there is a clear match,
* or {@code null} if none or more than one such value found
*/
@Nullable
public static Object findValueOfType(Collection<?> collection, Class<?>[] types) {
if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
return null;
}
for (Class<?> type : types) {
Object value = findValueOfType(collection, type);
if (value != null) {
return value;
}
}
return null;
}
代码示例来源:origin: spring-projects/spring-framework
protected boolean checkDestinationPrefix(@Nullable String destination) {
if (destination == null || CollectionUtils.isEmpty(this.destinationPrefixes)) {
return true;
}
for (String prefix : this.destinationPrefixes) {
if (destination.startsWith(prefix)) {
return true;
}
}
return false;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return whether this ModelAndView object is empty,
* i.e. whether it does not hold any view and does not contain a model.
*/
public boolean isEmpty() {
return (this.view == null && CollectionUtils.isEmpty(this.model));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a container-managed extended EntityManager proxy.
* @param emf the EntityManagerFactory to create the EntityManager with.
* If this implements the EntityManagerFactoryInfo interface, the corresponding
* JpaDialect and PersistenceUnitInfo will be detected accordingly.
* @param properties the properties to be passed into the {@code createEntityManager}
* call (may be {@code null})
* @param synchronizedWithTransaction whether to automatically join ongoing
* transactions (according to the JPA 2.1 SynchronizationType rules)
* @return a container-managed EntityManager that expects container-driven lifecycle
* management but may opt out of automatic transaction synchronization
* @since 4.0
* @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
*/
public static EntityManager createContainerManagedEntityManager(
EntityManagerFactory emf, @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) {
Assert.notNull(emf, "EntityManagerFactory must not be null");
if (emf instanceof EntityManagerFactoryInfo) {
EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
EntityManagerFactory nativeEmf = emfInfo.getNativeEntityManagerFactory();
EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ?
nativeEmf.createEntityManager(properties) : nativeEmf.createEntityManager());
return createProxy(rawEntityManager, emfInfo, true, synchronizedWithTransaction);
}
else {
EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ?
emf.createEntityManager(properties) : emf.createEntityManager());
return createProxy(rawEntityManager, null, null, null, null, true, synchronizedWithTransaction);
}
}
代码示例来源:origin: hs-web/hsweb-framework
@Override
public List<Term> getTerms() {
List<Term> terms = super.getTerms();
if (CollectionUtils.isEmpty(terms) && StringUtils.hasText(termExpression)) {
setTerms(terms = TermExpressionParser.parse(termExpression));
}
return terms;
}
代码示例来源:origin: spring-projects/spring-framework
@SuppressWarnings("unchecked")
private boolean safeExtension(HttpServletRequest request, @Nullable String extension) {
if (!StringUtils.hasText(extension)) {
return true;
}
extension = extension.toLowerCase(Locale.ENGLISH);
if (this.safeExtensions.contains(extension)) {
return true;
}
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (pattern != null && pattern.endsWith("." + extension)) {
return true;
}
if (extension.equals("html")) {
String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
Set<MediaType> mediaTypes = (Set<MediaType>) request.getAttribute(name);
if (!CollectionUtils.isEmpty(mediaTypes) && mediaTypes.contains(MediaType.TEXT_HTML)) {
return true;
}
}
return safeMediaTypesForExtension(new ServletWebRequest(request), extension);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Writes the given values as hidden fields.
*/
private void writeHiddenFields(@Nullable Map<String, String> hiddenFields) throws JspException {
if (!CollectionUtils.isEmpty(hiddenFields)) {
Assert.state(this.tagWriter != null, "No TagWriter set");
this.tagWriter.appendValue("<div>\n");
for (String name : hiddenFields.keySet()) {
this.tagWriter.appendValue("<input type=\"hidden\" ");
this.tagWriter.appendValue("name=\"" + name + "\" value=\"" + hiddenFields.get(name) + "\" ");
this.tagWriter.appendValue("/>\n");
}
this.tagWriter.appendValue("</div>");
}
}
代码示例来源:origin: spring-projects/spring-framework
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes)
throws Exception {
List<View> candidateViews = new ArrayList<>();
if (this.viewResolvers != null) {
Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set");
for (ViewResolver viewResolver : this.viewResolvers) {
View view = viewResolver.resolveViewName(viewName, locale);
if (view != null) {
candidateViews.add(view);
}
for (MediaType requestedMediaType : requestedMediaTypes) {
List<String> extensions = this.contentNegotiationManager.resolveFileExtensions(requestedMediaType);
for (String extension : extensions) {
String viewNameWithExtension = viewName + '.' + extension;
view = viewResolver.resolveViewName(viewNameWithExtension, locale);
if (view != null) {
candidateViews.add(view);
}
}
}
}
}
if (!CollectionUtils.isEmpty(this.defaultViews)) {
candidateViews.addAll(this.defaultViews);
}
return candidateViews;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Convert each {@code HttpRange} into a {@code ResourceRegion}, selecting the
* appropriate segment of the given {@code Resource} using HTTP Range information.
* @param ranges the list of ranges
* @param resource the resource to select the regions from
* @return the list of regions for the given resource
* @throws IllegalArgumentException if the sum of all ranges exceeds the
* resource length.
* @since 4.3
*/
public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Resource resource) {
if (CollectionUtils.isEmpty(ranges)) {
return Collections.emptyList();
}
List<ResourceRegion> regions = new ArrayList<>(ranges.size());
for (HttpRange range : ranges) {
regions.add(range.toResourceRegion(resource));
}
if (ranges.size() > 1) {
long length = getLengthFor(resource);
long total = regions.stream().map(ResourceRegion::getCount).reduce(0L, (count, sum) -> sum + count);
Assert.isTrue(total < length,
() -> "The sum of all ranges (" + total + ") " +
"should be less than the resource length (" + length + ")");
}
return regions;
}
代码示例来源:origin: spring-projects/spring-framework
@Nullable
private Locale resolveSupportedLocale(@Nullable List<Locale> requestLocales) {
if (CollectionUtils.isEmpty(requestLocales)) {
return this.defaultLocale; // may be null
if (!StringUtils.hasLength(candidate.getCountry()) &&
candidate.getLanguage().equals(locale.getLanguage())) {
languageMatch = candidate;
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a WebSocketExtension with the given name and parameters.
* @param name the name of the extension
* @param parameters the parameters
*/
public WebSocketExtension(String name, @Nullable Map<String, String> parameters) {
Assert.hasLength(name, "Extension name must not be empty");
this.name = name;
if (!CollectionUtils.isEmpty(parameters)) {
Map<String, String> map = new LinkedCaseInsensitiveMap<>(parameters.size(), Locale.ENGLISH);
map.putAll(parameters);
this.parameters = Collections.unmodifiableMap(map);
}
else {
this.parameters = Collections.emptyMap();
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return a unique result object from the given Collection.
* <p>Returns {@code null} if 0 result objects found;
* throws an exception if more than 1 instance found.
* @param results the result Collection (can be {@code null})
* @return the unique result object, or {@code null} if none
* @throws IncorrectResultSizeDataAccessException if more than one
* result object has been found in the given Collection
* @see org.springframework.util.CollectionUtils#hasUniqueObject
*/
@Nullable
public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
if (CollectionUtils.isEmpty(results)) {
return null;
}
if (!CollectionUtils.hasUniqueObject(results)) {
throw new IncorrectResultSizeDataAccessException(1, results.size());
}
return results.iterator().next();
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Assert that a Map contains entries; that is, it must not be {@code null}
* and must contain at least one entry.
* <pre class="code">
* Assert.notEmpty(map, () -> "The " + mapType + " map must contain entries");
* </pre>
* @param map the map to check
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
* @since 5.0
*/
public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public List<String> get(Object key) {
Assert.isInstanceOf(String.class, key, "Key must be a String-based header name");
Collection<String> values1 = servletResponse.getHeaders((String) key);
if (headersWritten) {
return new ArrayList<>(values1);
}
boolean isEmpty1 = CollectionUtils.isEmpty(values1);
List<String> values2 = super.get(key);
boolean isEmpty2 = CollectionUtils.isEmpty(values2);
if (isEmpty1 && isEmpty2) {
return null;
}
List<String> values = new ArrayList<>();
if (!isEmpty1) {
values.addAll(values1);
}
if (!isEmpty2) {
values.addAll(values2);
}
return values;
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create new ServletConfigPropertyValues.
* @param config the ServletConfig we'll use to take PropertyValues from
* @param requiredProperties set of property names we need, where
* we can't accept default values
* @throws ServletException if any required properties are missing
*/
public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)
throws ServletException {
Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
new HashSet<>(requiredProperties) : null);
Enumeration<String> paramNames = config.getInitParameterNames();
while (paramNames.hasMoreElements()) {
String property = paramNames.nextElement();
Object value = config.getInitParameter(property);
addPropertyValue(new PropertyValue(property, value));
if (missingProps != null) {
missingProps.remove(property);
}
}
// Fail if we are still missing properties.
if (!CollectionUtils.isEmpty(missingProps)) {
throw new ServletException(
"Initialization from ServletConfig for servlet '" + config.getServletName() +
"' failed; the following required properties were missing: " +
StringUtils.collectionToDelimitedString(missingProps, ", "));
}
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the environment properties used to construct the {@code JMXConnector}
* as {@code java.util.Properties} (String key/value pairs).
*/
public void setEnvironment(Properties environment) {
CollectionUtils.mergePropertiesIntoMap(environment, this.environment);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Render the inner '{@code option}' tags using the {@link #optionSource}.
* @see #doRenderFromCollection(java.util.Collection, TagWriter)
*/
private void renderFromArray(TagWriter tagWriter) throws JspException {
doRenderFromCollection(CollectionUtils.arrayToList(this.optionSource), tagWriter);
}
内容来源于网络,如有侵权,请联系作者删除!