Package io.blt.util

Class Ctr

java.lang.Object
io.blt.util.Ctr

public final class Ctr extends Object
Static utility methods for operating on implementations of Collection and Map i.e., Containers.

For methods that return a modification of a passed container, the result will be of the same type if possible. This is accomplished using Obj.newInstanceOf(Object) and its limitations apply.

  • Method Details

    • transformValues

      public static <K, V, R, E extends Throwable> Map<K,R> transformValues(Map<K,V> source, ThrowingFunction<? super V,R,E> transform) throws E
      Returns a new Map containing the entries of source with transform applied to the values.

      If possible, the result is of the same type as the passed source map.

      e.g.,
      
       var scores = Map.of("Louis", 95, "Greg", 92, "Mike", 71, "Phil", 86);
       var grades = Ctr.transformValues(scores, score -> {
           if (score >= 90) {
               return "A";
           } else if (score >= 80) {
               return "B";
           } else if (score >= 70) {
               return "C";
           } else if (score >= 60) {
               return "D";
           } else {
               return "F";
           }
       });
       // grades = Map.of("Louis", "A", "Greg", "A", "Mike", "C", "Phil", "B")
       
      Type Parameters:
      K - map key type
      V - map value type
      R - returned map value type
      E - type of transform throwable
      Parameters:
      source - Map whose values should be transformed
      transform - value transformation function
      Returns:
      a new Map containing the entries of source with transform applied to the values
      Throws:
      E
      See Also:
    • computeIfAbsent

      public static <K, V, E extends Throwable> V computeIfAbsent(Map<K,V> map, K key, ThrowingFunction<? super K,? extends V,E> compute) throws E
      For the specified map, if there is no value for the specified key then compute will be called and the result entered into the map. If a value is present, then it is returned. e.g.,
      
       private final Map<URL, String> cache = new HashMap<>();
      
       public String fetch(URL url) throws IOException {
           return Ctr.computeIfAbsent(cache, url, this::get);
       }
      
       private String get(URL url) throws IOException {
           try (var stream = url.openStream()) {
               return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
           }
       }
       

      If compute returns null, then the map is not modified and null is returned.

      If compute throws, then map is not modified and the exception will bubble up.

      Type Parameters:
      K - map key type
      V - map value type
      E - type of compute throwable
      Parameters:
      map - Map whose value is returned and may be computed
      key - key with which the specified value is to be associated
      compute - the computation function to use if the value is absent
      Returns:
      the existing or computed value associated with the key, or null if the computed value is null
      Throws:
      E - if an exception is thrown by the computation function
      See Also:
      Implementation Note:
      The implementation is equivalent to the following:
       
       if (map.get(key) == null) {
           V value = compute.apply(key);
           if (value != null) {
               map.put(key, value);
           }
       }
       return map.get(key);
       
    • computeIfAbsent

      public static <K, V, E extends Throwable> V computeIfAbsent(Map<K,V> map, K key, ThrowingSupplier<? extends V,E> compute) throws E
      For the specified map, if there is no value for the specified key then compute will be called and the result entered into the map. If a value is present, then it is returned. e.g.,
      
       private final Map<URL, String> cache = new HashMap<>();
      
       public String fetch(URL url) throws IOException {
           return Ctr.computeIfAbsent(cache, url, () -> {
               try (var stream = url.openStream()) {
                   return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
               }
           });
       }
       

      If compute returns null, then the map is not modified and null is returned.

      If compute throws, then map is not modified and the exception will bubble up.

      Type Parameters:
      K - map key type
      V - map value type
      E - type of compute throwable
      Parameters:
      map - Map whose value is returned and may be computed
      key - key with which the specified value is to be associated
      compute - the computation supplier to use if the value is absent
      Returns:
      the existing or computed value associated with the key, or null if the computed value is null
      Throws:
      E - if an exception is thrown by the computation supplier
      See Also:
      Implementation Note:
      The implementation is equivalent to the following:
       
       if (map.get(key) == null) {
           V value = compute.apply(key);
           if (value != null) {
               map.put(key, value);
           }
       }
       return map.get(key);