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

  • SEARCH
  • Home
  • 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 6789667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:37:54+00:00 2026-05-26T17:37:54+00:00

I’m trying to build an SDK using an adapter pattern. Here’s what I’ve got

  • 0

I’m trying to build an SDK using an adapter pattern. Here’s what I’ve got so far:

interface Adapter<T> {
  void doWork(WorkUnit<T> unit);
  Class<T> getT();
}

class WorkUnit<T> {
  public int getId() { ... }
  public T getExtras() { ... }
}

class OldWorkUnit {
  public <T> void setExtra(T data) { /* Store data in Map<Class, Object> */ }
  public <T> WorkUnit<T> toNewWorkUnit(Adapter<T> adapter) { /* Map.get(adapter.getT()) */ }
}

There’s a good amount of generics in there, but I can’t know T at compile time, and there may be multiple Adapters, all with different types of T. This is meant to be exposed to third parties, so I also need as little implementation as possible in the interface implementation, and it has to be an interface (no abstract class).

Now I want to take this and call doWork with a WorkUnit. My first pass at the code looks like this:

class FooAdapter implements Adapter<FooWorkUnit> {
  ...
}

OldWorkUnit w = new OldWorkUnit();
w.setExtra(new FooWorkUnit());

Adapter<?> a = new FooAdapter();
WorkUnit<?> unit = w.toNewWorkUnit(a);

a.doWork(unit);

Uh oh, that doesn’t compile:

The method doWork(WorkUnit<capture#2-of ?>) in the type Adapter<capture#2-of ?> 
is not applicable for the arguments (WorkUnit<capture#4-of ?>)

I know that the WorkUnit generic argument is the same type as the Adapter.doWork() generic argument, but without knowing the type I can’t cast it appropriately.

So is there a way to work my way through this?

  • 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-05-26T17:37:55+00:00Added an answer on May 26, 2026 at 5:37 pm

    Why do you wildcard the template class here?:

    Adapter<?> a = new FooAdapter();
    WorkUnit<?> unit = w.toNewWorkUnit(a);
    

    If you do this:

    Adapter<FooWorkUnit> a = new FooAdapter();
    WorkUnit<FooWorkUnit> unit = w.toNewWorkUnit(a);
    

    Doesn’t that preserve what you need?

    Now the compiler knows that FooWorkUnit is the common denominator, so to speak.

    Edit: OK point taken about the runtime variability. How about strongly-typing the method that does the work, so that the wildcards are eliminated, but still consistent:

    @SuppressWarnings("unchecked")
    public <X> void prepareArgsAndDoTheWork() {
        OldWorkUnit w = new OldWorkUnit();
        w.setExtra(new FooWorkUnit());
    
        Adapter<X> a = (Adapter<X>) ... ; // Obtain the Adapter by reflection etc 
        WorkUnit<X> unit = w.toNewWorkUnit(a);
        a.doWork(unit);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.