• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Creating Java Interfaces with type parameters (I think?)

Community Beginner ,
Jan 15, 2020 Jan 15, 2020

Copy link to clipboard

Copied

I'm working with the AWS Java 2.0 SDK and I'm trying to implement an async multi part upload, but I'm running into some trouble getting things set up, specifically in creating a BiConsumer Interface.

 

Docs: createMultipartUpload(CreateMultipartUploadRequest createMultipartUploadRequest) 

 

To start this process I have to call a3AsyncInterface.createMultipartUpload() and so I create a CreateMultipartUploadRequest object and pass that object into my call:

 

oCreateMultipartUploadRequest = createObject( 'java', 'software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest' ).builder()
    .bucket( ARGUMENTS.bucket )
    .key( ARGUMENTS.key )
    .contentType( ARGUMENTS.fileMimeType )
    .build();

oCreateMultipartUploadResponse = s3AsyncInterface.createMultipartUpload( oCreateMultipartUploadRequest );

 

 

So this all works great so far. My call returns a response object of type CompletableFuture<CreateMultipartUploadResponse>, but here's where it starts to fall apart.

 

From the CompletableFuture, I want to call it's .whenComplete() method but I can't figure out how to do this successfully.

 

The .whenComplete() method takes an object of type BiConsumer<? super T,? super Throwable> and I can't figure out how to create this object. BiConsumer is a Java Interface, so it doesn't have a constructor. I can create an object no problem, but that doesn't seem to cut it.

 

oBiConsumer = createObject( 'java', 'java.util.function.BiConsumer' );

 

 

I can't call the .accept( t, u ) method on this object because I haven't set it up correctly I think. It's .andThen() method takes another BiConsumer object so I have to figure that out.

 

So what I think I want to be doing is something like this (example is pure Java):

 

BiConsumer< List<Integer>, List<Integer> > equals = ( list1, list2 ) ->
{
	if ( list1.size() != list2.size() ) {
	    System.out.println( 'False' );
	} else {
		for ( int i = 0; i < list1.size(); i++ ) {
			if ( list1.get( i ) != list2.get( i ) ) {
				System.out.println( 'False' );
				return;
			}
		}
		System.out.println( 'True' );
	}
};
equals.accept( lista, listb );

 

 

Not sure if I need to even call the .accept() method right way, or kinda call it when I pass it into .whenComplete(). I may be gonig abotu this specific action all wrong, but thats fine, I can figure that out then when I get there. I still want to figure out how to set up the BiConsumer object properly, this seems to be a popular pattern in Java and I want to be able to work with it.

 

What I would love to be able to do is something like this:

 

oBiConsumer = createObject( 'java', 'java.util.function.BiConsumer<T, U>' );

 

 

But that doesn't seem to be supported, and I'm not sure if there's a different syntax to accomplish the same thing.

 

I get that I can go down the road of creating a dynamic proxy and a custom .jar to create/build this functionality, but is this the only way forward? I would greatly prefer to not have to do that if possible; stinks to maintain.

 

Any suggestions would be appreciated!

 

PS: Is it possible to add a new Topic for something like 'Java' that can be added to this post?

TOPICS
Advanced techniques , Asynchronous

Views

460

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2020 Jun 14, 2020

Copy link to clipboard

Copied

LATEST

What are the CompletableFutures defined by oCreateMultipartUploadResponse? That, I think, is the basis on which to proceed further.

 

Let's say the oCreateMultipartUploadResponse are arrays of MultipartUploads. Then, according to examples from the Amazon AWS documentation, you could proceed as follows:

 

oCreateMultipartUploadResponse
	.whenComplete((multipartUploads, e) => {
                  /* lambda expression body */
		 try {
			 if (!isNull(multipartUploads)) {
			  	arrayeach(multipartUploads, function(value, key) {
				        writeOutput("#key# | #value# <br>");
				});
			 } else {
				 /* Null-object error-handler */
			 }
	         } catch (any e) {
 			/* Error-handler */
			writedump(e);
                 }
        
 	});
 
 multipartUploads.join();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation