Left Pad lambda

This commit is contained in:
Przemek Grondek 2018-02-21 22:49:00 +01:00
parent c922cf86a0
commit e6a03b861d
6 changed files with 86 additions and 0 deletions

View File

@ -11,6 +11,7 @@ subprojects {
} }
dependencies { dependencies {
testCompile 'junit:junit:4.12'
} }
} }

View File

@ -0,0 +1,3 @@
dependencies {
compile 'com.amazonaws:aws-lambda-java-core:1.2.0'
}

View File

@ -0,0 +1,24 @@
package pl.grondek.aws.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LeftPadFunction implements RequestHandler<PadRequest, String> {
@Override
public String handleRequest(PadRequest request, Context context) {
if (request.getSize() < request.getString().length()) {
return request.getString();
} else {
final char[] input = request.getString().toCharArray();
char[] output = new char[request.getSize()];
final int paddingSize = request.getSize() - input.length;
for (int i = 0; i < paddingSize; i++) {
output[i] = request.getCharacter();
}
System.arraycopy(input, 0, output, paddingSize, input.length);
return String.valueOf(output);
}
}
}

View File

@ -0,0 +1,31 @@
package pl.grondek.aws.lambda;
public class PadRequest {
private String string;
private int size;
private char character;
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public char getCharacter() {
return character;
}
public void setCharacter(char character) {
this.character = character;
}
}

View File

@ -0,0 +1,26 @@
package pl.grondek.aws.lambda;
import org.junit.Assert;
import org.junit.Test;
public class LeftPadFunctionTest {
@Test
public void testFunction() {
// setup
LeftPadFunction leftPadFunction = new LeftPadFunction();
// given
PadRequest request = new PadRequest();
request.setCharacter(' ');
request.setSize(10);
request.setString("Test");
// when
final String response = leftPadFunction.handleRequest(request, null);
// then
Assert.assertEquals(response.length(), 10);
Assert.assertEquals(response, " Test");
}
}

View File

@ -1,2 +1,3 @@
rootProject.name = 'aws' rootProject.name = 'aws'
include "left-pad-lambda"