From ffb295971af010e44f066f8f69665df060d0d761 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 18 Jan 2017 00:27:04 +0100 Subject: [PATCH] Polishing --- .../web/util/HierarchicalUriComponents.java | 34 ++++++++--------- .../web/util/UriComponents.java | 30 +++++++-------- .../springframework/web/util/UriUtils.java | 38 +++++++++---------- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index dd86dc59e4..2adb6a5b6c 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,9 @@ final class HierarchicalUriComponents extends UriComponents { private static final char PATH_DELIMITER = '/'; + private static final String PATH_DELIMITER_STRING = "/"; + + private final String userInfo; private final String host; @@ -216,15 +219,13 @@ final class HierarchicalUriComponents extends UriComponents { /** * Encode the given source into an encoded String using the rules specified * by the given component and with the given options. - * @param source the source string - * @param encoding the encoding of the source string + * @param source the source String + * @param encoding the encoding of the source String * @param type the URI component for the source * @return the encoded URI * @throws IllegalArgumentException when the given uri parameter is not a valid URI */ - static String encodeUriComponent(String source, String encoding, Type type) - throws UnsupportedEncodingException { - + static String encodeUriComponent(String source, String encoding, Type type) throws UnsupportedEncodingException { if (source == null) { return null; } @@ -256,7 +257,7 @@ final class HierarchicalUriComponents extends UriComponents { } private Type getHostType() { - return (this.host != null && this.host.startsWith("[")) ? Type.HOST_IPV6 : Type.HOST_IPV4; + return (this.host != null && this.host.startsWith("[") ? Type.HOST_IPV6 : Type.HOST_IPV4); } @@ -365,7 +366,7 @@ final class HierarchicalUriComponents extends UriComponents { // other functionality /** - * Returns a URI string from this {@code UriComponents} instance. + * Returns a URI String from this {@code UriComponents} instance. */ @Override public String toUriString() { @@ -649,13 +650,12 @@ final class HierarchicalUriComponents extends UriComponents { /** - * Represents a path backed by a string. + * Represents a path backed by a String. */ static final class FullPathComponent implements PathComponent { private final String path; - public FullPathComponent(String path) { this.path = path; } @@ -667,15 +667,15 @@ final class HierarchicalUriComponents extends UriComponents { @Override public List getPathSegments() { - String delimiter = new String(new char[]{PATH_DELIMITER}); - String[] pathSegments = StringUtils.tokenizeToStringArray(path, delimiter); - return Collections.unmodifiableList(Arrays.asList(pathSegments)); + String[] segments = StringUtils.tokenizeToStringArray(this.path, PATH_DELIMITER_STRING); + return Collections.unmodifiableList(Arrays.asList(segments)); } @Override public PathComponent encode(String encoding) throws UnsupportedEncodingException { - String encodedPath = encodeUriComponent(getPath(),encoding, Type.PATH); - return new FullPathComponent(encodedPath); } + String encodedPath = encodeUriComponent(getPath(), encoding, Type.PATH); + return new FullPathComponent(encodedPath); + } @Override public void verify() { @@ -707,14 +707,14 @@ final class HierarchicalUriComponents extends UriComponents { /** - * Represents a path backed by a string list (i.e. path segments). + * Represents a path backed by a String list (i.e. path segments). */ static final class PathSegmentComponent implements PathComponent { private final List pathSegments; public PathSegmentComponent(List pathSegments) { - Assert.notNull(pathSegments); + Assert.notNull(pathSegments, "List must not be null"); this.pathSegments = Collections.unmodifiableList(new ArrayList(pathSegments)); } diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponents.java b/spring-web/src/main/java/org/springframework/web/util/UriComponents.java index b9d2c60767..28d1397881 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponents.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,7 +44,7 @@ public abstract class UriComponents implements Serializable { private static final String DEFAULT_ENCODING = "UTF-8"; - /** Captures URI template variable names. */ + /** Captures URI template variable names */ private static final Pattern NAMES_PATTERN = Pattern.compile("\\{([^/]+?)\\}"); @@ -59,57 +59,57 @@ public abstract class UriComponents implements Serializable { } - // component getters + // Component getters /** - * Returns the scheme. Can be {@code null}. + * Return the scheme. Can be {@code null}. */ public final String getScheme() { return this.scheme; } /** - * Returns the scheme specific part. Can be {@code null}. + * Return the scheme specific part. Can be {@code null}. */ public abstract String getSchemeSpecificPart(); /** - * Returns the user info. Can be {@code null}. + * Return the user info. Can be {@code null}. */ public abstract String getUserInfo(); /** - * Returns the host. Can be {@code null}. + * Return the host. Can be {@code null}. */ public abstract String getHost(); /** - * Returns the port. Returns {@code -1} if no port has been set. + * Return the port. {@code -1} if no port has been set. */ public abstract int getPort(); /** - * Returns the path. Can be {@code null}. + * Return the path. Can be {@code null}. */ public abstract String getPath(); /** - * Returns the list of path segments. Empty if no path has been set. + * Return the list of path segments. Empty if no path has been set. */ public abstract List getPathSegments(); /** - * Returns the query. Can be {@code null}. + * Return the query. Can be {@code null}. */ public abstract String getQuery(); /** - * Returns the map of query parameters. Empty if no query has been set. + * Return the map of query parameters. Empty if no query has been set. */ public abstract MultiValueMap getQueryParams(); /** - * Returns the fragment. Can be {@code null}. + * Return the fragment. Can be {@code null}. */ public final String getFragment() { return this.fragment; @@ -189,7 +189,7 @@ public abstract class UriComponents implements Serializable { public abstract UriComponents normalize(); /** - * Return a URI string from this {@code UriComponents} instance. + * Return a URI String from this {@code UriComponents} instance. */ public abstract String toUriString(); @@ -210,7 +210,7 @@ public abstract class UriComponents implements Serializable { protected abstract void copyToUriComponentsBuilder(UriComponentsBuilder builder); - // static expansion helpers + // Static expansion helpers static String expandUriComponent(String source, UriTemplateVariables uriVariables) { if (source == null) { diff --git a/spring-web/src/main/java/org/springframework/web/util/UriUtils.java b/spring-web/src/main/java/org/springframework/web/util/UriUtils.java index 6b2f3e5049..62b57a1321 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriUtils.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +41,7 @@ import org.springframework.util.Assert; public abstract class UriUtils { /** - * Encodes the given URI scheme with the given encoding. + * Encode the given URI scheme with the given encoding. * @param scheme the scheme to be encoded * @param encoding the character encoding to encode to * @return the encoded scheme @@ -52,7 +52,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI authority with the given encoding. + * Encode the given URI authority with the given encoding. * @param authority the authority to be encoded * @param encoding the character encoding to encode to * @return the encoded authority @@ -63,7 +63,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI user info with the given encoding. + * Encode the given URI user info with the given encoding. * @param userInfo the user info to be encoded * @param encoding the character encoding to encode to * @return the encoded user info @@ -74,7 +74,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI host with the given encoding. + * Encode the given URI host with the given encoding. * @param host the host to be encoded * @param encoding the character encoding to encode to * @return the encoded host @@ -85,7 +85,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI port with the given encoding. + * Encode the given URI port with the given encoding. * @param port the port to be encoded * @param encoding the character encoding to encode to * @return the encoded port @@ -96,7 +96,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI path with the given encoding. + * Encode the given URI path with the given encoding. * @param path the path to be encoded * @param encoding the character encoding to encode to * @return the encoded path @@ -107,7 +107,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI path segment with the given encoding. + * Encode the given URI path segment with the given encoding. * @param segment the segment to be encoded * @param encoding the character encoding to encode to * @return the encoded segment @@ -118,7 +118,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI query with the given encoding. + * Encode the given URI query with the given encoding. * @param query the query to be encoded * @param encoding the character encoding to encode to * @return the encoded query @@ -129,7 +129,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI query parameter with the given encoding. + * Encode the given URI query parameter with the given encoding. * @param queryParam the query parameter to be encoded * @param encoding the character encoding to encode to * @return the encoded query parameter @@ -140,7 +140,7 @@ public abstract class UriUtils { } /** - * Encodes the given URI fragment with the given encoding. + * Encode the given URI fragment with the given encoding. * @param fragment the fragment to be encoded * @param encoding the character encoding to encode to * @return the encoded fragment @@ -155,9 +155,9 @@ public abstract class UriUtils { * RFC 3986 Section 2. *

This can be used to ensure the given String will not contain any * characters with reserved URI meaning regardless of URI component. - * @param source the string to be encoded + * @param source the String to be encoded * @param encoding the character encoding to encode to - * @return the encoded string + * @return the encoded String * @throws UnsupportedEncodingException when the given encoding parameter is not supported */ public static String encode(String source, String encoding) throws UnsupportedEncodingException { @@ -165,25 +165,25 @@ public abstract class UriUtils { return HierarchicalUriComponents.encodeUriComponent(source, encoding, type); } - // decoding - /** - * Decodes the given encoded source String into an URI. Based on the following rules: + * Decode the given encoded URI component. *

    *
  • Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"}, and * {@code "0"} through {@code "9"} stay the same.
  • *
  • Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.
  • *
  • A sequence "{@code %xy}" is interpreted as a hexadecimal representation of the character.
  • *
- * @param source the source string + * @param source the encoded String * @param encoding the encoding - * @return the decoded URI + * @return the decoded value * @throws IllegalArgumentException when the given source contains invalid encoded sequences * @throws UnsupportedEncodingException when the given encoding parameter is not supported * @see java.net.URLDecoder#decode(String, String) */ public static String decode(String source, String encoding) throws UnsupportedEncodingException { - Assert.notNull(source, "Source must not be null"); + if (source == null) { + return null; + } Assert.hasLength(encoding, "Encoding must not be empty"); int length = source.length(); ByteArrayOutputStream bos = new ByteArrayOutputStream(length);