Skip to content

Commit 06d0ba1

Browse files
committed
Improve performance by wrapping http servlet request
1 parent 55557e5 commit 06d0ba1

File tree

10 files changed

+1091
-37
lines changed

10 files changed

+1091
-37
lines changed

joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/util/CollectionUtils.java

+60-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,13 @@ public static <T, V> List<V> toList(T[] arrays, Function<T, V> function) {
215215
public static <T> List<T> toList(Enumeration<T> enumeration) {
216216
List<T> result = null;
217217
if (enumeration != null && enumeration.hasMoreElements()) {
218-
result = new ArrayList<>(2);
219-
while (enumeration.hasMoreElements()) {
220-
result.add(enumeration.nextElement());
218+
if (enumeration instanceof ListEnumeration) {
219+
result = ((ListEnumeration<T>) enumeration).getElements();
220+
} else {
221+
result = new ArrayList<>(2);
222+
while (enumeration.hasMoreElements()) {
223+
result.add(enumeration.nextElement());
224+
}
221225
}
222226
}
223227
return result;
@@ -275,6 +279,28 @@ public static <V, T> Iterator<T> toIterator(V[] arrays, Function<V, T> function)
275279
return new ArrayIterator<>(arrays, function);
276280
}
277281

282+
/**
283+
* Converts a List of values to an Enumeration.
284+
*
285+
* @param values the List of values to convert
286+
* @param <T> the type of the values
287+
* @return an Enumeration containing the values from the List, or null if the List is null
288+
*/
289+
public static <T> Enumeration<T> toEnumeration(List<T> values) {
290+
return values == null ? Collections.emptyEnumeration() : new ListEnumeration<>(values);
291+
}
292+
293+
/**
294+
* Converts a Collection of values to an Enumeration.
295+
*
296+
* @param values the Collection of values to convert
297+
* @param <T> the type of the values
298+
* @return an Enumeration containing the values from the Collection, or null if the Collection is null
299+
*/
300+
public static <T> Enumeration<T> toEnumeration(Collection<T> values) {
301+
return values == null ? Collections.emptyEnumeration() : Collections.enumeration(values);
302+
}
303+
278304
/**
279305
* Converts an iterable of elements into a map using specified key and value functions.
280306
* If the provided iterable is null, the method returns null.
@@ -620,4 +646,35 @@ public T next() {
620646
return function.apply(arrays[index++]);
621647
}
622648
}
649+
650+
/**
651+
* A private static class that implements the Enumeration interface for a List of values.
652+
*
653+
* @param <T> the type of the values in the List
654+
*/
655+
private static class ListEnumeration<T> implements Enumeration<T> {
656+
657+
private final List<T> list;
658+
659+
private int index = 0;
660+
661+
ListEnumeration(List<T> list) {
662+
this.list = list;
663+
}
664+
665+
@Override
666+
public boolean hasMoreElements() {
667+
return list != null && index < list.size();
668+
}
669+
670+
@Override
671+
public T nextElement() {
672+
return list == null ? null : list.get(index++);
673+
}
674+
675+
public List<T> getElements() {
676+
return index == 0 || list == null ? list : list.subList(index, list.size());
677+
}
678+
}
679+
623680
}

joylive-core/joylive-governance-api/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@
1212
<artifactId>joylive-governance-api</artifactId>
1313
<properties>
1414
<guava.version>33.1.0-jre</guava.version>
15+
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
16+
<jakarta.servlet-api.version>6.1.0</jakarta.servlet-api.version>
1517
</properties>
1618

1719
<dependencies>
1820
<dependency>
1921
<groupId>com.jd.live</groupId>
2022
<artifactId>joylive-core-api</artifactId>
2123
</dependency>
24+
<dependency>
25+
<groupId>jakarta.servlet</groupId>
26+
<artifactId>jakarta.servlet-api</artifactId>
27+
<version>${jakarta.servlet-api.version}</version>
28+
<scope>provided</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>javax.servlet</groupId>
32+
<artifactId>javax.servlet-api</artifactId>
33+
<version>${javax.servlet-api.version}</version>
34+
<scope>provided</scope>
35+
</dependency>
2236
<dependency>
2337
<groupId>org.projectlombok</groupId>
2438
<artifactId>lombok</artifactId>

0 commit comments

Comments
 (0)