public class MarshalUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
MarshalUtils.GenericTransformer<V,T>
Deprecated.
|
static class |
MarshalUtils.Replacer
use this to pass to regexReplace for implementing custom RegEx replacements.
It will allow the regexReplace to dynamically substitute the pattern matching RegEx with substitute(String) of this class. |
static class |
MarshalUtils.Transformer<T>
Deprecated.
|
Modifier and Type | Field and Description |
---|---|
static String |
CLEAN_ALL_EXCEPT_ALPHANUMERIC |
static String |
CLEAN_ALL_WITHSPACE_EXCEPT_ALPHANUMERIC |
static org.apache.commons.collections.Transformer |
hierarchyTransformer
Convert the
List of HierarchialDomainObject to a Comma separated Query consumable String of hierarchies
Usage : StringUtils.join(CollectionUtils.transformedCollection(List<HierarchialDomainObject>, this), ',') |
static MarshalUtils.GenericTransformer |
jsonAuthTransformer |
static MarshalUtils.GenericTransformer<net.sf.ezmorph.bean.MorphDynaBean,Authorization> |
morphDynaBeanToAuthTransformer |
static String |
NAME_PATTERN |
static MarshalUtils.Transformer |
normalizer |
static org.apache.commons.collections.Transformer |
parentIdTransformer
Convert the
List of HierarchialDomainObject to a Comma separated Query consumable String of parent Ids
Usage : StringUtils.join(CollectionUtils.transformedCollection(List<HierarchialDomainObject>, this), ',') |
Constructor and Description |
---|
MarshalUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
cleanString(String str,
String regex,
String replaceStr,
String defaultStr)
It will remove all regex matches within the string.
|
static String |
cleanToDataSafe(String str)
Ensure String is without any special characters etc and all replaced characters are replaced with an underscore
|
static <S,T> Collection<T> |
convert(Collection<S> collection,
MarshalUtils.GenericTransformer<S,T> valueTransformer)
Returns a new Collection with the modified types
|
static <S,T> Collection<T> |
convert(Collection<S> collection,
MarshalUtils.GenericTransformer<S,T> valueTransformer,
Collection<T> existingCollection)
Adds to an existing Collection with the modified types
|
static <T> Collection<T> |
convert(Collection<String> coll,
MarshalUtils.Transformer<T> transformer) |
static <X extends Enum,S,T> |
convert(Map<String,S> map,
Class<X> xClass,
MarshalUtils.GenericTransformer<S,T> valueTransformer)
Converts a Map with String, T type to generic type X, T.
|
static <S,T> Map<String,T> |
convert(Map<String,S> map,
MarshalUtils.GenericTransformer<S,T> valueTransformer)
Returns a new map with the modified types
|
static <X extends Enum,T> |
convert(Map<String,T> map,
Class<X> xClass)
Converts a Map with String, T type to generic type X, T.
|
static <X extends Enum,T> |
convert(Map<X,T> map)
Converts a Map with X, T generic type to String, T.
|
static <S,T> Collection<T> |
convertAndAddAll(Collection<S> collection,
MarshalUtils.GenericTransformer<S,Collection<T>> valueTransformer,
Collection<T> existingCollection)
Adds list elements of the return from a valueTransformer, to an existing Collection with the modified types
|
static <T> Collection<String> |
convertBack(Collection<T> coll,
MarshalUtils.Transformer<T> transformer) |
static Map<String,String> |
convertKeyValueStrToMap(String keyValuePairs,
String delimiter) |
static <O,I> Map<O,Integer> |
convertListToCountMap(List<I> list,
Function<I,O> extractor)
If a List contains repeats, this method will marshal it into a Map, with each entity having a count of the number of instances
in the list passed
|
static String |
decode(String s,
String charSet)
Convenience use for simplified exception handling; handles the ususal Exceptions and nulls
|
static String |
displayFriendly(String str)
Convert Java Style camelCase to Spaced Words
|
static String |
encode(String s,
String charSet)
Convenience use for simplified exception handling; handles the ususal Exceptions and nulls
|
static String |
escapeCommas(String str) |
static String |
exceptionTraceToString(Throwable t) |
static <T> Collection<T> |
filter(Collection<T> collection,
org.apache.commons.collections.Predicate p)
Checks if Objects in the collection match the {@link Predicate; if they do they pass
and are collected in the resultant output else they are filtered out.
|
static <V,T> Collection<T> |
filterAndTransform(Collection<V> collection,
org.apache.commons.collections.Predicate p,
MarshalUtils.GenericTransformer<V,T> transformer)
Checks if Objects in the collection match the {@link Predicate; if they do they pass
and are collected in the resultant output else they are filtered out.
|
static Map |
flipMap(Map input)
Converts keys to value and value to Keys
|
static <T extends Enum> |
getEnumTransformer(Class<T> enumClass) |
static String |
getHex(byte[] bytes) |
static String |
getSHA1DigestHash(byte[] dataBytes)
Get a HexaDecimal Hash Code (SHA1 Digest) for a stream of bytes
|
static String |
getSHA1DigestHash(InputStream in)
Get HEX String (SHA1 Digest) for the stream
|
static String |
getStringAtIndexFromDelimittedString(String delimittedStr,
int index,
String delimiter)
From a String that represents say a comma separated value or some other delimiter.
|
static void |
main(String... s) |
static void |
marshalToCollection(String rawString,
Collection<String> collection)
Convenience method to extract a collection type of Strings from a delimited String (, or ; or new line)
and each returned token will be trimmed.
If the input is null then output is also null. The list is appended is the string that is converted. |
static void |
marshalToCollection(String rawString,
Collection collection,
MarshalUtils.Transformer t)
Convenience method to extract a collection type of Strings from a delimited String (, or ; or new line)
and each returned token will be trimmed.
If the input is null then output is also null. The list is appended is the string that is converted. |
static String |
marshalToJSON(Object o) |
static <T> List<T> |
marshalToList(String[] params,
MarshalUtils.Transformer<T> transformer)
Convenience method to marshal array to Specific Type
and each returned token will be trimmed.
If the input is null then output is also null. |
static <T> List<T> |
marshalToList(String rawString,
MarshalUtils.Transformer<T> transformer)
Convenience method to extract a list of Objects from a delimited String (, or ;)
and each returned token will be trimmed.
If the input is null then output is also null. |
static String |
marshalToString(Collection<?> coll,
Character quot,
MarshalUtils.Transformer t)
For a not null Collection of String's it will return a comma separated String,
with the elements optionally quited if the element is not null.
|
static String |
normalize(String str)
If not null then normalize (by default this means lowercase)'
|
static String |
pad(String s,
int digits,
String pad) |
static String |
padZero(String s)
Default to 2 Digits (single padding)
|
static String |
prepareCSV(String input)
Convert a raw record to a format acceptable for CSV output
|
static String |
regexReplace(String input,
MarshalUtils.Replacer replacer)
Replaces sub-sequences that match the RegEx in replacer with the dynamic ability of
MarshalUtils.Replacer.substitute(String) . |
static String |
replaceQueryParamsPlusSpace(String url)
Query params on encoding all spaces are converted to "+" unlike non Query params that are converted to %20.
|
static String |
sanitize(String str)
Ensure Any Content shown on Screen is sanitized for basic security & also remove formatting & markups before displaying it in a LISTING
|
static String[] |
splitAtCommas(String str)
Split the string as all non escaped comma locations
|
static String |
toAlphaNumeric(String str)
Strips out all non alpha numeric codes; underscores are left.
|
static Set<String> |
tokenize(List<String> strings,
String tokenPatternDelimeter)
For a given List of String , tokenize each string based on tokenPatternDelimeter and return a unique Set of tokens.
|
static <S,T> Map<S,T> |
transform(Collection<T> list,
org.apache.commons.collections.Transformer identifier)
Convert a List of generic type to a Map, with a String as an ID for each element in the List.
|
static <V,T> Collection<T> |
transform(Collection<V> params,
T typeTemplate,
MarshalUtils.GenericTransformer<V,T> transformer) |
static <T,V> Collection<V> |
transform(Map<T,V> map,
org.apache.commons.collections.Predicate keyPredicate,
org.apache.commons.collections.Predicate valuePredicate)
Convert Map values to a Collection; and optionally provide filters in the form of key & value predicates.
|
static <V,T> Collection<T> |
transform(V[] params,
T typeTemplate,
MarshalUtils.GenericTransformer<V,T> transformer) |
static String |
wrapJavaScript(String script) |
public static String CLEAN_ALL_EXCEPT_ALPHANUMERIC
public static String CLEAN_ALL_WITHSPACE_EXCEPT_ALPHANUMERIC
public static String NAME_PATTERN
public static MarshalUtils.Transformer normalizer
public static final MarshalUtils.GenericTransformer<net.sf.ezmorph.bean.MorphDynaBean,Authorization> morphDynaBeanToAuthTransformer
public static final MarshalUtils.GenericTransformer jsonAuthTransformer
public static org.apache.commons.collections.Transformer hierarchyTransformer
List
of HierarchialDomainObject
to a Comma separated Query consumable String of hierarchies
StringUtils.join(CollectionUtils.transformedCollection(List<HierarchialDomainObject>, this), ',')
public static org.apache.commons.collections.Transformer parentIdTransformer
List
of HierarchialDomainObject
to a Comma separated Query consumable String of parent Ids
StringUtils.join(CollectionUtils.transformedCollection(List<HierarchialDomainObject>, this), ',')
public static String toAlphaNumeric(String str)
str
- String
public static String cleanToDataSafe(String str)
public static String cleanString(String str, String regex, String replaceStr, String defaultStr)
CLEAN_ALL_EXCEPT_ALPHANUMERIC
etc.
If string is null default is returnedstr
- as String original inputregex
- as pattern to clean outreplaceStr
- as String replacement for all matching regex to be cleaneddefaultStr
- as String, default value if str is nullpublic static String encode(String s, String charSet)
public static String replaceQueryParamsPlusSpace(String url)
String
public static void main(String... s)
public static String decode(String s, String charSet)
public static String displayFriendly(String str)
String
public static <T> List<T> marshalToList(String rawString, MarshalUtils.Transformer<T> transformer)
as
- generic type MarshalUtils.Transformer
rawString
- as a , or ; or breaks (\r\n then \r then \n) delimeted Stringtransformer
- @nullable trueList
of Transformed Typepublic static <T> List<T> marshalToList(String[] params, MarshalUtils.Transformer<T> transformer)
as
- generic type MarshalUtils.Transformer
params
- as Array of StringList
of Transformed Typepublic static void marshalToCollection(String rawString, Collection<String> collection)
rawString
- as a , or ; or breaks (\r\n then \r then \n) delimited Stringas
- Collectionpublic static void marshalToCollection(String rawString, Collection collection, MarshalUtils.Transformer t)
rawString
- as a , or ; or breaks (\r\n then \r then \n) delimited Stringas
- Collection{@link
- Transformer} to transform Object to Stringpublic static <T> Collection<T> convert(Collection<String> coll, MarshalUtils.Transformer<T> transformer)
T
- as the converted typecoll
- as Collection
transformer
- as Transfomer
Collection
public static <T> Collection<String> convertBack(Collection<T> coll, MarshalUtils.Transformer<T> transformer)
T
- as the converted typecoll
- as Collection
transformer
- as Transfomer
Collection
of String
public static final <T extends Enum> MarshalUtils.Transformer<T> getEnumTransformer(Class<T> enumClass)
public static String marshalToString(Collection<?> coll, Character quot, MarshalUtils.Transformer t)
coll
- as Collection (can be null, will return null)quotation
- Character
(can be null, will return comma separated string)transformer
- as MarshalUtils.Transformer
public static String normalize(String str)
str
- as Stringpublic static final <T> Collection<T> filter(Collection<T> collection, org.apache.commons.collections.Predicate p)
T
- collection
- p
- as Predicate
Collection
public static final <V,T> Collection<T> filterAndTransform(Collection<V> collection, org.apache.commons.collections.Predicate p, MarshalUtils.GenericTransformer<V,T> transformer)
T
- collection
- p
- as Predicate
transformer
- as MarshalUtils.GenericTransformer
Collection
public static <V,T> Collection<T> transform(V[] params, T typeTemplate, MarshalUtils.GenericTransformer<V,T> transformer)
public static <V,T> Collection<T> transform(Collection<V> params, T typeTemplate, MarshalUtils.GenericTransformer<V,T> transformer)
public static <S,T> Map<S,T> transform(Collection<T> list, org.apache.commons.collections.Transformer identifier)
T
- list
- @nullable trueidentifier
- as MarshalUtils.Transformer
@nullable falsepublic static <T,V> Collection<V> transform(Map<T,V> map, org.apache.commons.collections.Predicate keyPredicate, org.apache.commons.collections.Predicate valuePredicate)
T
- V
- map
- keyPredicate
- as Predicate
@nullable true, if true on an evaluation of the key,
will filter out the item and it will not be added to the collectionvaluePredicate
- as Predicate
@nullable true, if true on an evaluation of the value,
will filter out the item and it will not be added to the collectionCollection
public static String regexReplace(String input, MarshalUtils.Replacer replacer) throws Exception
MarshalUtils.Replacer.substitute(String)
.input
- as Stringreplacer
- as MarshalUtils.Replacer
Exception
public static <X extends Enum,T> Map<String,T> convert(Map<X,T> map)
public static <X extends Enum,T> Map<X,T> convert(Map<String,T> map, Class<X> xClass)
public static <X extends Enum,S,T> Map<X,T> convert(Map<String,S> map, Class<X> xClass, MarshalUtils.GenericTransformer<S,T> valueTransformer)
convert(Map, Class)
; it allows us to ad an additional MarshalUtils.GenericTransformer
to transform the value type.X
- as key enum classS
- as Input Map value TypeT
- as Output Map value Typemap
- as Map
xClass
- as type Class
representing XvalueTransformer
- as MarshalUtils.GenericTransformer
Map
public static <O,I> Map<O,Integer> convertListToCountMap(List<I> list, Function<I,O> extractor)
list
- as Listextractor
- as Function
to derive key. It is nullable and by default will assume the object in the list is the Keypublic static <S,T> Map<String,T> convert(Map<String,S> map, MarshalUtils.GenericTransformer<S,T> valueTransformer)
S
- as Input Map value TypeT
- as Output Map value Typemap
- as Map
inputvalueTransformer
- as MarshalUtils.GenericTransformer
Map
public static <S,T> Collection<T> convert(Collection<S> collection, MarshalUtils.GenericTransformer<S,T> valueTransformer)
S
- as Input Collection value TypeT
- as Output Collection value Typecollection
- as Collection
inputvalueTransformer
- as MarshalUtils.GenericTransformer
Map
public static <S,T> Collection<T> convert(Collection<S> collection, MarshalUtils.GenericTransformer<S,T> valueTransformer, Collection<T> existingCollection)
S
- as Input Collection value TypeT
- as Output Collection value Typecollection
- as Collection
inputvalueTransformer
- as MarshalUtils.GenericTransformer
Add
- elements to newCollectionMap
public static <S,T> Collection<T> convertAndAddAll(Collection<S> collection, MarshalUtils.GenericTransformer<S,Collection<T>> valueTransformer, Collection<T> existingCollection)
S
- as Input Collection value TypeT
- as Output Collection value Typecollection
- as Collection
inputvalueTransformer
- as MarshalUtils.GenericTransformer
Add
- elements to newCollectionMap
public static Map flipMap(Map input)
input
- Map
public static String getHex(byte[] bytes)
public static String getSHA1DigestHash(byte[] dataBytes) throws Exception
public static String getSHA1DigestHash(InputStream in) throws Exception
public static final String[] splitAtCommas(String str)
public static String prepareCSV(String input)
input
- Stringpublic static String padZero(String s)
s
- as Stringpublic static String pad(String s, int digits, String pad)
s
- as input string (unpadded)digits
- the number of words the output should occupy. Note: A word is each character in the original string + the # of times we wish to repeat the pad.pad
- as String. The padd to prefix.public static String getStringAtIndexFromDelimittedString(String delimittedStr, int index, String delimiter)
IndexOutOfBoundsException
.delimittedStr
- as Stringpublic static String sanitize(String str)
public static Map<String,String> convertKeyValueStrToMap(String keyValuePairs, String delimiter)
keyValuePairs
- as String. E.g. param1=value1,param2=value2delimiter
- as String, E.g. ,Map
of key valuesCopyright © 2018. All rights reserved.