0

I am trying to change the Map value Object to List Value Object, so that a single key can store multiple values. Please find the code snippet.

  protected Subscriber updateSubscriberAttributes(Subscriber subscriber, Collection<SubscriberAttributeDto> subscriberAttributesCollection)
        throws ErrorException {
    // Do we have any custom attributes to store?
    if ((subscriberAttributesCollection != null) && !subscriberAttributesCollection.isEmpty()) {
        // Yes! Convert to a Map first.
        Map<String, Object> subscriberAttributesMap = new HashMap<String, Object>(subscriberAttributesCollection.size());
        for (SubscriberAttributeDto subscriberAttribute : subscriberAttributesCollection) {
            // Convert the input attribute value to a database-appropriate value
            SubscriberAttributeMetadata attrMetadata = subscriberAttribute.getAttributeMetadata();
            if (attrMetadata != null) {
                subscriberAttributesMap.put(attrMetadata.getColumnName(),
                        attrMetadata.convertToDatabaseValue((String) subscriberAttribute.getValue()));
            }
        }

        // Perform the update
        return updateSubscriberAttributes(subscriber, subscriberAttributesMap);
    } else {
        return subscriber;
    }
}

Sorry the code is little messy. So the trouble I am facing here is, If I change the declaration of subscribersAttributeMap to Map<String,List<Object>> I have to change the method declaration and throwing me so many errors. I did try it from long time.

2
  • That's part of the refactoring as well. Commented Nov 26, 2013 at 15:15
  • Thanks for refactoring Luiggi.. Commented Nov 26, 2013 at 15:19

3 Answers 3

1
protected Subscriber updateSubscriberAttributes(Subscriber subscriber, Collection<SubscriberAttributeDto> subscriberAttributesCollection) throws ErrorException {
    // Do we have any custom attributes to store?
    if ((subscriberAttributesCollection != null) && !subscriberAttributesCollection.isEmpty()) {
        // Yes! Convert to a Map first.
        Map<String, List<Object>> subscriberAttributesMap = new HashMap<String, List<Object>>(subscriberAttributesCollection.size());
        for (SubscriberAttributeDto subscriberAttribute : subscriberAttributesCollection) {
            // Convert the input attribute value to a database-appropriate value
            SubscriberAttributeMetadata attrMetadata = subscriberAttribute.getAttributeMetadata();
            if (attrMetadata != null) {
                //
                String columnName = attrMetadata.getColumnName();
                List<Object> list = subscriberAttributesMap.get(columnName);
                if (list == null) {
                    list = new ArrayList<Object>();
                    subscriberAttributesMap.put(columnName, list);
                }
                //
                list.add(attrMetadata.convertToDatabaseValue((String) subscriberAttribute.getValue()));
            }
        }

        // Perform the update
        return updateSubscriberAttributes(subscriber, subscriberAttributesMap);
    } else {
        return subscriber;
    }
}

protected Subscriber updateSubscriberAttributes(Subscriber subscriber,  Map<String, List<Object>> map) throws ErrorException {
    // do some logic hiere ...
    return new Subscriber();
}
Sign up to request clarification or add additional context in comments.

Comments

1

probably something like:

 Map<String,List<Object>>subscriberAttributesMap = new HashMap<String, List<Object>>(subscriberAttributesCollection.size());
                for (SubscriberAttributeDto subscriberAttribute : subscriberAttributesCollection) {
                    // Convert the input attribute value to a database-appropriate value
                    SubscriberAttributeMetadata attrMetadata = subscriberAttribute.getAttributeMetadata();
                    if (attrMetadata != null) {
                        List<Object> lst = subscriberAttributesMap.get(attrMetadata.getColumnName());
                        if(lst == null){
                            lst = new ArrayList<Object>();
                            subscriberAttributesMap.put(attrMetadata.getColumnName(), lst);
                        }
                        lst.add(attrMetadata.convertToDatabaseValue((String) subscriberAttribute.getValue()));

                    }
                }

3 Comments

So the trouble I am facing here is, If I change the declaration of subscribersAttributeMap to Map<String,List<Object>> I have to change the method declaration and throwing me so many errors this is the main problem.
i can't fix his whole program then :(
Modifying the methods that use this Map<Key, Value> is part of the changes to accomplish as well. Also, the fact to have Map<Key, Object> looks like code smell...
0

You may want using/re-implementing/copying Guava's Multimap, which has the functionality you need and the interface similar to java.util.Map. So this would minimize changes in the code. Example from their documentation:

ListMultimap<String, Object> multimap = ArrayListMultimap.create();

for (President pres : US_PRESIDENTS_IN_ORDER) {
    multimap.put(pres.firstName(), pres.lastName());
}

for (String firstName : multimap.keySet()) {
    List<Object> lastNames = multimap.get(firstName);
    out.println(firstName + ": " + lastNames);
}

Multimap can be converted back to a Map:

// java.util.Map representation
Map<String,Collection<Object>> stringCollectionMap = multimap.asMap();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.