Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8393773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:53:22+00:00 2026-06-09T19:53:22+00:00

I have a utility method to help use ConcurrentMap.putIfAbsent like this: public static <K,

  • 0

I have a utility method to help use ConcurrentMap.putIfAbsent like this:

public static <K, V> V putIfAbsent(
        ConcurrentMap<K, V> map, K key, Callable<V> task) {
    try {
        V result = map.get(key);
        if (result == null) {
            V value = task.call();
            result = map.putIfAbsent(key, value);
            if (result == null) {
                result = value;
            }
        }
        return result;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

Now I have to deal with such a heterogeneous map:

private ConcurrentMap<Class<?>, List<ClassHandler<?>>> handlerMap;

and a method to add handlers to it:

public <T> void addHandler(Class<T> c, ClassHandler<? super T> handler) {
    putIfAbsent(handlerMap, c, new Callable<List<ClassHandler<?>>>() {
        @Override
        public List<ClassHandler<?>> call() throws Exception {
            return new CopyOnWriteArrayList<>();
        }
    }).add(handler);
}

So far so good, but the problem comes when I try to handle a class, e.g.:

Class<String> c = String.class;
List<ClassHandler<?>> handlers = handlerMap.get(c);
if (handlers != null) {
    for (ClassHandler<?> c : handlers) {
        handler.handle(c);
    }
}

However, this code doesn’t compile:

error: method handle in class ClassHandler<T> cannot be applied to given types;
        handler.handle(c);
  required: Class<CAP#1>
  found: Class<String>
  reason: actual argument Class<String> cannot be converted to Class<CAP#1> by method invocation conversion
  where T is a type-variable:
    T extends Object declared in class ClassHandler
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
1 error

The code compiles if I use handler.handle((Class) c), but I will receive a unchecked warning. Though I can add a @SuppressWarnings("unchecked") annotation, this would be the last choice if there’s no better way.

Any idea about this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:53:23+00:00Added an answer on June 9, 2026 at 7:53 pm

    Right. The type of the map itself doesn’t guarantee that the type parameter of the class and the corresponding class handlers are the same. (No type will express that anyway.) However, you correctly used your own add method to ensure that they match when you put them in. So you can trust that they are the same. In that case, simply cast the handler to the appropriately-parameterized type (you will need to suppress a warning):

    for (ClassHandler<?> handler : handlers) {
        ((ClassHandler<String>)handler).handle(c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a utility method that uses Display Templates to generate HTML: public static
I have a utility method (= a static one) that I call a lot,
I have a static utility method named toSet() which receives objects and returns a
I have a utility method which returns a strongly typed value from an old
i have a string and i need a utility method which looks for bad
I have a static methods class, Utils, that is basically for utility methods, its
I have a utility class that creates & returns a db connection: Public Shared
I'm trying to build a utility method using Linq that will help me with
I have a utility method to find an object's getter method for a particular
Suppose I have a Utility class, public class Utility { private Utility() {} //Don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.