Skip to content

Commit

Permalink
Switch from Date to Instant in generated models
Browse files Browse the repository at this point in the history
Better java.time APIs and improves immutability of model objects.

Fixes #15
  • Loading branch information
dagnir committed Jul 6, 2017
1 parent 3d660cb commit 999f218
Show file tree
Hide file tree
Showing 41 changed files with 174 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

package software.amazon.awssdk.metrics.internal.cloudwatch.spi;

import java.util.Date;
import java.time.Instant;
import java.util.List;
import software.amazon.awssdk.Request;
import software.amazon.awssdk.metrics.spi.MetricType;
Expand Down Expand Up @@ -58,8 +58,8 @@ public static long endTimeMilli(TimingInfo ti) {
return endTimeMilli == null ? System.currentTimeMillis() : endTimeMilli;
}

public static Date endTimestamp(TimingInfo ti) {
return new Date(endTimeMilli(ti));
public static Instant endTimestamp(TimingInfo ti) {
return Instant.ofEpochMilli(endTimeMilli(ti));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;

import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -32,7 +33,7 @@ public void cloneMetricDatum() {
.metricName("MetricName")
.statisticValues(StatisticSet.builder().maximum(100.0)
.minimum(10.0).sampleCount(12.34).sum(99.9).build())
.timestamp(new Date())
.timestamp(Instant.now())
.unit(StandardUnit.Milliseconds)
.value(56.78)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
package software.amazon.awssdk.metrics.internal.cloudwatch;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -107,7 +107,7 @@ record = it.next();
final String metric = fields[1];
m.metricName(metricName)
.dimensions(Dimension.builder().name("metric").value(metric).build())
.timestamp(new Date(Long.parseLong(fields[3])));
.timestamp(Instant.ofEpochMilli(Long.parseLong(fields[3])));
if (fields[4] != null && !fields[4].isEmpty())
m.value(Double.parseDouble(fields[4]));
m.unit(fields[6]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -60,7 +60,7 @@ public class TypeUtils {
DATA_TYPE_MAPPINGS.put("long", Long.class.getSimpleName());
DATA_TYPE_MAPPINGS.put("float", Float.class.getSimpleName());
DATA_TYPE_MAPPINGS.put("byte", Byte.class.getSimpleName());
DATA_TYPE_MAPPINGS.put("timestamp", Date.class.getName());
DATA_TYPE_MAPPINGS.put("timestamp", Instant.class.getName());
DATA_TYPE_MAPPINGS.put("blob", ByteBuffer.class.getName());
DATA_TYPE_MAPPINGS.put("stream", InputStream.class.getName());
DATA_TYPE_MAPPINGS.put("bigdecimal", BigDecimal.class.getName());
Expand All @@ -77,7 +77,7 @@ public class TypeUtils {
MARSHALLING_TYPE_MAPPINGS.put("Long", "LONG");
MARSHALLING_TYPE_MAPPINGS.put("Float", "FLOAT");
MARSHALLING_TYPE_MAPPINGS.put("Double", "DOUBLE");
MARSHALLING_TYPE_MAPPINGS.put("Date", "DATE");
MARSHALLING_TYPE_MAPPINGS.put("Instant", "INSTANT");
MARSHALLING_TYPE_MAPPINGS.put("ByteBuffer", "BYTE_BUFFER");
MARSHALLING_TYPE_MAPPINGS.put("Boolean", "BOOLEAN");
MARSHALLING_TYPE_MAPPINGS.put("BigDecimal", "BIG_DECIMAL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -143,7 +143,7 @@ public TypeName getTypeNameForSimpleType(String simpleType) {
// and non-streaming
ByteBuffer.class,
InputStream.class,
Date.class)
Instant.class)
.filter(cls -> cls.getName().equals(simpleType) || cls.getSimpleName().equals(simpleType))
.map(ClassName::get)
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ${shape.shapeName}Unmarshaller implements Unmarshaller<${shape.shap
<#if memberModel.http.isHeader() >
if (context.getHeader("${memberModel.http.unmarshallLocationName}") != null) {
context.setCurrentHeader("${memberModel.http.unmarshallLocationName}");
<#if memberModel.variable.simpleType == "Date">
<#if memberModel.variable.simpleType == "Instant">
${shape.variable.variableName}Builder.${memberModel.fluentSetterMethodName}(software.amazon.awssdk.util.DateUtils.parseRfc822Date(context.readText()));
<#else>
${shape.variable.variableName}Builder.${memberModel.fluentSetterMethodName}(<@MemberUnmarshallerDeclarationMacro.content memberModel />.unmarshall(context));
Expand Down Expand Up @@ -76,4 +76,4 @@ public class ${shape.shapeName}Unmarshaller implements Unmarshaller<${shape.shap
if (INSTANCE == null) INSTANCE = new ${shape.shapeName}Unmarshaller();
return INSTANCE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class ${shape.shapeName}Unmarshaller implements Unmarshaller<${shape.shap
<#if memberModel.http.isHeader() >
context.setCurrentHeader("${memberModel.http.unmarshallLocationName}");
${shape.variable.variableName}.${memberModel.fluentSetterMethodName}(
<#if memberModel.variable.simpleType == "Date">
<#if memberModel.variable.simpleType == "Instant">
software.amazon.awssdk.util.DateUtils.parseRfc822Date(context.readText()));
<#else>
${memberModel.variable.simpleType}Unmarshaller.getInstance().unmarshall(context));
Expand Down Expand Up @@ -169,4 +169,4 @@ public class ${shape.shapeName}Unmarshaller implements Unmarshaller<${shape.shap
if (INSTANCE == null) INSTANCE = new ${shape.shapeName}Unmarshaller();
return INSTANCE;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package software.amazon.awssdk.services.jsonprotocoltests.model;

import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
Expand All @@ -16,7 +16,7 @@
*/
@Generated("software.amazon.awssdk:codegen")
public class AllTypesRequest extends AmazonWebServiceRequest implements
ToCopyableBuilder<AllTypesRequest.Builder, AllTypesRequest> {
ToCopyableBuilder<AllTypesRequest.Builder, AllTypesRequest> {
private final String stringMember;

private final Integer integerMember;
Expand All @@ -41,7 +41,7 @@ public class AllTypesRequest extends AmazonWebServiceRequest implements

private final Map<String, SimpleStruct> mapOfStringToStruct;

private final Date timestampMember;
private final Instant timestampMember;

private final StructWithTimestamp structWithNestedTimestampMember;

Expand Down Expand Up @@ -214,7 +214,7 @@ public Map<String, SimpleStruct> mapOfStringToStruct() {
*
* @return The value of the TimestampMember property for this object.
*/
public Date timestampMember() {
public Instant timestampMember() {
return timestampMember;
}

Expand Down Expand Up @@ -329,7 +329,7 @@ public int hashCode() {
hashCode = 31 * hashCode + ((mapOfStringToStruct() == null) ? 0 : mapOfStringToStruct().hashCode());
hashCode = 31 * hashCode + ((timestampMember() == null) ? 0 : timestampMember().hashCode());
hashCode = 31 * hashCode
+ ((structWithNestedTimestampMember() == null) ? 0 : structWithNestedTimestampMember().hashCode());
+ ((structWithNestedTimestampMember() == null) ? 0 : structWithNestedTimestampMember().hashCode());
hashCode = 31 * hashCode + ((blobArg() == null) ? 0 : blobArg().hashCode());
hashCode = 31 * hashCode + ((structWithNestedBlob() == null) ? 0 : structWithNestedBlob().hashCode());
hashCode = 31 * hashCode + ((blobMap() == null) ? 0 : blobMap().hashCode());
Expand Down Expand Up @@ -434,7 +434,7 @@ public boolean equals(Object obj) {
return false;
}
if (other.structWithNestedTimestampMember() != null
&& !other.structWithNestedTimestampMember().equals(this.structWithNestedTimestampMember())) {
&& !other.structWithNestedTimestampMember().equals(this.structWithNestedTimestampMember())) {
return false;
}
if (other.blobArg() == null ^ this.blobArg() == null) {
Expand Down Expand Up @@ -471,14 +471,14 @@ public boolean equals(Object obj) {
return false;
}
if (other.polymorphicTypeWithSubTypes() != null
&& !other.polymorphicTypeWithSubTypes().equals(this.polymorphicTypeWithSubTypes())) {
&& !other.polymorphicTypeWithSubTypes().equals(this.polymorphicTypeWithSubTypes())) {
return false;
}
if (other.polymorphicTypeWithoutSubTypes() == null ^ this.polymorphicTypeWithoutSubTypes() == null) {
return false;
}
if (other.polymorphicTypeWithoutSubTypes() != null
&& !other.polymorphicTypeWithoutSubTypes().equals(this.polymorphicTypeWithoutSubTypes())) {
&& !other.polymorphicTypeWithoutSubTypes().equals(this.polymorphicTypeWithoutSubTypes())) {
return false;
}
return true;
Expand Down Expand Up @@ -698,7 +698,7 @@ public interface Builder extends CopyableBuilder<Builder, AllTypesRequest> {
* The new value for the TimestampMember property for this object.
* @return Returns a reference to this object so that method calls can be chained together.
*/
Builder timestampMember(Date timestampMember);
Builder timestampMember(Instant timestampMember);

/**
* Sets the value of the StructWithNestedTimestampMember property for this object.
Expand Down Expand Up @@ -811,7 +811,7 @@ private static final class BuilderImpl implements Builder {

private Map<String, SimpleStruct> mapOfStringToStruct;

private Date timestampMember;
private Instant timestampMember;

private StructWithTimestamp structWithNestedTimestampMember;

Expand Down Expand Up @@ -1060,18 +1060,18 @@ public final void setMapOfStringToStruct(Map<String, SimpleStruct> mapOfStringTo
this.mapOfStringToStruct = MapOfStringToSimpleStructCopier.copy(mapOfStringToStruct);
}

public final Date getTimestampMember() {
public final Instant getTimestampMember() {
return timestampMember;
}

@Override
public final Builder timestampMember(Date timestampMember) {
this.timestampMember = StandardMemberCopier.copy(timestampMember);
public final Builder timestampMember(Instant timestampMember) {
this.timestampMember = timestampMember;
return this;
}

public final void setTimestampMember(Date timestampMember) {
this.timestampMember = StandardMemberCopier.copy(timestampMember);
public final void setTimestampMember(Instant timestampMember) {
this.timestampMember = timestampMember;
}

public final StructWithTimestamp getStructWithNestedTimestampMember() {
Expand Down Expand Up @@ -1204,3 +1204,4 @@ public AllTypesRequest build() {
}
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package software.amazon.awssdk.services.jsonprotocoltests.model;

import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
Expand All @@ -17,7 +17,7 @@
*/
@Generated("software.amazon.awssdk:codegen")
public class AllTypesResponse extends AmazonWebServiceResult<ResponseMetadata> implements
ToCopyableBuilder<AllTypesResponse.Builder, AllTypesResponse> {
ToCopyableBuilder<AllTypesResponse.Builder, AllTypesResponse> {
private final String stringMember;

private final Integer integerMember;
Expand All @@ -42,7 +42,7 @@ public class AllTypesResponse extends AmazonWebServiceResult<ResponseMetadata> i

private final Map<String, SimpleStruct> mapOfStringToStruct;

private final Date timestampMember;
private final Instant timestampMember;

private final StructWithTimestamp structWithNestedTimestampMember;

Expand Down Expand Up @@ -215,7 +215,7 @@ public Map<String, SimpleStruct> mapOfStringToStruct() {
*
* @return The value of the TimestampMember property for this object.
*/
public Date timestampMember() {
public Instant timestampMember() {
return timestampMember;
}

Expand Down Expand Up @@ -330,7 +330,7 @@ public int hashCode() {
hashCode = 31 * hashCode + ((mapOfStringToStruct() == null) ? 0 : mapOfStringToStruct().hashCode());
hashCode = 31 * hashCode + ((timestampMember() == null) ? 0 : timestampMember().hashCode());
hashCode = 31 * hashCode
+ ((structWithNestedTimestampMember() == null) ? 0 : structWithNestedTimestampMember().hashCode());
+ ((structWithNestedTimestampMember() == null) ? 0 : structWithNestedTimestampMember().hashCode());
hashCode = 31 * hashCode + ((blobArg() == null) ? 0 : blobArg().hashCode());
hashCode = 31 * hashCode + ((structWithNestedBlob() == null) ? 0 : structWithNestedBlob().hashCode());
hashCode = 31 * hashCode + ((blobMap() == null) ? 0 : blobMap().hashCode());
Expand Down Expand Up @@ -435,7 +435,7 @@ public boolean equals(Object obj) {
return false;
}
if (other.structWithNestedTimestampMember() != null
&& !other.structWithNestedTimestampMember().equals(this.structWithNestedTimestampMember())) {
&& !other.structWithNestedTimestampMember().equals(this.structWithNestedTimestampMember())) {
return false;
}
if (other.blobArg() == null ^ this.blobArg() == null) {
Expand Down Expand Up @@ -472,14 +472,14 @@ public boolean equals(Object obj) {
return false;
}
if (other.polymorphicTypeWithSubTypes() != null
&& !other.polymorphicTypeWithSubTypes().equals(this.polymorphicTypeWithSubTypes())) {
&& !other.polymorphicTypeWithSubTypes().equals(this.polymorphicTypeWithSubTypes())) {
return false;
}
if (other.polymorphicTypeWithoutSubTypes() == null ^ this.polymorphicTypeWithoutSubTypes() == null) {
return false;
}
if (other.polymorphicTypeWithoutSubTypes() != null
&& !other.polymorphicTypeWithoutSubTypes().equals(this.polymorphicTypeWithoutSubTypes())) {
&& !other.polymorphicTypeWithoutSubTypes().equals(this.polymorphicTypeWithoutSubTypes())) {
return false;
}
return true;
Expand Down Expand Up @@ -699,7 +699,7 @@ public interface Builder extends CopyableBuilder<Builder, AllTypesResponse> {
* The new value for the TimestampMember property for this object.
* @return Returns a reference to this object so that method calls can be chained together.
*/
Builder timestampMember(Date timestampMember);
Builder timestampMember(Instant timestampMember);

/**
* Sets the value of the StructWithNestedTimestampMember property for this object.
Expand Down Expand Up @@ -812,7 +812,7 @@ private static final class BuilderImpl implements Builder {

private Map<String, SimpleStruct> mapOfStringToStruct;

private Date timestampMember;
private Instant timestampMember;

private StructWithTimestamp structWithNestedTimestampMember;

Expand Down Expand Up @@ -1061,18 +1061,18 @@ public final void setMapOfStringToStruct(Map<String, SimpleStruct> mapOfStringTo
this.mapOfStringToStruct = MapOfStringToSimpleStructCopier.copy(mapOfStringToStruct);
}

public final Date getTimestampMember() {
public final Instant getTimestampMember() {
return timestampMember;
}

@Override
public final Builder timestampMember(Date timestampMember) {
this.timestampMember = StandardMemberCopier.copy(timestampMember);
public final Builder timestampMember(Instant timestampMember) {
this.timestampMember = timestampMember;
return this;
}

public final void setTimestampMember(Date timestampMember) {
this.timestampMember = StandardMemberCopier.copy(timestampMember);
public final void setTimestampMember(Instant timestampMember) {
this.timestampMember = timestampMember;
}

public final StructWithTimestamp getStructWithNestedTimestampMember() {
Expand Down Expand Up @@ -1205,3 +1205,4 @@ public AllTypesResponse build() {
}
}
}

Loading

0 comments on commit 999f218

Please sign in to comment.