2020import java .util .ArrayList ;
2121import java .util .Collections ;
2222import java .util .LinkedHashMap ;
23+ import java .util .LinkedHashSet ;
2324import java .util .List ;
2425import java .util .Map ;
26+ import java .util .Set ;
2527
2628/**
2729 * Catch-all holder class for static helper methods.
@@ -91,6 +93,21 @@ public static String makeStackTrace(Throwable throwable) {
9193 return text ;
9294 }
9395
96+ /**
97+ * Synchronizes on the set and then returns a copy of the set that is safe to iterate over. Useful when wanting to do thread-safe iteration over
98+ * a Set wrapped in {@link Collections#synchronizedSet(Set)}.
99+ *
100+ * @param set
101+ * The set, which may not be {@code null}
102+ * @return LinkedHashSet copy of the list
103+ */
104+ public static <E > Set <E > copy (final Set <E > set ) {
105+ // No Sonar: this very list instance can be synchronized in other places of its owning class
106+ synchronized (set ) { //NOSONAR
107+ return new LinkedHashSet <>(set );
108+ }
109+ }
110+
94111 /**
95112 * Synchronizes on the list and then returns a copy of the list that is safe to iterate over. Useful when wanting to do thread-safe iteration over
96113 * a List wrapped in {@link Collections#synchronizedList(List)}.
@@ -102,7 +119,7 @@ public static String makeStackTrace(Throwable throwable) {
102119 public static <E > List <E > copy (final List <E > list ) {
103120 // No Sonar: this very list instance can be synchronized in other places of its owning class
104121 synchronized (list ) { //NOSONAR
105- return new ArrayList <E >(list );
122+ return new ArrayList <>(list );
106123 }
107124 }
108125
@@ -117,7 +134,7 @@ public static <E> List<E> copy(final List<E> list) {
117134 public static <K , V > Map <K , V > copy (final Map <K , V > map ) {
118135 // No Sonar: this very map instance can be synchronized in other places of its owning class
119136 synchronized (map ) { //NOSONAR
120- return new LinkedHashMap <K , V >(map );
137+ return new LinkedHashMap <>(map );
121138 }
122139 }
123140}
0 commit comments