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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:51:09+00:00 2026-05-15T05:51:09+00:00

If this has already been asked, please link and close this one. I’m currently

  • 0

If this has already been asked, please link and close this one.


I’m currently prototyping a design for a simplified API of a certain another API that’s a lot more complex (and potentially dangerous) to use.

Considering the related somewhat complex object creation I decided to use static factory methods to simplify the API and I currently have the following which works as expected:

public class Glue<T> {

    private List<Type<T>> types;

    private Glue() { types = new ArrayList<Type<T>>(); }
    private static class Type<T> {
        private T value;
        /* some other properties, omitted for simplicity */
        public Type(T value) { this.value = value; }
    }

    public static <T> Glue<T> glueFactory(String name, T first, T second) {
        Glue<T> g = new Glue<T>();
        Type<T> firstType = new Glue.Type<T>(first);
        Type<T> secondType = new Glue.Type<T>(second);

        g.types.add(firstType);
        g.types.add(secondType);
        /* omitted complex stuff */
        return g;
    }
}

As said, this works as intended. When the API user (=another developer) types Glue<Horse> strongGlue = Glue.glueFactory("2HP", new Horse(), new Horse()); he gets exactly what he wanted.

What I’m missing is that how do I enforce that Horse – or whatever is put into the factory method – always implements both Serializable and Comparable? Simply adding them to factory method’s signature using <T extends Comparable<T> & Serializable> doesn’t necessarily enforce this rule in all cases, only when this simplified API is used. That’s why I’d like to add them to the class’ definition and then modify the factory method accordingly.

PS: No horses (and definitely no ponies!) were harmed in writing of this question.

  • 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-15T05:51:10+00:00Added an answer on May 15, 2026 at 5:51 am

    You’re actually very close. Here’s my solution

    public class Glue<T extends Serializable & Comparable<T>> {
    
      private List<Type<T>> types;
    
      private Glue() { types = new ArrayList<Type<T>>(); }
      private static class Type<T> {
          private T value;
          /* some other properties, omitted for simplicity */
          public Type(T value) { this.value = value; }
      }
    
      public static <V extends Serializable & Comparable<V>> Glue<V> glueFactory(
          String name, V first, V second) {
          Glue<V> g = new Glue<V>();
          Type<V> firstType = new Glue.Type<V>(first);
          Type<V> secondType = new Glue.Type<V>(second);
    
          g.types.add(firstType);
          g.types.add(secondType);
          /* omitted complex stuff */
          return g;
      }
    }
    
    public class Horse implements Serializable, Comparable<Horse> {
      private static final long serialVersionUID = 1156763996034008367L;
    
      @Override
      public int compareTo(Horse o) {
         return 0;
      }      
    }
    
    public class Cat  { }
    
    public static void main(String[] args) {
      Glue<Horse> gh = Glue.glueFactory("2HP", new Horse(), new Horse());
      Glue<Cat> gc = Glue.glueFactory("2C", new Cat(), new Cat()); // <--- Does not compile, as requested!!
    }
    

    This program is different from your code in these aspects:

    • I renamed the type parameter T (of the glueFactory() method) to V. Given that the method is static its type parameter has no relation to the containing class type parameter and thus the code is much clearer when the two have different names.
    • I added the bounds (implements Serializable & Comparable ...) to the declaration of both V and T

    As expected the resulting program compiles Glue.glueFactory("2HP", new Horse(), new Horse()); but rejects Glue.glueFactory("2C", new Cat(), new Cat());

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

Sidebar

Related Questions

No related questions found

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.