Left Pad lambda
This commit is contained in:
parent
c922cf86a0
commit
e6a03b861d
@ -11,6 +11,7 @@ subprojects {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
||||
|
||||
}
|
3
left-pad-lambda/build.gradle
Normal file
3
left-pad-lambda/build.gradle
Normal file
@ -0,0 +1,3 @@
|
||||
dependencies {
|
||||
compile 'com.amazonaws:aws-lambda-java-core:1.2.0'
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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");
|
||||
}
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
rootProject.name = 'aws'
|
||||
|
||||
include "left-pad-lambda"
|
Loading…
Reference in New Issue
Block a user