2828import java .util .Collections ;
2929import java .util .List ;
3030import java .util .Map ;
31+ import java .util .Objects ;
3132
3233public final class DynamoTableUtils {
3334
@@ -71,20 +72,18 @@ public static ProvisionedThroughput createProvisionedThroughput(Long readCapacit
7172
7273 ProvisionedThroughput .Builder provisionedThroughputBuilder = ProvisionedThroughput .builder ();
7374
74- if (readCapacityUnits != null && readCapacityUnits .longValue () > 0 )
75+ if (readCapacityUnits != null && readCapacityUnits .longValue () > 0 ) {
7576 provisionedThroughputBuilder .readCapacityUnits (readCapacityUnits );
76- else {
77+ } else {
7778 provisionedThroughputBuilder .readCapacityUnits (READ_CAPACITY_UNITS );
7879 }
7980
80-
81- if (writeCapacityUnit != null && writeCapacityUnit .longValue () > 0 )
81+ if (writeCapacityUnit != null && writeCapacityUnit .longValue () > 0 ) {
8282 provisionedThroughputBuilder .writeCapacityUnits (writeCapacityUnit );
83- else {
83+ } else {
8484 provisionedThroughputBuilder .writeCapacityUnits (READ_CAPACITY_UNITS );
8585 }
8686
87-
8887 return provisionedThroughputBuilder .build ();
8988 }
9089
@@ -98,34 +97,37 @@ public static Map<String, ScalarAttributeType> createAttributesType() {
9897
9998 public static void manageTables (String tableName , DynamoDbClient client , Long readCapacityUnits , Long writeCapacityUnit ) {
10099
101- boolean hasTable = true ;
100+ boolean hasTable = false ;
102101 String lastName = null ;
103102
104- while (hasTable ) {
103+ while (! hasTable ) {
105104 try {
106- ListTablesResponse response = null ;
107- if (lastName == null ) {
108- ListTablesRequest request = ListTablesRequest .builder ().build ();
109- response = client .listTables (request );
110- } else {
111- ListTablesRequest request = ListTablesRequest .builder ().exclusiveStartTableName (lastName ).build ();
112- response = client .listTables (request );
105+ ListTablesRequest .Builder builder = ListTablesRequest .builder ();
106+ if (Objects .nonNull (lastName )) {
107+ builder .exclusiveStartTableName (lastName );
113108 }
109+ ListTablesRequest request = builder .build ();
110+ ListTablesResponse response = client .listTables (request );
114111
115112 List <String > tableNames = response .tableNames ();
116113
117- if (tableNames .size () == 0 ) {
118- createTable ( tableName , client , readCapacityUnits , writeCapacityUnit ) ;
114+ if (tableNames .isEmpty () ) {
115+ break ;
119116 } else {
120- lastName = response . lastEvaluatedTableName ();
121- if ( lastName == null ) {
122- hasTable = false ;
117+ if ( tableNames . contains ( tableName )) {
118+ hasTable = true ;
119+ break ;
123120 }
121+ Collections .reverse (tableNames );
122+ lastName = tableNames .stream ().findFirst ().get ();
124123 }
125124 } catch (DynamoDbException e ) {
126125 throw new RuntimeException (e );
127126 }
128127 }
128+ if (!hasTable ) {
129+ createTable (tableName , client , readCapacityUnits , writeCapacityUnit );
130+ }
129131 }
130132
131133 private static void createTable (String tableName , DynamoDbClient client , Long readCapacityUnits , Long writeCapacityUnit ) {
0 commit comments