Added bunch of javadocs and restricted some visibility.
No functional changes
This commit is contained in:
1 parent
aad8dac580
commit
59f16bfdda
35 files changed
+393
-64
No files matched your search
@@ -2,9 +2,19 @@ package org.scribe.utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Utils for {@link Map} manipulation
|
||||
*
|
||||
* @author Pablo Fernandez
|
||||
*/
|
||||
public class MapUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* Sorts a Map
|
||||
*
|
||||
* @param map unsorted map
|
||||
* @return sorted map
|
||||
*/
|
||||
public static final Map<String, String> sort(Map<String, String> map)
|
||||
{
|
||||
Preconditions.checkNotNull(map, "Cannot sort a null object.");
|
||||
|
||||
@@ -4,27 +4,60 @@ import java.util.regex.Pattern;
|
||||
|
||||
import org.scribe.model.OAuthConstants;
|
||||
|
||||
/**
|
||||
* Utils for checking preconditions and invariants
|
||||
*
|
||||
* @author Pablo Fernandez
|
||||
*/
|
||||
public class Preconditions
|
||||
{
|
||||
private static final String DEFAULT_MESSAGE = "Received an invalid parameter";
|
||||
private static final Pattern URL_PATTERN = Pattern.compile("[a-zA-Z_-]+://\\S+");
|
||||
|
||||
/**
|
||||
* Checks that an object is not null.
|
||||
*
|
||||
* @param object any object
|
||||
* @param errorMsg error message
|
||||
*
|
||||
* @throws IllegalArgumentException if the object is null
|
||||
*/
|
||||
public static void checkNotNull(Object object, String errorMsg)
|
||||
{
|
||||
check(object != null, errorMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that a string is not null or empty
|
||||
*
|
||||
* @param string any string
|
||||
* @param errorMsg error message
|
||||
*
|
||||
* @throws IllegalArgumentException if the string is null or empty
|
||||
*/
|
||||
public static void checkEmptyString(String string, String errorMsg)
|
||||
{
|
||||
check(string != null && !string.trim().equals(""), errorMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that a URL is valid
|
||||
*
|
||||
* @param url any string
|
||||
* @param errorMsg error message
|
||||
*/
|
||||
public static void checkValidUrl(String url, String errorMsg)
|
||||
{
|
||||
checkEmptyString(url, errorMsg);
|
||||
check(isUrl(url), errorMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that a URL is a valid OAuth callback
|
||||
*
|
||||
* @param url any string
|
||||
* @param errorMsg error message
|
||||
*/
|
||||
public static void checkValidOAuthCallback(String url, String errorMsg)
|
||||
{
|
||||
checkEmptyString(url, errorMsg);
|
||||
@@ -34,7 +67,7 @@ public class Preconditions
|
||||
}
|
||||
}
|
||||
|
||||
static boolean isUrl(String url)
|
||||
private static boolean isUrl(String url)
|
||||
{
|
||||
return URL_PATTERN.matcher(url).matches();
|
||||
}
|
||||
|
||||
@@ -2,9 +2,19 @@ package org.scribe.utils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Utils to deal with Streams.
|
||||
*
|
||||
* @author Pablo Fernandez
|
||||
*/
|
||||
public class StreamUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns the stream contents as an UTF-8 encoded string
|
||||
*
|
||||
* @param is input stream
|
||||
* @return string contents
|
||||
*/
|
||||
public static String getStreamContents(InputStream is)
|
||||
{
|
||||
Preconditions.checkNotNull(is, "Cannot get String from a null object");
|
||||
|
||||
@@ -6,9 +6,13 @@ import java.util.*;
|
||||
|
||||
import org.scribe.exceptions.*;
|
||||
|
||||
/**
|
||||
* Utils to deal with URL and url-encodings
|
||||
*
|
||||
* @author Pablo Fernandez
|
||||
*/
|
||||
public class URLUtils
|
||||
{
|
||||
|
||||
private static final String EMPTY_STRING = "";
|
||||
private static final String UTF_8 = "UTF-8";
|
||||
private static final char PAIR_SEPARATOR = '=';
|
||||
@@ -16,6 +20,12 @@ public class URLUtils
|
||||
|
||||
private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);
|
||||
|
||||
/**
|
||||
* Turns a map into a form-url-encoded string (key=value&key2=value2)
|
||||
*
|
||||
* @param map any map
|
||||
* @return form-url-encoded string
|
||||
*/
|
||||
public static String formURLEncodeMap(Map<String, String> map)
|
||||
{
|
||||
Preconditions.checkNotNull(map, "Cannot url-encode a null object");
|
||||
@@ -36,6 +46,12 @@ public class URLUtils
|
||||
return encodedString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Percent encodes a string
|
||||
*
|
||||
* @param plain
|
||||
* @return percent encoded string
|
||||
*/
|
||||
public static String percentEncode(String string)
|
||||
{
|
||||
Preconditions.checkNotNull(string, "Cannot encode null string");
|
||||
@@ -49,6 +65,12 @@ public class URLUtils
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Percent decodes a string
|
||||
*
|
||||
* @param string percent encoded string
|
||||
* @return plain string
|
||||
*/
|
||||
public static String percentDecode(String string)
|
||||
{
|
||||
Preconditions.checkNotNull(string, "Cannot decode null string");
|
||||
|
||||
Reference in new issue
Block a user