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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:25:51+00:00 2026-06-16T00:25:51+00:00

Here we have multiple custom query-DSLs that use related grammar. I am creating to

  • 0

Here we have multiple custom query-DSLs that use related grammar. I am creating to create an AbstractBuilder so that all the commonality can be written in one place. The problem is that is causes issues with method-chaining. When I try to chain from a method in to AbstractBuilder so a subclass, it doesn’t work without casting.

With these classes:

class AbstractBuilder{
    protected final StringBuilder bldr = new StringBuilder();

    AbstractBuilder addValue( String name, String value ){
        bldr.append( name ).append( '=' ).append( value )append( ',' );
        return this;
    }

    String toString(){
        return bldr.toString();
    }
}

class IntBuilder extends AbstractBuilder{

    IntBuilder addValue( String name, int value ){
        bldr.append( name ).append( '=' ).append( value )append( ',' );
        return this;
    }
}

This works

new IntBuilder().addValue( "age", 12 ).addValue( "name", "Bruce" ).toString();` but `new IntBuilder().addValue( "name", "Bruce" ).addValue( "age", 12 ).toString();

doesn’t unless you make an ugly cast like:

((IntBuilder) (new IntBuilder().addValue( "name", "Bruce" ))).addValue( "age", 12 ).toString();

Now I guess I could override each methods and implement them with calls to their parents (via super.addValue( name, value );), but that is really ugly.

How else can I get every method to return the current class and not the class on which it was defined?

  • 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-16T00:25:51+00:00Added an answer on June 16, 2026 at 12:25 am

    You could achieve this using generics.

    class BaseBuilder<B extends BaseBuilder>{
        protected final StringBuilder bldr = new StringBuilder();
        private Class<B> builderImplementationClass;
    
        protected BaseBuilder( Class<B> builderImpl ){
            builderImplementationClass = builderImpl;
    
            if( this.getClass() != builderImpl )
                throw new IllegalStateException( "Should match." );
        }
    
        B addValue( String name, String value ){
            bldr.append( name ).append( '=' ).append( value )append( ',' );
            return builderImplementationClass.cast( this );
        }
    
        String toString(){
            return bldr.toString();
        }
    }
    
    class IntBuilder extends AbstractBuilder<IntBuilder>{
    
        public IntBuilder(){
            super( IntBuilder.class );
        }
    
        IntBuilder addValue( String name, int value ){
            bldr.append( name ).append( '=' ).append( value )append( ',' );
            return this;
        }
    }
    

    Basically you make all your methods return a value of type B and make sure that B is set to your current Implementation

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

Sidebar

Related Questions

I have a T4 that is generating multiple .html files. After creating them all
I have tried solving this problem by posting other related questions here that focused
I need to create a custom dialog fragment that works with all Android api
here is briefly the problem I'm facing: I have built a custom Content Query
I have a form made in JSP, here I have multiple buttons - Approve,
Here's a scenario... I have a column. In the column i have multiple divs
Here's what I am working on. At my website I have multiple processes with
Here is what I have- How I can include multiple keys and their values
I have a project here the goal is to merge multiple Access DB's into
I have a show/hide toggle working well in multiple instances (thanks to help here

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.