@@ -1613,6 +1613,72 @@ private static Object makeObjectForRename(Object value, final String oldKey, fin
16131613 return result ;
16141614 }
16151615
1616+ public static Map <String , Object > setValue (final Map <String , Object > map , final String key ,
1617+ final Object newValue ) {
1618+ return setValue (map , key , new BiFunction <String , Object , Object >() {
1619+ public Object apply (String key , Object value ) { return newValue ; } });
1620+ }
1621+
1622+ public static Map <String , Object > setValue (final Map <String , Object > map , final String key ,
1623+ final BiFunction <String , Object , Object > newValue ) {
1624+ Map <String , Object > outMap = newLinkedHashMap ();
1625+ for (Map .Entry <String , Object > entry : map .entrySet ()) {
1626+ if (entry .getKey ().equals (key )) {
1627+ outMap .put (key , makeObjectForSetValue (newValue .apply (key , entry .getValue ()), key , newValue ));
1628+ } else {
1629+ outMap .put (entry .getKey (), makeObjectForSetValue (entry .getValue (), key , newValue ));
1630+ }
1631+ }
1632+ return outMap ;
1633+ }
1634+
1635+ @ SuppressWarnings ("unchecked" )
1636+ private static Object makeObjectForSetValue (Object value , final String key ,
1637+ final BiFunction <String , Object , Object > newValue ) {
1638+ final Object result ;
1639+ if (value instanceof List ) {
1640+ List <Object > values = newArrayList ();
1641+ for (Object item : (List ) value ) {
1642+ values .add (item instanceof Map ? setValue ((Map <String , Object >) item , key , newValue ) : item );
1643+ }
1644+ result = values ;
1645+ } else if (value instanceof Map ) {
1646+ result = setValue ((Map <String , Object >) value , key , newValue );
1647+ } else {
1648+ result = value ;
1649+ }
1650+ return result ;
1651+ }
1652+
1653+ @ SuppressWarnings ("unchecked" )
1654+ public static Map <String , Object > update (final Map <String , Object > map1 , final Map <String , Object > map2 ) {
1655+ Map <String , Object > outMap = newLinkedHashMap ();
1656+ for (String key : map2 .keySet ()) {
1657+ Object value2 = map2 .get (key );
1658+ if (map1 .containsKey (key )) {
1659+ Object value1 = map1 .get (key );
1660+ if (value1 instanceof Map && value2 instanceof Map ) {
1661+ outMap .put (key , update ((Map <String , Object >) value1 , (Map <String , Object >) value2 ));
1662+ } else if (value1 instanceof List && value2 instanceof List ) {
1663+ outMap .put (key , merge ((List <Object >) value1 , (List <Object >) value2 ));
1664+ } else {
1665+ outMap .put (key , value2 );
1666+ }
1667+ } else {
1668+ outMap .put (key , value2 );
1669+ }
1670+ }
1671+ return outMap ;
1672+ }
1673+
1674+ public static List <Object > merge (List <Object > list1 , List <Object > list2 ) {
1675+ List <Object > outList1 = newArrayList (list1 );
1676+ List <Object > outList2 = newArrayList (list2 );
1677+ outList2 .removeAll (list1 );
1678+ outList1 .addAll (outList2 );
1679+ return outList1 ;
1680+ }
1681+
16161682 public static class FetchResponse {
16171683 private final boolean ok ;
16181684 private final int status ;
@@ -2187,6 +2253,14 @@ public static String formatXml(String xml) {
21872253 return Xml .formatXml (xml );
21882254 }
21892255
2256+ public static String changeXmlEncoding (String xml , Xml .XmlStringBuilder .Step identStep , String encoding ) {
2257+ return Xml .changeXmlEncoding (xml , identStep , encoding );
2258+ }
2259+
2260+ public static String changeXmlEncoding (String xml , String encoding ) {
2261+ return Xml .changeXmlEncoding (xml , encoding );
2262+ }
2263+
21902264 public static Map <String , Object > removeMinusesAndConvertNumbers (Map <String , Object > map ) {
21912265 Map <String , Object > outMap = newLinkedHashMap ();
21922266 for (Map .Entry <String , Object > entry : map .entrySet ()) {
@@ -2267,6 +2341,21 @@ public static Map<String, Object> replaceSelfClosingWithNull(Map<String, Object>
22672341 return outMap ;
22682342 }
22692343
2344+ public static long gcd (long value1 , long value2 ) {
2345+ if (value1 == 0 ) {
2346+ return value2 ;
2347+ }
2348+ return gcd (value2 % value1 , value1 );
2349+ }
2350+
2351+ public static long findGcd (long ... array ) {
2352+ long result = array [0 ];
2353+ for (int index = 1 ; index < array .length ; index += 1 ) {
2354+ result = gcd (array [index ], result );
2355+ }
2356+ return result ;
2357+ }
2358+
22702359 @ SuppressWarnings ("unchecked" )
22712360 private static Object makeObjectSelfClose (Object value ) {
22722361 final Object result ;
0 commit comments