-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
ec0266d
commit 70537a3
Showing
17 changed files
with
664 additions
and
641 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 37 additions & 37 deletions
74
java/hello-world/src/main/java/software/amazon/awscdk/examples/HelloJavaStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
122 changes: 62 additions & 60 deletions
122
java/hello-world/src/main/java/software/amazon/awscdk/examples/SinkQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.