Example #1
0
 @Override
 public Tuple2<HashMap<K, V>, HashMap<K, V>> partition(Predicate<? super Tuple2<K, V>> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   final Tuple2<Iterator<Tuple2<K, V>>, Iterator<Tuple2<K, V>>> p =
       iterator().partition(predicate);
   return Tuple.of(HashMap.ofAll(p._1), HashMap.ofAll(p._2));
 }
Example #2
0
 public static HashMap<Long, Long> factorization(long num) {
   if (num == 1) {
     return HashMap.empty();
   } else {
     return primeFactors(num)
         .map(p -> HashMap.of(Tuple.of(p, 1L)).merge(factorization(num / p), (a, b) -> a + b))
         .orElseGet(HashMap::empty);
   }
 }
Example #3
0
 @Test
 public void shouldConvertToMap() {
   final Value<Integer> value = of(1, 2, 3);
   final Map<Integer, Integer> map = value.toMap(v -> Tuple.of(v, v));
   if (value.isSingleValued()) {
     assertThat(map).isEqualTo(HashMap.of(1, 1));
   } else {
     assertThat(map).isEqualTo(HashMap.empty().put(1, 1).put(2, 2).put(3, 3));
   }
 }
Example #4
0
 @Override
 public <C> Map<C, HashMap<K, V>> groupBy(Function<? super Tuple2<K, V>, ? extends C> classifier) {
   Objects.requireNonNull(classifier, "classifier is null");
   return foldLeft(
       HashMap.empty(),
       (map, entry) -> {
         final C key = classifier.apply(entry);
         final HashMap<K, V> values =
             map.get(key).map(entries -> entries.put(entry)).orElse(HashMap.of(entry));
         return map.put(key, values);
       });
 }
Example #5
0
 @Override
 public HashMap<K, V> takeRight(int n) {
   if (trie.size() <= n) {
     return this;
   } else {
     return HashMap.ofAll(trie.iterator().takeRight(n));
   }
 }
Example #6
0
 @Override
 public HashMap<K, V> dropRight(int n) {
   if (n <= 0) {
     return this;
   }
   if (n >= length()) {
     return empty();
   }
   return HashMap.ofAll(iterator().dropRight(n));
 }
Example #7
0
 @Override
 public <U, W> HashMap<U, W> map(
     BiFunction<? super K, ? super V, ? extends Tuple2<? extends U, ? extends W>> mapper) {
   Objects.requireNonNull(mapper, "mapper is null");
   return foldLeft(
       HashMap.empty(),
       (acc, entry) ->
           acc.put(
               entry.flatMap((BiFunction<K, V, Tuple2<? extends U, ? extends W>>) mapper::apply)));
 }
Example #8
0
 @Override
 public HashMap<K, V> merge(Map<? extends K, ? extends V> that) {
   Objects.requireNonNull(that, "that is null");
   if (isEmpty()) {
     return HashMap.ofAll(that);
   } else if (that.isEmpty()) {
     return this;
   } else {
     return that.foldLeft(this, (map, entry) -> !map.containsKey(entry._1) ? map.put(entry) : map);
   }
 }
Example #9
0
 public Map<String, String> getResponseHeaders(Request request) {
   String fallbackAllowOriginValue = allowedOrigins.mkString(",");
   return HashMap.of(
       ACCESS_CONTROL_ALLOW_ORIGIN_HEADER,
           isAllowed(request)
               ? request.getHeader(ORIGIN_HEADER).getOrElse(fallbackAllowOriginValue)
               : fallbackAllowOriginValue,
       ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER, "true",
       ACCESS_CONTROL_EXPOSE_HEADERS_HEADER, "*",
       ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*",
       ACCESS_CONTROL_MAX_AGE_HEADER, "1800",
       ACCESS_CONTROL_ALLOW_METHODS_HEADER, "*");
 }
Example #10
0
 @Override
 public HashMap<K, V> filter(Predicate<? super Tuple2<K, V>> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   return foldLeft(
       HashMap.<K, V>empty(),
       (acc, entry) -> {
         if (predicate.test(entry)) {
           return acc.put(entry);
         } else {
           return acc;
         }
       });
 }
Example #11
0
 @Override
 public <U, W> HashMap<U, W> flatMap(
     BiFunction<
             ? super K,
             ? super V,
             ? extends java.lang.Iterable<? extends Tuple2<? extends U, ? extends W>>>
         mapper) {
   Objects.requireNonNull(mapper, "mapper is null");
   return foldLeft(
       HashMap.<U, W>empty(),
       (acc, entry) -> {
         for (Tuple2<? extends U, ? extends W> mappedEntry : mapper.apply(entry._1, entry._2)) {
           acc = acc.put(mappedEntry);
         }
         return acc;
       });
 }
Example #12
0
 @Override
 public <U extends V> HashMap<K, V> merge(
     Map<? extends K, U> that, BiFunction<? super V, ? super U, ? extends V> collisionResolution) {
   Objects.requireNonNull(that, "that is null");
   Objects.requireNonNull(collisionResolution, "collisionResolution is null");
   if (isEmpty()) {
     return HashMap.ofAll(that);
   } else if (that.isEmpty()) {
     return this;
   } else {
     return that.foldLeft(
         this,
         (map, entry) -> {
           final K key = entry._1;
           final U value = entry._2;
           final V newValue =
               map.get(key).map(v -> (V) collisionResolution.apply(v, value)).orElse(value);
           return map.put(key, newValue);
         });
   }
 }
Example #13
0
 @Override
 public HashMap<K, V> takeWhile(Predicate<? super Tuple2<K, V>> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   final HashMap<K, V> taken = HashMap.ofAll(iterator().takeWhile(predicate));
   return taken.length() == length() ? this : taken;
 }
Example #14
0
 @Override
 public HashMap<K, V> scan(
     Tuple2<K, V> zero,
     BiFunction<? super Tuple2<K, V>, ? super Tuple2<K, V>, ? extends Tuple2<K, V>> operation) {
   return HashMap.ofAll(scanLeft(zero, operation));
 }
Example #15
0
 @Override
 public HashMap<K, V> clear() {
   return HashMap.empty();
 }
Example #16
0
 @Override
 public HashMap<K, V> distinctBy(Comparator<? super Tuple2<K, V>> comparator) {
   Objects.requireNonNull(comparator, "comparator is null");
   return HashMap.ofAll(iterator().distinctBy(comparator));
 }
Example #17
0
 @Override
 public HashMap<K, V> dropWhile(Predicate<? super Tuple2<K, V>> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   return HashMap.ofAll(iterator().dropWhile(predicate));
 }
Example #18
0
 @Override
 public <U> HashMap<K, V> distinctBy(Function<? super Tuple2<K, V>, ? extends U> keyExtractor) {
   Objects.requireNonNull(keyExtractor, "keyExtractor is null");
   return HashMap.ofAll(iterator().distinctBy(keyExtractor));
 }