예제 #1
0
  /**
   * Creates an immutable dictionary containing all of the keys and objects from two dictionaries.
   *
   * @param dict1 the first dictionary
   * @param dict2 the second dictionary
   * @return immutbale dictionary containing the union of the two dictionaries.
   */
  public static <K, V> NSDictionary<K, V> dictionaryWithDictionaryAndDictionary(
      NSDictionary<? extends K, ? extends V> dict1, NSDictionary<? extends K, ? extends V> dict2) {
    if (dict1 == null || dict1.allKeys().count() == 0) return (NSDictionary<K, V>) dict2;
    if (dict2 == null || dict2.allKeys().count() == 0) return (NSDictionary<K, V>) dict1;

    NSMutableDictionary<K, V> result = new NSMutableDictionary<K, V>(dict2);
    result.addEntriesFromDictionary(dict1);
    return new NSDictionary<K, V>(result);
  }
예제 #2
0
 /**
  * Returns a deep clone of the given dictionary. A deep clone will attempt to clone the keys and
  * values (deeply) of this dictionary as well as the dictionary itself.
  *
  * @param dict the dictionary to clone
  * @param onlyCollections if true, only collections in this dictionary will be cloned, not
  *     individual values
  * @return a deep clone of dict
  */
 public static <K, V> NSDictionary<K, V> deepClone(
     NSDictionary<K, V> dict, boolean onlyCollections) {
   NSMutableDictionary<K, V> clonedDict = null;
   if (dict != null) {
     clonedDict = dict.mutableClone();
     for (K key : dict.allKeys()) {
       V value = dict.objectForKey(key);
       K cloneKey = ERXUtilities.deepClone(key, onlyCollections);
       V cloneValue = ERXUtilities.deepClone(value, onlyCollections);
       if (cloneKey != key) {
         clonedDict.removeObjectForKey(key);
         if (cloneValue != null) {
           clonedDict.setObjectForKey(cloneValue, cloneKey);
         }
       } else if (cloneValue != null) {
         if (cloneValue != value) {
           clonedDict.setObjectForKey(cloneValue, cloneKey);
         }
       } else {
         clonedDict.removeObjectForKey(key);
       }
     }
   }
   return clonedDict;
 }
 protected EOQualifier extraQualifier(D2WContext c, NSDictionary<String, Object> dict) {
   NSMutableArray<EOQualifier> qualifiers = new NSMutableArray<>();
   EOQualifier result = null;
   for (String key : dict.allKeys()) {
     Object value = null;
     if (dict.objectForKey(key) instanceof NSDictionary) {
       // qualifier definition with operator
       NSDictionary qDict = (NSDictionary) dict.objectForKey(key);
       if (qDict.size() == 1) {
         String operatorKey = (String) qDict.allKeys().lastObject();
         String contextKeyPath = (String) qDict.objectForKey(operatorKey);
         if ("NSKeyValueCoding.NullValue".equals(contextKeyPath)) {
           value = NSKeyValueCoding.NullValue;
         } else {
           value = c.valueForKeyPath(contextKeyPath);
         }
         if (value != null) {
           EOQualifier q = qualifierForOperatorAndObject(key, operatorKey, value);
           qualifiers.addObject(q);
         }
       }
     } else {
       value = c.valueForKeyPath((String) dict.objectForKey(key));
       if (value != null) {
         EOQualifier q;
         if (value instanceof NSArray) {
           q = qualifierForArray(key, (NSArray) value);
         } else {
           if (value == NSKeyValueCoding.NullValue) {
             value = null;
           }
           q = qualifierForObject(key, value);
         }
         if (q != null) {
           qualifiers.addObject(q);
         }
       }
     }
   }
   if (qualifiers.count() > 0) result = new EOAndQualifier(qualifiers);
   if (log.isDebugEnabled()) {
     log.debug("Computed qualifier: " + result);
   }
   return result;
 }
예제 #4
0
  /**
   * @param d dictionary to sort keys from
   * @return keys from d sorted by ascending value they are mapped to
   */
  public static <T> NSArray<T> keysSortedByValueAscending(final NSDictionary<T, ?> d) {
    NSArray<T> result = null;

    if (d != null && d.count() > 0) {
      final NSArray<T> keys = d.allKeys();
      result =
          ERXArrayUtilities.sortedArrayUsingComparator(keys, new NSDictionaryKeyValueComparator(d));
    }

    return result != null ? result : NSArray.EmptyArray;
  }
예제 #5
0
  // if you're keys are not all strings, this method will throw.
  public static NSArray<String> stringKeysSortedAscending(final NSDictionary<String, ?> d) {
    NSArray<String> result = null;

    if (d != null && d.count() > 0) {
      final NSArray<String> keys = d.allKeys();
      result =
          ERXArrayUtilities.sortedArrayUsingComparator(
              keys, NSComparator.AscendingStringComparator);
    }

    return result != null ? result : NSArray.EmptyArray;
  }
예제 #6
0
 /**
  * Removes an array of keys from a dictionary and returns the result.
  *
  * @param d dictionary to be pruned
  * @param a array of keys to be pruned
  * @return pruned dictionary
  */
 public static <K, V> NSDictionary<K, V> dictionaryByRemovingFromDictionaryKeysInArray(
     NSDictionary<K, V> d, NSArray<K> a) {
   NSMutableDictionary<K, V> result = new NSMutableDictionary<K, V>();
   if (d != null && a != null) {
     for (Enumeration<K> e = d.allKeys().objectEnumerator(); e.hasMoreElements(); ) {
       K key = e.nextElement();
       if (!a.containsObject(key)) {
         result.setObjectForKey(d.objectForKey(key), key);
       }
     }
   }
   return result.immutableClone();
 }
예제 #7
0
 public static Object toJavaCollections(Object obj) {
   Object result;
   if (obj instanceof NSDictionary) {
     Map map = new HashMap();
     NSDictionary nsDict = (NSDictionary) obj;
     Enumeration keysEnum = nsDict.allKeys().objectEnumerator();
     while (keysEnum.hasMoreElements()) {
       Object key = keysEnum.nextElement();
       Object value = nsDict.objectForKey(key);
       key = toJavaCollections(key);
       value = toJavaCollections(value);
       map.put(key, value);
     }
     result = map;
   } else if (obj instanceof NSArray) {
     List list = new LinkedList();
     NSArray nsArray = (NSArray) obj;
     Enumeration valuesEnum = nsArray.objectEnumerator();
     while (valuesEnum.hasMoreElements()) {
       Object value = valuesEnum.nextElement();
       value = toJavaCollections(value);
       list.add(value);
     }
     result = list;
   } else if (obj instanceof NSSet) {
     Set set = new HashSet();
     NSSet nsSet = (NSSet) obj;
     Enumeration valuesEnum = nsSet.objectEnumerator();
     while (valuesEnum.hasMoreElements()) {
       Object value = valuesEnum.nextElement();
       value = toJavaCollections(value);
       set.add(value);
     }
     result = set;
   } else {
     result = obj;
   }
   return result;
 }
예제 #8
0
 /**
  * Encodes a dictionary into a string that can be used in a request uri.
  *
  * @param dict dictionary with form values
  * @param separator optional value separator
  */
 public static String queryStringForDictionary(
     NSDictionary<?, ?> dict, String separator, String encoding) {
   if (separator == null) {
     separator = "&";
   }
   StringBuilder sb = new StringBuilder(100);
   if (dict != null) {
     for (Enumeration<?> e = dict.allKeys().objectEnumerator(); e.hasMoreElements(); ) {
       Object key = e.nextElement();
       try {
         sb.append(URLEncoder.encode(key.toString(), encoding));
         sb.append('=');
         sb.append(URLEncoder.encode(dict.objectForKey(key).toString(), encoding));
         if (e.hasMoreElements()) {
           sb.append(separator);
         }
       } catch (UnsupportedEncodingException ex) {
         // yeah right...like this will ever happen
         throw NSForwardException._runtimeExceptionForThrowable(ex);
       }
     }
   }
   return sb.toString();
 }