This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 71abbb46f5 Utility class cleanup
71abbb46f5 is described below
commit 71abbb46f5f1da4ee0d13cc9f26ab1d222650ec3
Author: James Bognar <[email protected]>
AuthorDate: Tue Oct 21 16:11:51 2025 -0400
Utility class cleanup
---
.../org/apache/juneau/common/utils/DateUtils.java | 1 -
.../juneau/common/utils/GranularZonedDateTime.java | 147 +++++++++++++++++++++
.../juneau/objecttools/TimeMatcherFactory.java | 120 +++++------------
3 files changed, 179 insertions(+), 89 deletions(-)
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/utils/DateUtils.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/utils/DateUtils.java
index 7a882663cb..3026924922 100644
---
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/utils/DateUtils.java
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/utils/DateUtils.java
@@ -436,7 +436,6 @@ public class DateUtils {
if (state != S7)
result +=
ZonedDateTime.now(ZoneId.systemDefault()).getOffset().toString();
- System.err.println("XXX - " + result);
return result;
}
diff --git
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/utils/GranularZonedDateTime.java
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/utils/GranularZonedDateTime.java
new file mode 100644
index 0000000000..a181f50e93
--- /dev/null
+++
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/utils/GranularZonedDateTime.java
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juneau.common.utils;
+
+import java.time.*;
+import java.time.temporal.*;
+import java.util.*;
+
+/**
+ * A ZonedDateTime with precision information for granular time operations.
+ *
+ * <p>
+ * This class combines a {@link ZonedDateTime} with a {@link ChronoField}
precision identifier,
+ * allowing for granular time operations such as rolling by specific time
units.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bjava'>
+ * <jc>// Create with year precision</jc>
+ * GranularZonedDateTime <jv>gdt</jv> =
GranularZonedDateTime.parse(<js>"2011"</js>);
+ * <jc>// Roll forward by one year</jc>
+ * <jv>gdt</jv>.roll(1);
+ * <jc>// Get the ZonedDateTime</jc>
+ * ZonedDateTime <jv>zdt</jv> = <jv>gdt</jv>.getZonedDateTime();
+ * </p>
+ *
+ * <h5 class='section'>Thread Safety:</h5>
+ * <p>
+ * This class is immutable and thread-safe.
+ *
+ * <h5 class='section'>See Also:</h5><ul>
+ * <li class='jm'>{@link ZonedDateTime}
+ * <li class='jm'>{@link ChronoField}
+ * <li class='jm'>{@link DateUtils}
+ * </ul>
+ */
+public class GranularZonedDateTime {
+
+ /** The ZonedDateTime value */
+ public final ZonedDateTime zdt;
+
+ /** The precision of this time value */
+ public final ChronoField precision;
+
+ /**
+ * Constructor.
+ *
+ * @param date The date to wrap.
+ * @param precision The precision of this time value.
+ */
+ public GranularZonedDateTime(Date date, ChronoField precision) {
+ this.zdt = date.toInstant().atZone(ZoneId.systemDefault());
+ this.precision = precision;
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param zdt The ZonedDateTime value.
+ * @param precision The precision of this time value.
+ */
+ public GranularZonedDateTime(ZonedDateTime zdt, ChronoField precision) {
+ this.zdt = zdt;
+ this.precision = precision;
+ }
+
+ /**
+ * Creates a copy of this object.
+ *
+ * @return A new GranularZonedDateTime with the same values.
+ */
+ public GranularZonedDateTime copy() {
+ return new GranularZonedDateTime(zdt, precision);
+ }
+
+ /**
+ * Returns the ZonedDateTime value.
+ *
+ * @return The ZonedDateTime value.
+ */
+ public ZonedDateTime getZonedDateTime() {
+ return zdt;
+ }
+
+ /**
+ * Rolls this time value by the specified amount using the current
precision.
+ *
+ * @param amount The amount to roll by.
+ * @return A new GranularZonedDateTime with the rolled value.
+ */
+ public GranularZonedDateTime roll(int amount) {
+ return roll(precision, amount);
+ }
+
+ /**
+ * Rolls this time value by the specified amount using the specified
field.
+ *
+ * @param field The field to roll by.
+ * @param amount The amount to roll by.
+ * @return A new GranularZonedDateTime with the rolled value.
+ */
+ public GranularZonedDateTime roll(ChronoField field, int amount) {
+ // Use DateUtils utility method to convert ChronoField to
ChronoUnit
+ ChronoUnit unit = DateUtils.toChronoUnit(field);
+ if (unit != null) {
+ ZonedDateTime newZdt = zdt.plus(amount, unit);
+ return new GranularZonedDateTime(newZdt, precision);
+ }
+ return this;
+ }
+
+ /**
+ * Parses a timestamp string and returns a GranularZonedDateTime.
+ *
+ * <p>
+ * This method uses {@link DateUtils#fromIso8601(String)} for parsing
and
+ * {@link DateUtils#getPrecisionFromString(String)} for determining
precision.
+ *
+ * @param seg The string segment to parse.
+ * @return A GranularZonedDateTime representing the parsed timestamp.
+ * @throws BasicRuntimeException If the string cannot be parsed as a
valid timestamp.
+ */
+ public static GranularZonedDateTime parse(String seg) {
+ // Try DateUtils.fromIso8601 first for consistency
+ ZonedDateTime zdt = DateUtils.fromIso8601(seg);
+ if (zdt != null) {
+ // Determine precision based on the input string
+ var precision = DateUtils.getPrecisionFromString(seg);
+ return new GranularZonedDateTime(zdt, precision);
+ }
+
+ throw new RuntimeException("Invalid date encountered: '" + seg
+ "'");
+ }
+}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/objecttools/TimeMatcherFactory.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/objecttools/TimeMatcherFactory.java
index 63cb82a3fe..f443719153 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/objecttools/TimeMatcherFactory.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/objecttools/TimeMatcherFactory.java
@@ -17,8 +17,9 @@
package org.apache.juneau.objecttools;
import static org.apache.juneau.common.utils.StateEnum.*;
+import static java.time.temporal.ChronoField.*;
-import java.time.temporal.*;
+import java.time.*;
import java.util.*;
import org.apache.juneau.*;
@@ -49,41 +50,6 @@ import org.apache.juneau.common.utils.*;
*/
public class TimeMatcherFactory extends MatcherFactory {
- /**
- * Combines a Calendar with a precision identifier.
- */
- private static class CalendarP {
- public Calendar c;
- public ChronoField precision;
-
- public CalendarP(Date date, ChronoField precision) {
- c = Calendar.getInstance();
- c.setTime(date);
- this.precision = precision;
- }
-
- public CalendarP(Calendar c, ChronoField precision) {
- this.c = (Calendar)c.clone();
- this.precision = precision;
- }
-
- public CalendarP copy() {
- return new CalendarP(c, precision);
- }
-
- public Calendar getCalendar() { return (Calendar)c.clone(); }
-
- public CalendarP roll(int amount) {
- return roll(precision, amount);
- }
-
- public CalendarP roll(ChronoField field, int amount) {
- // Use DateUtils utility method to convert ChronoField
to Calendar field
- int calendarField = DateUtils.toCalendarField(field);
- c.add(calendarField, amount);
- return this;
- }
- }
/**
* A construct representing a single search pattern.
@@ -119,9 +85,9 @@ public class TimeMatcherFactory extends MatcherFactory {
// S12 = Found [2000 - "], looking for ["] (["]=S1)
// S13 = Found [2000 - 2], looking for WS (WS=S1)
- StateEnum state = S1;
- int mark = 0;
- Equality eq = Equality.NONE;
+ var state = S1;
+ var mark = 0;
+ var eq = Equality.NONE;
String s1 = null, s2 = null;
int i;
@@ -339,15 +305,16 @@ public class TimeMatcherFactory extends MatcherFactory {
if (ranges.length == 0)
return true;
- Calendar c = null;
- if (cm.isCalendar())
- c = (Calendar)o;
- else {
- c = Calendar.getInstance();
- c.setTime((Date)o);
+ ZonedDateTime zdt = null;
+ if (cm.isCalendar()) {
+ var c = (Calendar)o;
+ zdt =
c.toInstant().atZone(c.getTimeZone().toZoneId());
+ } else {
+ var date = (Date)o;
+ zdt =
date.toInstant().atZone(ZoneId.systemDefault());
}
for (TimestampRange range : ranges)
- if (range.matches(c))
+ if (range.matches(zdt))
return true;
return false;
}
@@ -358,39 +325,38 @@ public class TimeMatcherFactory extends MatcherFactory {
* All possible forms of search patterns are boiled down to these
timestamp ranges.
*/
private static class TimestampRange {
- Calendar start;
- Calendar end;
+ ZonedDateTime start;
+ ZonedDateTime end;
public TimestampRange(Equality eq, String singleDate) {
- CalendarP singleDate1 = parseDate(singleDate);
+ var singleDate1 =
GranularZonedDateTime.parse(singleDate);
if (eq == Equality.GT) {
- this.start =
singleDate1.roll(1).roll(ChronoField.MILLI_OF_SECOND, -1).getCalendar();
- this.end = new CalendarP(new
Date(Long.MAX_VALUE), ChronoField.MILLI_OF_SECOND).getCalendar();
+ this.start =
singleDate1.roll(1).roll(MILLI_OF_SECOND, -1).getZonedDateTime();
+ this.end =
Instant.ofEpochMilli(Long.MAX_VALUE).atZone(ZoneId.systemDefault());
} else if (eq == Equality.LT) {
- this.start = new CalendarP(new Date(0),
ChronoField.MILLI_OF_SECOND).getCalendar();
- this.end = singleDate1.getCalendar();
+ this.start =
Instant.ofEpochMilli(0).atZone(ZoneId.systemDefault());
+ this.end = singleDate1.getZonedDateTime();
} else if (eq == Equality.GTE) {
- this.start =
singleDate1.roll(ChronoField.MILLI_OF_SECOND, -1).getCalendar();
- this.end = new CalendarP(new
Date(Long.MAX_VALUE), ChronoField.MILLI_OF_SECOND).getCalendar();
+ this.start = singleDate1.roll(MILLI_OF_SECOND,
-1).getZonedDateTime();
+ this.end =
Instant.ofEpochMilli(Long.MAX_VALUE).atZone(ZoneId.systemDefault());
} else if (eq == Equality.LTE) {
- this.start = new CalendarP(new Date(0),
ChronoField.MILLI_OF_SECOND).getCalendar();
- this.end = singleDate1.roll(1).getCalendar();
+ this.start =
Instant.ofEpochMilli(0).atZone(ZoneId.systemDefault());
+ this.end =
singleDate1.roll(1).getZonedDateTime();
} else {
- this.start =
singleDate1.copy().roll(ChronoField.MILLI_OF_SECOND, -1).getCalendar();
- this.end = singleDate1.roll(1).getCalendar();
+ this.start =
singleDate1.copy().roll(MILLI_OF_SECOND, -1).getZonedDateTime();
+ this.end =
singleDate1.roll(1).getZonedDateTime();
}
}
public TimestampRange(String start, String end) {
- CalendarP start1 = parseDate(start);
- CalendarP end1 = parseDate(end);
- this.start =
start1.copy().roll(ChronoField.MILLI_OF_SECOND, -1).getCalendar();
- this.end = end1.roll(1).getCalendar();
+ var start1 = GranularZonedDateTime.parse(start);
+ var end1 = GranularZonedDateTime.parse(end);
+ this.start = start1.copy().roll(MILLI_OF_SECOND,
-1).getZonedDateTime();
+ this.end = end1.roll(1).getZonedDateTime();
}
- public boolean matches(Calendar c) {
- boolean b = (c.after(start) && c.before(end));
- return b;
+ public boolean matches(ZonedDateTime zdt) {
+ return zdt.isAfter(start) && zdt.isBefore(end);
}
}
@@ -399,28 +365,6 @@ public class TimeMatcherFactory extends MatcherFactory {
*/
public static final TimeMatcherFactory DEFAULT = new
TimeMatcherFactory();
- /**
- * Parses a timestamp string off the beginning of the string segment
'seg'.
- * Goes through each possible valid timestamp format until it finds a
match.
- * The position where the parsing left off is stored in pp.
- *
- * @param seg The string segment being parsed.
- * @param pp Where parsing last left off.
- * @return An object representing a timestamp.
- */
- static CalendarP parseDate(String seg) {
- // Try DateUtils.parseISO8601Calendar first for consistency
- Calendar c = GregorianCalendar.from(DateUtils.fromIso8601(seg));
- if (c != null) {
- // Determine precision based on the input string
- var precision = DateUtils.getPrecisionFromString(seg);
- return new CalendarP(c, precision);
- }
-
- throw new BasicRuntimeException("Invalid date encountered:
''{0}''", seg);
- }
-
-
/**
* Constructor.
*/