Skip to content

Commit 052f0fb

Browse files
authored
Fix implementations of query() (#376)
Precision query is defined to return null when date is inconsistent `MonthDay` is inconsistent, but all the clases here are consistent Fix implementations and enhance docs Fix other minor Javadoc issues
1 parent d896bcf commit 052f0fb

File tree

14 files changed

+68
-95
lines changed

14 files changed

+68
-95
lines changed

src/main/java/org/threeten/extra/AmPm.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,9 @@ public long getLong(TemporalField field) {
310310
/**
311311
* Queries this am-pm using the specified query.
312312
* <p>
313-
* This queries this am-pm using the specified query strategy object.
314-
* The {@code TemporalQuery} object defines the logic to be used to
315-
* obtain the result. Read the documentation of the query to understand
316-
* what the result of this method will be.
317-
* <p>
318-
* The result of this method is obtained by invoking the
319-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
320-
* specified query passing {@code this} as the argument.
313+
* {@link TemporalQueries#precision()} is directly supported.
314+
* Otherwise, the result of this method is obtained by invoking
315+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
321316
*
322317
* @param <R> the type of the result
323318
* @param query the query to invoke, not null

src/main/java/org/threeten/extra/DayOfMonth.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.time.chrono.Chronology;
4545
import java.time.chrono.IsoChronology;
4646
import java.time.temporal.ChronoField;
47+
import java.time.temporal.ChronoUnit;
4748
import java.time.temporal.Temporal;
4849
import java.time.temporal.TemporalAccessor;
4950
import java.time.temporal.TemporalAdjuster;
@@ -366,14 +367,9 @@ public boolean isValidYearMonth(YearMonth yearMonth) {
366367
/**
367368
* Queries this day-of-month using the specified query.
368369
* <p>
369-
* This queries this day-of-month using the specified query strategy object.
370-
* The {@code TemporalQuery} object defines the logic to be used to
371-
* obtain the result. Read the documentation of the query to understand
372-
* what the result of this method will be.
373-
* <p>
374-
* The result of this method is obtained by invoking the
375-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
376-
* specified query passing {@code this} as the argument.
370+
* {@link TemporalQueries#chronology()} and {@link TemporalQueries#precision()} are directly supported.
371+
* Otherwise, the result of this method is obtained by invoking
372+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
377373
*
378374
* @param <R> the type of the result
379375
* @param query the query to invoke, not null
@@ -386,6 +382,8 @@ public boolean isValidYearMonth(YearMonth yearMonth) {
386382
public <R> R query(TemporalQuery<R> query) {
387383
if (query == TemporalQueries.chronology()) {
388384
return (R) IsoChronology.INSTANCE;
385+
} else if (query == TemporalQueries.precision()) {
386+
return (R) ChronoUnit.DAYS;
389387
}
390388
return TemporalAccessor.super.query(query);
391389
}

src/main/java/org/threeten/extra/DayOfYear.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.time.chrono.Chronology;
4343
import java.time.chrono.IsoChronology;
4444
import java.time.temporal.ChronoField;
45+
import java.time.temporal.ChronoUnit;
4546
import java.time.temporal.Temporal;
4647
import java.time.temporal.TemporalAccessor;
4748
import java.time.temporal.TemporalAdjuster;
@@ -350,7 +351,7 @@ public long getLong(TemporalField field) {
350351
/**
351352
* Checks if the year is valid for this day-of-year.
352353
* <p>
353-
* This method checks whether this day-of-yearand the input year form
354+
* This method checks whether this day-of-year and the input year form
354355
* a valid date. This can only return false for day-of-year 366.
355356
*
356357
* @param year the year to validate
@@ -364,14 +365,9 @@ public boolean isValidYear(int year) {
364365
/**
365366
* Queries this day-of-year using the specified query.
366367
* <p>
367-
* This queries this day-of-year using the specified query strategy object.
368-
* The {@code TemporalQuery} object defines the logic to be used to
369-
* obtain the result. Read the documentation of the query to understand
370-
* what the result of this method will be.
371-
* <p>
372-
* The result of this method is obtained by invoking the
373-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
374-
* specified query passing {@code this} as the argument.
368+
* {@link TemporalQueries#chronology()} and {@link TemporalQueries#precision()} are directly supported.
369+
* Otherwise, the result of this method is obtained by invoking
370+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
375371
*
376372
* @param <R> the type of the result
377373
* @param query the query to invoke, not null
@@ -384,6 +380,8 @@ public boolean isValidYear(int year) {
384380
public <R> R query(TemporalQuery<R> query) {
385381
if (query == TemporalQueries.chronology()) {
386382
return (R) IsoChronology.INSTANCE;
383+
} else if (query == TemporalQueries.precision()) {
384+
return (R) ChronoUnit.DAYS;
387385
}
388386
return TemporalAccessor.super.query(query);
389387
}

src/main/java/org/threeten/extra/Half.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public int getValue() {
175175

176176
//-----------------------------------------------------------------------
177177
/**
178-
* Gets the textual representation, such as 'H1' or '4th half'.
178+
* Gets the textual representation, such as 'H1' or '2nd half'.
179179
* <p>
180180
* This returns the textual name used to identify the half-of-year,
181181
* suitable for presentation to the user.
@@ -362,7 +362,7 @@ public Half minus(long halves) {
362362
* H2 has 184 days.
363363
*
364364
* @param leapYear true if the length is required for a leap year
365-
* @return the length of this month in days, 181, 182 or 184
365+
* @return the length of this half in days, 181, 182 or 184
366366
*/
367367
public int length(boolean leapYear) {
368368
return this == H1 ? (leapYear ? 182 : 181) : 184;
@@ -385,14 +385,9 @@ public Month firstMonth() {
385385
/**
386386
* Queries this half-of-year using the specified query.
387387
* <p>
388-
* This queries this half-of-year using the specified query strategy object.
389-
* The {@code TemporalQuery} object defines the logic to be used to
390-
* obtain the result. Read the documentation of the query to understand
391-
* what the result of this method will be.
392-
* <p>
393-
* The result of this method is obtained by invoking the
394-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
395-
* specified query passing {@code this} as the argument.
388+
* {@link TemporalQueries#chronology()} and {@link TemporalQueries#precision()} are directly supported.
389+
* Otherwise, the result of this method is obtained by invoking
390+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
396391
*
397392
* @param <R> the type of the result
398393
* @param query the query to invoke, not null

src/main/java/org/threeten/extra/HourMinute.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public ValueRange range(TemporalField field) {
404404
* <p>
405405
* If the field is a {@link ChronoField} then the query is implemented here.
406406
* The {@link #isSupported(TemporalField) supported fields} will return valid
407-
* values based on this hour-minute,.
407+
* values based on this hour-minute.
408408
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
409409
* <p>
410410
* If the field is not a {@code ChronoField}, then the result of this method
@@ -674,13 +674,13 @@ public HourMinute plus(TemporalAmount amountToAdd) {
674674
* The supported fields behave as follows:
675675
* <ul>
676676
* <li>{@code MINUTES} -
677-
* Returns a {@code LocalTime} with the specified number of minutes added.
677+
* Returns an {@code HourMinute} with the specified number of minutes added.
678678
* This is equivalent to {@link #plusMinutes(long)}.
679679
* <li>{@code HOURS} -
680-
* Returns a {@code LocalTime} with the specified number of hours added.
680+
* Returns an {@code HourMinute} with the specified number of hours added.
681681
* This is equivalent to {@link #plusHours(long)}.
682682
* <li>{@code HALF_DAYS} -
683-
* Returns a {@code LocalTime} with the specified number of half-days added.
683+
* Returns an {@code HourMinute} with the specified number of half-days added.
684684
* This is equivalent to {@link #plusHours(long)} with the amount
685685
* multiplied by 12.
686686
* </ul>
@@ -842,14 +842,9 @@ public HourMinute minusMinutes(long minutesToSubtract) {
842842
/**
843843
* Queries this hour-minute using the specified query.
844844
* <p>
845-
* This queries this hour-minute using the specified query strategy object.
846-
* The {@code TemporalQuery} object defines the logic to be used to
847-
* obtain the result. Read the documentation of the query to understand
848-
* what the result of this method will be.
849-
* <p>
850-
* The result of this method is obtained by invoking the
851-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
852-
* specified query passing {@code this} as the argument.
845+
* {@link TemporalQueries#localTime()} and {@link TemporalQueries#precision()} are directly supported.
846+
* Otherwise, the result of this method is obtained by invoking
847+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
853848
*
854849
* @param <R> the type of the result
855850
* @param query the query to invoke, not null

src/main/java/org/threeten/extra/OffsetDate.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,14 +1050,10 @@ public OffsetDate minusDays(long days) {
10501050
/**
10511051
* Queries this date using the specified query.
10521052
* <p>
1053-
* This queries this date using the specified query strategy object.
1054-
* The {@code TemporalQuery} object defines the logic to be used to
1055-
* obtain the result. Read the documentation of the query to understand
1056-
* what the result of this method will be.
1057-
* <p>
1058-
* The result of this method is obtained by invoking the
1059-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1060-
* specified query passing {@code this} as the argument.
1053+
* {@link TemporalQueries#localDate()}, {@link TemporalQueries#chronology()}, {@link TemporalQueries#offset()},
1054+
* {@link TemporalQueries#zone()} and {@link TemporalQueries#precision()} are directly supported.
1055+
* Otherwise, the result of this method is obtained by invoking
1056+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
10611057
*
10621058
* @param <R> the type of the result
10631059
* @param query the query to invoke, not null
@@ -1068,7 +1064,9 @@ public OffsetDate minusDays(long days) {
10681064
@SuppressWarnings("unchecked")
10691065
@Override
10701066
public <R> R query(TemporalQuery<R> query) {
1071-
if (query == TemporalQueries.chronology()) {
1067+
if (query == TemporalQueries.localDate()) {
1068+
return (R) date;
1069+
} else if (query == TemporalQueries.chronology()) {
10721070
return (R) IsoChronology.INSTANCE;
10731071
} else if (query == TemporalQueries.precision()) {
10741072
return (R) DAYS;

src/main/java/org/threeten/extra/Quarter.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public Quarter minus(long quarters) {
383383
* Q3 and Q4 have 92 days.
384384
*
385385
* @param leapYear true if the length is required for a leap year
386-
* @return the length of this month in days, from 90 to 92
386+
* @return the length of this quarter in days, from 90 to 92
387387
*/
388388
public int length(boolean leapYear) {
389389
switch (this) {
@@ -429,14 +429,9 @@ public Month firstMonth() {
429429
/**
430430
* Queries this quarter-of-year using the specified query.
431431
* <p>
432-
* This queries this quarter-of-year using the specified query strategy object.
433-
* The {@code TemporalQuery} object defines the logic to be used to
434-
* obtain the result. Read the documentation of the query to understand
435-
* what the result of this method will be.
436-
* <p>
437-
* The result of this method is obtained by invoking the
438-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
439-
* specified query passing {@code this} as the argument.
432+
* {@link TemporalQueries#chronology()} and {@link TemporalQueries#precision()} are directly supported.
433+
* Otherwise, the result of this method is obtained by invoking
434+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
440435
*
441436
* @param <R> the type of the result
442437
* @param query the query to invoke, not null

src/main/java/org/threeten/extra/YearHalf.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public static YearHalf from(TemporalAccessor temporal) {
270270
* Obtains an instance of {@code YearHalf} from a text string such as {@code 2007-H2}.
271271
* <p>
272272
* The string must represent a valid year-half.
273-
* The format must be {@code uuuu-'Q'Q} where the 'Q' is case insensitive.
273+
* The format must be {@code uuuu-'H'H} where the 'H' is case insensitive.
274274
* Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.
275275
*
276276
* @param text the text to parse such as "2007-H2", not null
@@ -452,7 +452,7 @@ public ValueRange range(TemporalField field) {
452452
* <p>
453453
* If the field is a {@link ChronoField} then the query is implemented here.
454454
* The {@link #isSupported(TemporalField) supported fields} will return valid
455-
* values based on this year-half,.
455+
* values based on this year-half.
456456
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
457457
* <p>
458458
* If the field is not a {@code ChronoField}, then the result of this method
@@ -974,14 +974,9 @@ public YearHalf minusHalves(long halvesToSubtract) {
974974
/**
975975
* Queries this year-half using the specified query.
976976
* <p>
977-
* This queries this year-half using the specified query strategy object.
978-
* The {@code TemporalQuery} object defines the logic to be used to
979-
* obtain the result. Read the documentation of the query to understand
980-
* what the result of this method will be.
981-
* <p>
982-
* The result of this method is obtained by invoking the
983-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
984-
* specified query passing {@code this} as the argument.
977+
* {@link TemporalQueries#chronology()} and {@link TemporalQueries#precision()} are directly supported.
978+
* Otherwise, the result of this method is obtained by invoking
979+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
985980
*
986981
* @param <R> the type of the result
987982
* @param query the query to invoke, not null

src/main/java/org/threeten/extra/YearQuarter.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public ValueRange range(TemporalField field) {
453453
* <p>
454454
* If the field is a {@link ChronoField} then the query is implemented here.
455455
* The {@link #isSupported(TemporalField) supported fields} will return valid
456-
* values based on this year-quarter,.
456+
* values based on this year-quarter.
457457
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
458458
* <p>
459459
* If the field is not a {@code ChronoField}, then the result of this method
@@ -975,14 +975,9 @@ public YearQuarter minusQuarters(long quartersToSubtract) {
975975
/**
976976
* Queries this year-quarter using the specified query.
977977
* <p>
978-
* This queries this year-quarter using the specified query strategy object.
979-
* The {@code TemporalQuery} object defines the logic to be used to
980-
* obtain the result. Read the documentation of the query to understand
981-
* what the result of this method will be.
982-
* <p>
983-
* The result of this method is obtained by invoking the
984-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
985-
* specified query passing {@code this} as the argument.
978+
* {@link TemporalQueries#chronology()} and {@link TemporalQueries#precision()} are directly supported.
979+
* Otherwise, the result of this method is obtained by invoking
980+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
986981
*
987982
* @param <R> the type of the result
988983
* @param query the query to invoke, not null

src/main/java/org/threeten/extra/YearWeek.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -842,14 +842,9 @@ public YearWeek minusWeeks(long weeksToSubtract) {
842842
/**
843843
* Queries this year-week using the specified query.
844844
* <p>
845-
* This queries this year-week using the specified query strategy object.
846-
* The {@code TemporalQuery} object defines the logic to be used to
847-
* obtain the result. Read the documentation of the query to understand
848-
* what the result of this method will be.
849-
* <p>
850-
* The result of this method is obtained by invoking the
851-
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
852-
* specified query passing {@code this} as the argument.
845+
* {@link TemporalQueries#chronology()} and {@link TemporalQueries#precision()} are directly supported.
846+
* Otherwise, the result of this method is obtained by invoking
847+
* {@link TemporalAccessor#query(TemporalQuery)} on the parent interface.
853848
*
854849
* @param <R> the type of the result
855850
* @param query the query to invoke, not null
@@ -862,6 +857,8 @@ public YearWeek minusWeeks(long weeksToSubtract) {
862857
public <R> R query(TemporalQuery<R> query) {
863858
if (query == TemporalQueries.chronology()) {
864859
return (R) IsoChronology.INSTANCE;
860+
} else if (query == TemporalQueries.precision()) {
861+
return (R) WEEKS;
865862
}
866863
return Temporal.super.query(query);
867864
}

0 commit comments

Comments
 (0)