feat: introduce `java.time` methods and variables (#2780) · googleapis/java-bigquerystorage@8dd66d5

@@ -15,12 +15,15 @@

1515

*/

1616

package com.google.cloud.bigquery.storage.v1;

171718+

import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toJavaTimeLocalDateTime;

19+

import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toJavaTimeLocalTime;

20+

import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toThreetenLocalDateTime;

21+

import static com.google.cloud.bigquery.storage.util.TimeConversionUtils.toThreetenLocalTime;

1822

import static com.google.common.base.Preconditions.checkArgument;

192320-

import org.threeten.bp.DateTimeException;

21-

import org.threeten.bp.LocalDateTime;

22-

import org.threeten.bp.LocalTime;

23-

import org.threeten.bp.temporal.ChronoUnit;

24+

import com.google.api.core.ObsoleteApi;

25+

import java.time.DateTimeException;

26+

import java.time.temporal.ChronoUnit;

24272528

/**

2629

* Ported from ZetaSQL CivilTimeEncoder Original code can be found at:

@@ -89,7 +92,7 @@ public final class CivilTimeEncoder {

8992

* @see #decodePacked32TimeSeconds(int)

9093

*/

9194

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

92-

private static int encodePacked32TimeSeconds(LocalTime time) {

95+

private static int encodePacked32TimeSeconds(java.time.LocalTime time) {

9396

checkValidTimeSeconds(time);

9497

int bitFieldTimeSeconds = 0x0;

9598

bitFieldTimeSeconds |= time.getHour() << HOUR_SHIFT;

@@ -112,19 +115,29 @@ private static int encodePacked32TimeSeconds(LocalTime time) {

112115

* @see #encodePacked32TimeSeconds(LocalTime)

113116

*/

114117

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

115-

private static LocalTime decodePacked32TimeSeconds(int bitFieldTimeSeconds) {

118+

private static java.time.LocalTime decodePacked32TimeSeconds(int bitFieldTimeSeconds) {

116119

checkValidBitField(bitFieldTimeSeconds, TIME_SECONDS_MASK);

117120

int hourOfDay = getFieldFromBitField(bitFieldTimeSeconds, HOUR_MASK, HOUR_SHIFT);

118121

int minuteOfHour = getFieldFromBitField(bitFieldTimeSeconds, MINUTE_MASK, MINUTE_SHIFT);

119122

int secondOfMinute = getFieldFromBitField(bitFieldTimeSeconds, SECOND_MASK, SECOND_SHIFT);

120123

// LocalTime validates the input parameters.

121124

try {

122-

return LocalTime.of(hourOfDay, minuteOfHour, secondOfMinute);

125+

return java.time.LocalTime.of(hourOfDay, minuteOfHour, secondOfMinute);

123126

} catch (DateTimeException e) {

124127

throw new IllegalArgumentException(e.getMessage(), e);

125128

}

126129

}

127130131+

/**

132+

* This method is obsolete. Use {@link #encodePacked64TimeMicrosLocalTime(java.time.LocalTime)}

133+

* instead.

134+

*/

135+

@ObsoleteApi("Use encodePacked64TimeMicrosLocalTime(java.time.LocalTime) instead")

136+

@SuppressWarnings("GoodTime")

137+

public static long encodePacked64TimeMicros(org.threeten.bp.LocalTime time) {

138+

return encodePacked64TimeMicrosLocalTime(toJavaTimeLocalTime(time));

139+

}

140+128141

/**

129142

* Encodes {@code time} as a 8-byte integer with microseconds precision.

130143

*

@@ -140,13 +153,21 @@ private static LocalTime decodePacked32TimeSeconds(int bitFieldTimeSeconds) {

140153

* @see #encodePacked64TimeMicros(LocalTime)

141154

*/

142155

@SuppressWarnings("GoodTime")

143-

public static long encodePacked64TimeMicros(LocalTime time) {

156+

public static long encodePacked64TimeMicrosLocalTime(java.time.LocalTime time) {

144157

checkValidTimeMicros(time);

145158

return (((long) encodePacked32TimeSeconds(time)) << MICRO_LENGTH) | (time.getNano() / 1_000L);

146159

}

147160161+

/** This method is obsolete. Use {@link #decodePacked64TimeMicrosLocalTime(long)} instead. */

162+

@ObsoleteApi("Use decodePacked64TimeMicrosLocalTime(long) instead")

163+

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

164+

public static org.threeten.bp.LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {

165+

return toThreetenLocalTime(decodePacked64TimeMicrosLocalTime(bitFieldTimeMicros));

166+

}

167+148168

/**

149-

* Decodes {@code bitFieldTimeMicros} as a {@link LocalTime} with microseconds precision.

169+

* Decodes {@code bitFieldTimeMicros} as a {@link java.time.LocalTime} with microseconds

170+

* precision.

150171

*

151172

* <p>Encoding is as the following:

152173

*

@@ -159,13 +180,13 @@ public static long encodePacked64TimeMicros(LocalTime time) {

159180

* @see #encodePacked64TimeMicros(LocalTime)

160181

*/

161182

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

162-

public static LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {

183+

public static java.time.LocalTime decodePacked64TimeMicrosLocalTime(long bitFieldTimeMicros) {

163184

checkValidBitField(bitFieldTimeMicros, TIME_MICROS_MASK);

164185

int bitFieldTimeSeconds = (int) (bitFieldTimeMicros >> MICRO_LENGTH);

165-

LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);

186+

java.time.LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);

166187

int microOfSecond = getFieldFromBitField(bitFieldTimeMicros, MICRO_MASK, MICRO_SHIFT);

167188

checkValidMicroOfSecond(microOfSecond);

168-

LocalTime time = timeSeconds.withNano(microOfSecond * 1000);

189+

java.time.LocalTime time = timeSeconds.withNano(microOfSecond * 1000);

169190

checkValidTimeMicros(time);

170191

return time;

171192

}

@@ -184,7 +205,7 @@ public static LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {

184205

* @see #decodePacked64DatetimeSeconds(long)

185206

*/

186207

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

187-

private static long encodePacked64DatetimeSeconds(LocalDateTime dateTime) {

208+

private static long encodePacked64DatetimeSeconds(java.time.LocalDateTime dateTime) {

188209

checkValidDateTimeSeconds(dateTime);

189210

long bitFieldDatetimeSeconds = 0x0L;

190211

bitFieldDatetimeSeconds |= (long) dateTime.getYear() << YEAR_SHIFT;

@@ -208,16 +229,17 @@ private static long encodePacked64DatetimeSeconds(LocalDateTime dateTime) {

208229

* @see #encodePacked64DatetimeSeconds(LocalDateTime)

209230

*/

210231

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

211-

private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetimeSeconds) {

232+

private static java.time.LocalDateTime decodePacked64DatetimeSeconds(

233+

long bitFieldDatetimeSeconds) {

212234

checkValidBitField(bitFieldDatetimeSeconds, DATETIME_SECONDS_MASK);

213235

int bitFieldTimeSeconds = (int) (bitFieldDatetimeSeconds & TIME_SECONDS_MASK);

214-

LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);

236+

java.time.LocalTime timeSeconds = decodePacked32TimeSeconds(bitFieldTimeSeconds);

215237

int year = getFieldFromBitField(bitFieldDatetimeSeconds, YEAR_MASK, YEAR_SHIFT);

216238

int monthOfYear = getFieldFromBitField(bitFieldDatetimeSeconds, MONTH_MASK, MONTH_SHIFT);

217239

int dayOfMonth = getFieldFromBitField(bitFieldDatetimeSeconds, DAY_MASK, DAY_SHIFT);

218240

try {

219-

LocalDateTime dateTime =

220-

LocalDateTime.of(

241+

java.time.LocalDateTime dateTime =

242+

java.time.LocalDateTime.of(

221243

year,

222244

monthOfYear,

223245

dayOfMonth,

@@ -231,6 +253,16 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime

231253

}

232254

}

233255256+

/**

257+

* This method is obsolete. Use {@link

258+

* #encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime)} instead.

259+

*/

260+

@ObsoleteApi("Use encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime) instead")

261+

@SuppressWarnings({"GoodTime-ApiWithNumericTimeUnit", "JavaLocalDateTimeGetNano"})

262+

public static long encodePacked64DatetimeMicros(org.threeten.bp.LocalDateTime dateTime) {

263+

return encodePacked64DatetimeMicrosLocalDateTime(toJavaTimeLocalDateTime(dateTime));

264+

}

265+234266

/**

235267

* Encodes {@code dateTime} as a 8-byte integer with microseconds precision.

236268

*

@@ -245,14 +277,26 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime

245277

* @see #decodePacked64DatetimeMicros(long)

246278

*/

247279

@SuppressWarnings({"GoodTime-ApiWithNumericTimeUnit", "JavaLocalDateTimeGetNano"})

248-

public static long encodePacked64DatetimeMicros(LocalDateTime dateTime) {

280+

public static long encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime dateTime) {

249281

checkValidDateTimeMicros(dateTime);

250282

return (encodePacked64DatetimeSeconds(dateTime) << MICRO_LENGTH)

251283

| (dateTime.getNano() / 1_000L);

252284

}

253285254286

/**

255-

* Decodes {@code bitFieldDatetimeMicros} as a {@link LocalDateTime} with microseconds precision.

287+

* This method is obsolete. Use {@link #decodePacked64DatetimeMicrosLocalDateTime(long)} instead.

288+

*/

289+

@ObsoleteApi("Use decodePacked64DatetimeMicrosLocalDateTime(long) instead")

290+

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

291+

public static org.threeten.bp.LocalDateTime decodePacked64DatetimeMicros(

292+

long bitFieldDatetimeMicros) {

293+

return toThreetenLocalDateTime(

294+

decodePacked64DatetimeMicrosLocalDateTime(bitFieldDatetimeMicros));

295+

}

296+297+

/**

298+

* Decodes {@code bitFieldDatetimeMicros} as a {@link java.time.LocalDateTime} with microseconds

299+

* precision.

256300

*

257301

* <p>Encoding is as the following:

258302

*

@@ -265,13 +309,15 @@ public static long encodePacked64DatetimeMicros(LocalDateTime dateTime) {

265309

* @see #encodePacked64DatetimeMicros(LocalDateTime)

266310

*/

267311

@SuppressWarnings("GoodTime-ApiWithNumericTimeUnit")

268-

public static LocalDateTime decodePacked64DatetimeMicros(long bitFieldDatetimeMicros) {

312+

public static java.time.LocalDateTime decodePacked64DatetimeMicrosLocalDateTime(

313+

long bitFieldDatetimeMicros) {

269314

checkValidBitField(bitFieldDatetimeMicros, DATETIME_MICROS_MASK);

270315

long bitFieldDatetimeSeconds = bitFieldDatetimeMicros >> MICRO_LENGTH;

271-

LocalDateTime dateTimeSeconds = decodePacked64DatetimeSeconds(bitFieldDatetimeSeconds);

316+

java.time.LocalDateTime dateTimeSeconds =

317+

decodePacked64DatetimeSeconds(bitFieldDatetimeSeconds);

272318

int microOfSecond = getFieldFromBitField(bitFieldDatetimeMicros, MICRO_MASK, MICRO_SHIFT);

273319

checkValidMicroOfSecond(microOfSecond);

274-

LocalDateTime dateTime = dateTimeSeconds.withNano(microOfSecond * 1_000);

320+

java.time.LocalDateTime dateTime = dateTimeSeconds.withNano(microOfSecond * 1_000);

275321

checkValidDateTimeMicros(dateTime);

276322

return dateTime;

277323

}

@@ -280,25 +326,25 @@ private static int getFieldFromBitField(long bitField, long mask, int shift) {

280326

return (int) ((bitField & mask) >> shift);

281327

}

282328283-

private static void checkValidTimeSeconds(LocalTime time) {

329+

private static void checkValidTimeSeconds(java.time.LocalTime time) {

284330

checkArgument(time.getHour() >= 0 && time.getHour() <= 23);

285331

checkArgument(time.getMinute() >= 0 && time.getMinute() <= 59);

286332

checkArgument(time.getSecond() >= 0 && time.getSecond() <= 59);

287333

}

288334289-

private static void checkValidDateTimeSeconds(LocalDateTime dateTime) {

335+

private static void checkValidDateTimeSeconds(java.time.LocalDateTime dateTime) {

290336

checkArgument(dateTime.getYear() >= 1 && dateTime.getYear() <= 9999);

291337

checkArgument(dateTime.getMonthValue() >= 1 && dateTime.getMonthValue() <= 12);

292338

checkArgument(dateTime.getDayOfMonth() >= 1 && dateTime.getDayOfMonth() <= 31);

293339

checkValidTimeSeconds(dateTime.toLocalTime());

294340

}

295341296-

private static void checkValidTimeMicros(LocalTime time) {

342+

private static void checkValidTimeMicros(java.time.LocalTime time) {

297343

checkValidTimeSeconds(time);

298344

checkArgument(time.equals(time.truncatedTo(ChronoUnit.MICROS)));

299345

}

300346301-

private static void checkValidDateTimeMicros(LocalDateTime dateTime) {

347+

private static void checkValidDateTimeMicros(java.time.LocalDateTime dateTime) {

302348

checkValidDateTimeSeconds(dateTime);

303349

checkArgument(dateTime.equals(dateTime.truncatedTo(ChronoUnit.MICROS)));

304350

}