@@ -82,7 +82,8 @@ public class U<T> extends com.github.underscore.U<T> {
8282 }
8383
8484 public enum Mode {
85- REPLACE_SELF_CLOSING_WITH_NULL ;
85+ REPLACE_SELF_CLOSING_WITH_NULL ,
86+ REPLACE_SELF_CLOSING_WITH_EMPTY ;
8687 }
8788
8889 public U (final Iterable <T > iterable ) {
@@ -739,6 +740,35 @@ public Chain<T> chain() {
739740 return new U .Chain <T >(newArrayList (value ()));
740741 }
741742
743+ public static Chain <String > of (final String item ) {
744+ return new U .Chain <String >(item );
745+ }
746+
747+ public static <T > Chain <T > of (final List <T > list ) {
748+ return new U .Chain <T >(list );
749+ }
750+
751+ public static <T > Chain <T > of (final Iterable <T > iterable ) {
752+ return new U .Chain <T >(newArrayList (iterable ));
753+ }
754+
755+ public static <T > Chain <T > of (final Iterable <T > iterable , int size ) {
756+ return new U .Chain <T >(newArrayList (iterable , size ));
757+ }
758+
759+ @ SuppressWarnings ("unchecked" )
760+ public static <T > Chain <T > of (final T ... list ) {
761+ return new U .Chain <T >(Arrays .asList (list ));
762+ }
763+
764+ public static Chain <Integer > of (final int [] array ) {
765+ return new U .Chain <Integer >(newIntegerList (array ));
766+ }
767+
768+ public Chain <T > of () {
769+ return new U .Chain <T >(newArrayList (value ()));
770+ }
771+
742772 public static <T > List <T > drop (final Iterable <T > iterable ) {
743773 return rest (newArrayList (iterable ));
744774 }
@@ -2226,12 +2256,19 @@ public static String jsonToXml(String json) {
22262256
22272257 @ SuppressWarnings ("unchecked" )
22282258 public static String xmlToJson (String xml , Json .JsonStringBuilder .Step identStep , Mode mode ) {
2229- Object result = Xml .fromXml (xml );
2230- if (result instanceof Map ) {
2231- return Json .toJson (mode == Mode .REPLACE_SELF_CLOSING_WITH_NULL ?
2232- replaceSelfClosingWithNull ((Map ) result ) : (Map ) result , identStep );
2259+ Object object = Xml .fromXml (xml );
2260+ final String result ;
2261+ if (object instanceof Map ) {
2262+ if (mode == Mode .REPLACE_SELF_CLOSING_WITH_NULL ) {
2263+ result = Json .toJson (replaceSelfClosingWithNull ((Map ) object ), identStep );
2264+ } else if (mode == Mode .REPLACE_SELF_CLOSING_WITH_EMPTY ) {
2265+ result = Json .toJson (replaceSelfClosingWithEmpty ((Map ) object ), identStep );
2266+ } else {
2267+ result = Json .toJson ((Map ) object , identStep );
2268+ }
2269+ return result ;
22332270 }
2234- return Json .toJson ((List ) result , identStep );
2271+ return Json .toJson ((List ) object , identStep );
22352272 }
22362273
22372274 public static String xmlToJson (String xml ) {
@@ -2331,21 +2368,50 @@ public static boolean isJsonNumber(final String string) {
23312368 return numberEncountered ;
23322369 }
23332370
2371+ @ SuppressWarnings ("unchecked" )
23342372 public static Map <String , Object > replaceSelfClosingWithNull (Map <String , Object > map ) {
2335- Map <String , Object > outMap = newLinkedHashMap ();
2373+ return (Map <String , Object >) replaceSelfClosingWithValue (map , null );
2374+ }
2375+
2376+ @ SuppressWarnings ("unchecked" )
2377+ public static Map <String , Object > replaceSelfClosingWithEmpty (Map <String , Object > map ) {
2378+ return (Map <String , Object >) replaceSelfClosingWithValue (map , "" );
2379+ }
2380+
2381+ @ SuppressWarnings ("unchecked" )
2382+ public static Object replaceSelfClosingWithValue (Map <String , Object > map , String value ) {
2383+ Object outMap = newLinkedHashMap ();
23362384 for (Map .Entry <String , Object > entry : map .entrySet ()) {
23372385 if ("-self-closing" .equals (entry .getKey ()) && "true" .equals (entry .getValue ())) {
23382386 if (map .size () == 1 ) {
2339- outMap = null ;
2387+ outMap = value ;
23402388 break ;
23412389 }
23422390 continue ;
23432391 }
2344- outMap .put (String .valueOf (entry .getKey ()), makeObjectSelfClose (entry .getValue ()));
2392+ ((Map <String , Object >) outMap ).put (String .valueOf (entry .getKey ()),
2393+ makeObjectSelfClose (entry .getValue (), value ));
23452394 }
23462395 return outMap ;
23472396 }
23482397
2398+ @ SuppressWarnings ("unchecked" )
2399+ private static Object makeObjectSelfClose (Object value , String newValue ) {
2400+ final Object result ;
2401+ if (value instanceof List ) {
2402+ List <Object > values = newArrayList ();
2403+ for (Object item : (List ) value ) {
2404+ values .add (item instanceof Map ? replaceSelfClosingWithValue ((Map ) item , newValue ) : item );
2405+ }
2406+ result = values ;
2407+ } else if (value instanceof Map ) {
2408+ result = replaceSelfClosingWithValue ((Map ) value , newValue );
2409+ } else {
2410+ result = value ;
2411+ }
2412+ return result ;
2413+ }
2414+
23492415 public static long gcd (long value1 , long value2 ) {
23502416 if (value1 == 0 ) {
23512417 return value2 ;
@@ -2361,23 +2427,6 @@ public static long findGcd(long ... array) {
23612427 return result ;
23622428 }
23632429
2364- @ SuppressWarnings ("unchecked" )
2365- private static Object makeObjectSelfClose (Object value ) {
2366- final Object result ;
2367- if (value instanceof List ) {
2368- List <Object > values = newArrayList ();
2369- for (Object item : (List ) value ) {
2370- values .add (item instanceof Map ? replaceSelfClosingWithNull ((Map ) item ) : item );
2371- }
2372- result = values ;
2373- } else if (value instanceof Map ) {
2374- result = replaceSelfClosingWithNull ((Map ) value );
2375- } else {
2376- result = value ;
2377- }
2378- return result ;
2379- }
2380-
23812430 public static Builder objectBuilder () {
23822431 return new U .Builder ();
23832432 }
0 commit comments