Skip to content

Commit

Permalink
feat: Format Java Code (#199)
Browse files Browse the repository at this point in the history
Formats existing java examples using google-java-format. Adds 'style and
formatting' section to CONTRIBUTING.md regarding java code formatting.

Fixes #198
  • Loading branch information
MrArnoldPalmer authored and mergify[bot] committed Dec 4, 2019
1 parent ec0266d commit 70537a3
Show file tree
Hide file tree
Showing 17 changed files with 664 additions and 641 deletions.
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ Create a directory with a name that is descriptive of the resource type or workf
}
```

## Style and Formatting

We strive to keep examples consistent in style and formatting. This hopefully makes navigating examples easier for users. Since the examples span different languages, we try to keep example code as idiomatic as possible.

New guidelines for various languages will be added as we define them.

### Java
1. Use builders wherever possible. Some classes are unable to have builders generated in JSII but for those that do, prefer them over constructing by hand.
2. Format your code with [google-java-format](https://github.com/google/google-java-format). If you're using visual studio code, [see this comment](https://github.com/redhat-developer/vscode-java/issues/419#issuecomment-360820321) to get a formatter task setup.

## Finding contributions to work on
Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any ['help wanted'](https://github.com/aws-samples/aws-cdk-examples/labels/help%20wanted) issues is a great place to start.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import software.amazon.awscdk.core.App;

public class HelloJavaApp {
public static void main(final String[] args) {
App app = new App();
public static void main(final String[] args) {
App app = new App();

new HelloJavaStack(app, "hello-cdk");
new HelloJavaStack(app, "hello-cdk");

app.synth();
}
app.synth();
}
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
package software.amazon.awscdk.examples;

import software.amazon.awscdk.core.Stack;
import java.util.Collections;
import java.util.List;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.services.autoscaling.AutoScalingGroup;
import software.amazon.awscdk.services.ec2.AmazonLinuxImage;
import software.amazon.awscdk.services.ec2.InstanceType;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.sns.Topic;

import java.util.Collections;
import java.util.List;

/**
* Hello, CDK for Java!
*/
/** Hello, CDK for Java! */
class HelloJavaStack extends Stack {
public HelloJavaStack(final Construct parent, final String name) {
super(parent, name);

Vpc vpc = Vpc.Builder.create(this, "VPC").build();
public HelloJavaStack(final Construct parent, final String name) {
super(parent, name);

MyAutoScalingGroupProps autoScalingGroupProps = new MyAutoScalingGroupProps();
autoScalingGroupProps.vpc = vpc;
Vpc vpc = Vpc.Builder.create(this, "VPC").build();

int topicCount = 5;
MyAutoScalingGroupProps autoScalingGroupProps = new MyAutoScalingGroupProps();
autoScalingGroupProps.vpc = vpc;

SinkQueue sinkQueue = new SinkQueue(this, "MySinkQueue", SinkQueueProps.builder().withRequiredTopicCount(5).build());
int topicCount = 5;

for (int i = 0; i < topicCount; ++i) {
sinkQueue.subscribe(new Topic(this, "Topic" + (i+1)));
}
SinkQueue sinkQueue =
new SinkQueue(
this, "MySinkQueue", SinkQueueProps.builder().withRequiredTopicCount(5).build());

new MyAutoScalingGroup(this, "MyAutoScalingGroup", autoScalingGroupProps);
for (int i = 0; i < topicCount; ++i) {
sinkQueue.subscribe(new Topic(this, "Topic" + (i + 1)));
}

static class MyAutoScalingGroupProps {
public Vpc vpc;
new MyAutoScalingGroup(this, "MyAutoScalingGroup", autoScalingGroupProps);
}

static class MyAutoScalingGroupProps {
public Vpc vpc;
}

static class MyAutoScalingGroup extends Construct {
MyAutoScalingGroup(
final Construct parent, final String name, final MyAutoScalingGroupProps props) {
super(parent, name);

AutoScalingGroup.Builder.create(this, "Compute")
.instanceType(new InstanceType("t2.micro"))
.machineImage(new AmazonLinuxImage())
.vpc(props.vpc)
.build();
}

static class MyAutoScalingGroup extends Construct {
MyAutoScalingGroup(final Construct parent, final String name, final MyAutoScalingGroupProps props) {
super(parent, name);

AutoScalingGroup.Builder.create(this, "Compute")
.instanceType(new InstanceType("t2.micro"))
.machineImage(new AmazonLinuxImage())
.vpc(props.vpc)
.build();
}

@Override
public List<String> validate() {
System.err.println("Validating MyAutoScalingGroup...");
return Collections.emptyList();
}
@Override
public List<String> validate() {
System.err.println("Validating MyAutoScalingGroup...");
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,81 +1,83 @@
package software.amazon.awscdk.examples;

import java.util.Arrays;
import java.util.List;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.services.sns.Topic;
import software.amazon.awscdk.services.sns.subscriptions.SqsSubscription;
import software.amazon.awscdk.services.sqs.Queue;
import software.amazon.awscdk.services.sqs.QueueProps;

import java.util.Arrays;
import java.util.List;

/**
* A sink queue is a queue aggregates messages published to any number of SNS topics.
*/
/** A sink queue is a queue aggregates messages published to any number of SNS topics. */
public class SinkQueue extends Construct {
private final Queue queue;
private final int expectedTopicCount;
private final Queue queue;
private final int expectedTopicCount;

private int actualTopicCount = 0;
private int actualTopicCount = 0;

/**
* Defines a SinkQueue.
*
* @param parent Parent construct
* @param name Logical name
* @param props Props
*/
public SinkQueue(final Construct parent, final String name, SinkQueueProps props) {
super(parent, name);
/**
* Defines a SinkQueue.
*
* @param parent Parent construct
* @param name Logical name
* @param props Props
*/
public SinkQueue(final Construct parent, final String name, SinkQueueProps props) {
super(parent, name);

// ensure props is non-null
props = props != null ? props : SinkQueueProps.builder().build();
// ensure props is non-null
props = props != null ? props : SinkQueueProps.builder().build();

// defaults
QueueProps queueProps = props.getQueueProps();
this.expectedTopicCount = props.getRequiredTopicCount() != null ? props.getRequiredTopicCount().intValue() : 0;
// defaults
QueueProps queueProps = props.getQueueProps();
this.expectedTopicCount =
props.getRequiredTopicCount() != null ? props.getRequiredTopicCount().intValue() : 0;

// WORKAROUND: https://github.com/awslabs/aws-cdk/issues/157
if (queueProps == null) {
queueProps = QueueProps.builder().build();
}

this.queue = new Queue(this, "Resource", queueProps);
// WORKAROUND: https://github.com/awslabs/aws-cdk/issues/157
if (queueProps == null) {
queueProps = QueueProps.builder().build();
}

/**
* Defines a SinkQueue with default props.
* @param parent Parent construct
* @param name Logical name
*/
public SinkQueue(final Construct parent, final String name) {
this(parent, name, null);
}
this.queue = new Queue(this, "Resource", queueProps);
}

/**
* Subscribes this queue to receive messages published to the specified topics.
*
* @param topics The topics to subscribe to
*/
public void subscribe(final Topic... topics) {
for (Topic topic: topics) {
if (expectedTopicCount != 0 && actualTopicCount >= expectedTopicCount) {
throw new RuntimeException("Cannot add more topics to the sink. Maximum topics is configured to " + this.expectedTopicCount);
}
topic.addSubscription(new SqsSubscription(this.queue));
actualTopicCount++;
}
}
/**
* Defines a SinkQueue with default props.
*
* @param parent Parent construct
* @param name Logical name
*/
public SinkQueue(final Construct parent, final String name) {
this(parent, name, null);
}

@Override
public List<String> validate() {
if (actualTopicCount < expectedTopicCount) {
return Arrays.asList(
"There are not enough subscribers to the sink. Expecting " +
this.expectedTopicCount +
", actual is " + this.actualTopicCount);
}
/**
* Subscribes this queue to receive messages published to the specified topics.
*
* @param topics The topics to subscribe to
*/
public void subscribe(final Topic... topics) {
for (Topic topic : topics) {
if (expectedTopicCount != 0 && actualTopicCount >= expectedTopicCount) {
throw new RuntimeException(
"Cannot add more topics to the sink. Maximum topics is configured to "
+ this.expectedTopicCount);
}
topic.addSubscription(new SqsSubscription(this.queue));
actualTopicCount++;
}
}

return super.validate();
@Override
public List<String> validate() {
if (actualTopicCount < expectedTopicCount) {
return Arrays.asList(
"There are not enough subscribers to the sink. Expecting "
+ this.expectedTopicCount
+ ", actual is "
+ this.actualTopicCount);
}

return super.validate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,61 @@

import software.amazon.awscdk.services.sqs.QueueProps;

/**
* Props for {@link SinkQueue}.
*/
/** Props for {@link SinkQueue}. */
public class SinkQueueProps {
private QueueProps queueProps;
private Number requiredTopicCount;

/** @return A builder for {@link SinkQueueProps}. */
public static SinkQueuePropsBuilder builder() {
return new SinkQueuePropsBuilder();
}

/**
* The exact number of topics required to subscribe to the queue. You must call {@link
* SinkQueue::subscribe} to subscribe topics to this queue.
*
* @default 0
*/
public Number getRequiredTopicCount() {
return requiredTopicCount;
}

/**
* Props for the queue itself
*
* @default See {@link software.amazon.awscdk.sqs.Queue} defaults
*/
public QueueProps getQueueProps() {
return queueProps;
}

/** Builder for {@link SinkQueue}. */
public static final class SinkQueuePropsBuilder {
private QueueProps queueProps;
private Number requiredTopicCount;

/**
* @return A builder for {@link SinkQueueProps}.
*/
public static SinkQueuePropsBuilder builder() {
return new SinkQueuePropsBuilder();
}
private SinkQueuePropsBuilder() {}

/**
* The exact number of topics required to subscribe to the queue.
* You must call {@link SinkQueue::subscribe} to subscribe topics to this queue.
* @default 0
*/
public Number getRequiredTopicCount() {
return requiredTopicCount;
public static SinkQueuePropsBuilder aSinkQueueProps() {
return new SinkQueuePropsBuilder();
}

/**
* Props for the queue itself
* @default See {@link software.amazon.awscdk.sqs.Queue} defaults
*/
public QueueProps getQueueProps() {
return queueProps;
public SinkQueuePropsBuilder withQueueProps(QueueProps queueProps) {
this.queueProps = queueProps;
return this;
}

/**
* Builder for {@link SinkQueue}.
*/
public static final class SinkQueuePropsBuilder {
private QueueProps queueProps;
private Number requiredTopicCount;

private SinkQueuePropsBuilder() {
}

public static SinkQueuePropsBuilder aSinkQueueProps() {
return new SinkQueuePropsBuilder();
}

public SinkQueuePropsBuilder withQueueProps(QueueProps queueProps) {
this.queueProps = queueProps;
return this;
}

public SinkQueuePropsBuilder withRequiredTopicCount(Number requiredTopicCount) {
this.requiredTopicCount = requiredTopicCount;
return this;
}
public SinkQueuePropsBuilder withRequiredTopicCount(Number requiredTopicCount) {
this.requiredTopicCount = requiredTopicCount;
return this;
}

public SinkQueueProps build() {
SinkQueueProps sinkQueueProps = new SinkQueueProps();
sinkQueueProps.requiredTopicCount = this.requiredTopicCount;
sinkQueueProps.queueProps = this.queueProps;
return sinkQueueProps;
}
public SinkQueueProps build() {
SinkQueueProps sinkQueueProps = new SinkQueueProps();
sinkQueueProps.requiredTopicCount = this.requiredTopicCount;
sinkQueueProps.queueProps = this.queueProps;
return sinkQueueProps;
}
}
}
Loading

0 comments on commit 70537a3

Please sign in to comment.