1
\$\begingroup\$

This post is the continuation of Mapping composition in Java.

This time, I:

  1. Disallowed the null values as the range value.
  2. Simplified the toString method via using the streams.

Code

com.github.coderodde.mapping.Mapping.java:

package com.github.coderodde.mapping;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * This class implements a mapping from a domain set to a range set.
 * 
 * @param <D> the domain element type.
 * @param <R> the range element type.
 * 
 * @author Rodion "rodde" Efremov
 * @version 1.61 (Sep 14, 2023)
 * @since 1.6 (Sep 3, 2023)
 */
public final class Mapping<D, R> {

    final Map<D, R> data = new HashMap<>();
    
    public void map(D domainValue, R rangeValue) {
        checkDomainValueNotYetMapped(domainValue);
        Objects.requireNonNull(rangeValue,
                               "The range value is not allowed to be nul..");
        
        data.put(domainValue, rangeValue);
    }
    
    public boolean isMapped(D domainValue) {
        return data.containsKey(
                Objects.requireNonNull(
                        domainValue,
                        "The input domain value is null."));
    }
    
    public R map(D domainValue) {
        checkDomainValueIsMapped(domainValue);
        return data.get(domainValue);
    }
    
    @Override
    public String toString() {
        return data.entrySet()
                   .stream()
                   .map(Mapping::convertMapEntryToString)
                   .collect(Collectors.joining(", ", "[", "]"));
    }
    
    private static <D, R> 
        String convertMapEntryToString(Map.Entry<D, R> entry) {
        return new StringBuilder()
                    .append("(")
                    .append(entry.getKey())
                    .append(" -> ")
                    .append(entry.getValue())
                    .append(")").toString();
    }
    
    private void checkDomainValueNotYetMapped(D domainValue) {
        if (data.containsKey(
                Objects.requireNonNull(domainValue, "Domain value is null."))) {
            throw new DuplicateDomainValueException(
                    "Trying to map a domain value ["
                            + domainValue
                            + "] twice.");
        }
    }
    
    private void checkDomainValueIsMapped(D domainValue) {
        if (!data.containsKey(domainValue)) {
            throw new DomainValueIsNotMappedException(
                    "Domain value [" + domainValue + "] is not mapped.");
        }
    }
}

Critique request

As always, please tell me whatever comes to mind.

\$\endgroup\$
1
  • \$\begingroup\$ Why dont you use the adapter pattern? Could you fix spotbugs, PMD and checkstyle issues? Are you signing the jar? \$\endgroup\$
    – Grim
    Commented Sep 17, 2023 at 8:17

1 Answer 1

1
\$\begingroup\$

Your Mapping class seems unnecessary, as your compose function shown in the previous post could just use Map types, which are already defined in a standard way within Java. all you're really doing is wrapping the methods of the inner data instance variable which is a map.

\$\endgroup\$
4
  • \$\begingroup\$ True. However, note that rolling my own wrapping class gives me — at the very least— control over the way a map is converted to a string. \$\endgroup\$
    – coderodde
    Commented Sep 20, 2023 at 9:06
  • \$\begingroup\$ Yeah and that could be extracted out into a separate function that takes a map as input and then you wouldn’t have to convert existing maps into Mapping to use your new functionality \$\endgroup\$ Commented Sep 21, 2023 at 14:38
  • \$\begingroup\$ I think it’d be cool to have compose be a variadic function \$\endgroup\$ Commented Sep 21, 2023 at 14:42
  • \$\begingroup\$ Not quite sure how to write variadic functions. Wanna elaborate your answer? \$\endgroup\$
    – coderodde
    Commented Sep 21, 2023 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.