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 8927697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:16:29+00:00 2026-06-15T08:16:29+00:00

Let’s say I have a manufacturing scheduling system, which is made up of four

  • 0

Let’s say I have a manufacturing scheduling system, which is made up of four parts:

  • There are factories that can manufacture a certain type of product and know if they are busy:

    interface Factory<ProductType> {
        void buildProduct(ProductType product);
        boolean isBusy();
    }
    
  • There is a set of different products, which (among other things) know in which factory they are built:

    interface Product<ActualProductType extends Product<ActualProductType>> {
        Factory<ActualProductType> getFactory();
    }
    
  • Then there is an ordering system that can generate requests for products to be built:

    interface OrderSystem {
        Product<?> getNextProduct();
    }
    
  • Finally, there’s a dispatcher that grabs the orders and maintains a work-queue for each factory:

    class Dispatcher {
        Map<Factory<?>, Queue<Product<?>>> workQueues
                          = new HashMap<Factory<?>, Queue<Product<?>>>();
    
        public void addNextOrder(OrderSystem orderSystem) {
            Product<?> nextProduct = orderSystem.getNextProduct();
            workQueues.get(nextProduct.getFactory()).add(nextProduct);
        }
    
        public void assignWork() {
            for (Factory<?> factory: workQueues.keySet())
                if (!factory.isBusy())
                    factory.buildProduct(workQueues.get(factory).poll());
        }
    }
    

Disclaimer: This code is merely an example and has several bugs (check if factory exists as a key in workQueues missing, …) and is highly non-optimal (could iterate over entryset instead of keyset, …)

Now the question:

The last line in the Dispatcher (factory.buildProduct(workqueues.get(factory).poll());) throws this compile-error:

The method buildProduct(capture#5-of ?) in the type Factory<capture#5-of ?> is not applicable for the arguments (Product<capture#7-of ?>)

I’ve been racking my brain over how to fix this in a type-safe way, but my Generics-skills have failed me here…

Changing it to the following, for example, doesn’t help either:

public void assignWork() {
    for (Factory<?> factory: workQueues.keySet())
        if (!factory.isBusy()) {
            Product<?> product = workQueues.get(factory).poll();
            product.getFactory().buildProduct(product);
        }
}

Even though in this case it should be clear that this is ok…

I guess I could add a “buildMe()” function to every Product that calls factory.buildProduct(this), but I have a hard time believing that this should be my most elegant solution.

Any ideas?

EDIT:

A quick example for an implementation of Product and Factory:

class Widget implements Product<Widget> {
    public String color;

    @Override
        public Factory<Widget> getFactory() {
            return WidgetFactory.INSTANCE;
    }
}

class WidgetFactory implements Factory<Widget> {
    static final INSTANCE = new WidgetFactory();

    @Override
    public void buildProduct(Widget product) {
        // Build the widget of the given color (product.color)
    }

    @Override
    public boolean isBusy() {
        return false; // It's really quick to make this widget
    }
}
  • 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-15T08:16:30+00:00Added an answer on June 15, 2026 at 8:16 am

    Got it! Thanks to meriton who answered this version of the question:

    How to replace run-time instanceof check with compile-time generics validation

    I need to baby-step the compiler through the product.getFactory().buildProduct(product)-part by doing this in a separate generic function. Here are the changes that I needed to make to the code to get it to work (what a mess):

    • Be more specific about the OrderSystem:

      interface OrderSystem {
          <ProductType extends Product<ProductType>> ProductType getNextProduct();
      }
      
    • Define my own, more strongly typed queue to hold the products:

      @SuppressWarnings("serial")
      class MyQueue<T extends Product<T>> extends LinkedList<T> {};
      
    • And finally, changing the Dispatcher to this beast:

      class Dispatcher {
          Map<Factory<?>, MyQueue<?>> workQueues = new HashMap<Factory<?>, MyQueue<?>>();
      
          @SuppressWarnings("unchecked")
          public <ProductType extends Product<ProductType>> void addNextOrder(OrderSystem orderSystem) {
              ProductType nextProduct = orderSystem.getNextProduct();
              MyQueue<ProductType> myQueue = (MyQueue<ProductType>) workQueues.get(nextProduct.getFactory());
              myQueue.add(nextProduct);
          }       
      
          public void assignWork() {
              for (Factory<?> factory: workQueues.keySet())
                  if (!factory.isBusy())
                      buildProduct(workQueues.get(factory).poll());
          }
      
          public <ProductType extends Product<ProductType>> void buildProduct(ProductType product) {
              product.getFactory().buildProduct(product);
          }
      }   
      

    Notice all the generic functions, especially the last one. Also notice, that I can NOT inline this function back into my for loop as I did in the original question.

    Also note, that the @SuppressWarnings("unchecked") annotation on the addNextOrder() function is needed for the typecast of the queue, not some Product object. Since I only call “add” on this queue, which, after compilation and type-erasure, stores all elements simply as objects, this should not result in any run-time casting exceptions, ever. (Please do correct me if this is wrong!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say I have an abstract parent class called shape, and that there are
Let's say, I have an application that access(read/write) the file system(files inside application), Active
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have a method in java, which looks up a user in
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say for a moment that I have the following module in python: class
Let's say I have some text as follows: do this, do that, then this,
Let's say, I have a .NET 2 installed. Can I programmatically install version 4

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.