Skip to content

Commit 73cb021

Browse files
committed
Fixing UnsupportedOperationException
1 parent d4c4e06 commit 73cb021

File tree

2 files changed

+86
-15
lines changed

2 files changed

+86
-15
lines changed

src/main/java/me/croabeast/file/Configurable.java

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,43 @@ default <T> T get(String path, Class<T> clazz) {
8686
*/
8787
@SuppressWarnings("unchecked")
8888
default <T> T get(String path, T def) {
89-
return (T) getConfiguration().get(path, def);
89+
try {
90+
return (T) getConfiguration().get(path, def);
91+
} catch (Exception e) {
92+
return def;
93+
}
94+
}
95+
96+
/**
97+
* Retrieves a list of values from the configuration at the specified path, with a guaranteed non-empty result.
98+
* <p>
99+
* Behavior:
100+
* <ul>
101+
* <li>If the value at {@code path} is a list, each element is attempted to be cast to {@code T} and
102+
* collected into the resulting list. Elements that fail to cast are ignored.</li>
103+
* <li>If the list exists but ends up empty after casting, a singleton list containing {@code def} is returned.</li>
104+
* <li>If the value at {@code path} is not a list, this method returns a singleton list containing the value
105+
* at {@code path} (or {@code def} if the path is absent).</li>
106+
* </ul>
107+
*
108+
* @param path the configuration path to read.
109+
* @param def the fallback element used when no valid values are found.
110+
* @param <T> the expected element type of the list.
111+
* @return a non-empty {@link List} of {@code T} derived from the configuration, or a singleton list with {@code def}.
112+
*/
113+
@SuppressWarnings("unchecked")
114+
default <T> List<T> getList(String path, T def) {
115+
if (getConfiguration().isList(path)) {
116+
List<T> list = new ArrayList<>();
117+
for (Object o : getConfiguration().getList(path, new ArrayList<>()))
118+
try {
119+
list.add((T) o);
120+
} catch (Exception ignored) {}
121+
122+
return (list.isEmpty()) ? new ArrayList<>(Collections.singletonList(def)) : list;
123+
}
124+
125+
return new ArrayList<>(Collections.singletonList(get(path, def)));
90126
}
91127

92128
/**
@@ -132,6 +168,18 @@ default ConfigurationSection getSection(String path) {
132168
return StringUtils.isBlank(path) ? getConfiguration() : getConfiguration().getConfigurationSection(path);
133169
}
134170

171+
/**
172+
* Retrieves a list of strings from the configuration at the specified path.
173+
*
174+
* @param path the path to the list.
175+
* @param defaultList the default list if the path doesn't exist
176+
*
177+
* @return a list of strings from the configuration, or defaultList if the path does not exist.
178+
*/
179+
default List<String> toStringList(String path, List<String> defaultList) {
180+
return toStringList(getConfiguration(), path, defaultList);
181+
}
182+
135183
/**
136184
* Retrieves a list of strings from the configuration at the specified path.
137185
*
@@ -176,13 +224,14 @@ default List<String> getKeys(String path) {
176224
@NotNull
177225
default Map<String, ConfigurationSection> getSections(String path, boolean deep) {
178226
Map<String, ConfigurationSection> map = new LinkedHashMap<>();
227+
179228
ConfigurationSection section = getSection(path);
180-
if (section != null) {
229+
if (section != null)
181230
for (String key : section.getKeys(deep)) {
182231
ConfigurationSection c = section.getConfigurationSection(key);
183232
if (c != null) map.put(key, c);
184233
}
185-
}
234+
186235
return map;
187236
}
188237

@@ -231,16 +280,21 @@ default <U extends ConfigurableUnit> UnitMappable.Set<U> asUnitMap(String path,
231280
*/
232281
static List<String> toStringList(ConfigurationSection section, String path, List<String> def) {
233282
if (section == null) return def;
283+
234284
if (!section.isList(path)) {
235-
final Object temp = section.get(path);
236-
return temp != null ? Collections.singletonList(temp.toString()) : def;
285+
Object temp = section.get(path);
286+
return temp != null ?
287+
new ArrayList<>(Collections.singletonList(temp.toString())) :
288+
def;
237289
}
290+
238291
List<?> raw = section.getList(path, new ArrayList<>());
239292
if (!raw.isEmpty()) {
240293
List<String> list = new ArrayList<>();
241294
raw.forEach(o -> list.add(o.toString()));
242295
return list;
243296
}
297+
244298
return def;
245299
}
246300

@@ -270,22 +324,27 @@ static List<String> toStringList(ConfigurationSection section, String path) {
270324
static SectionMappable.Set toSectionMap(@Nullable ConfigurationSection section, @Nullable String path) {
271325
if (StringUtils.isNotBlank(path) && section != null)
272326
section = section.getConfigurationSection(path);
273-
if (section == null)
274-
return SectionMappable.asSet();
327+
328+
if (section == null) return SectionMappable.asSet();
329+
275330
Set<String> sectionKeys = section.getKeys(false);
276-
if (sectionKeys.isEmpty())
277-
return SectionMappable.asSet();
331+
if (sectionKeys.isEmpty()) return SectionMappable.asSet();
332+
278333
Map<Integer, Set<ConfigurationSection>> map = new HashMap<>();
279334
for (String key : sectionKeys) {
280335
ConfigurationSection id = section.getConfigurationSection(key);
281336
if (id == null) continue;
337+
282338
String perm = id.getString("permission", "DEFAULT");
339+
283340
int def = perm.matches("(?i)default") ? 0 : 1;
284341
int priority = id.getInt("priority", def);
342+
285343
Set<ConfigurationSection> m = map.getOrDefault(priority, new LinkedHashSet<>());
286344
m.add(id);
287345
map.put(priority, m);
288346
}
347+
289348
return SectionMappable.asSet(map).order(false);
290349
}
291350

src/main/java/me/croabeast/file/MapUtils.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ static abstract class AbstractSection<C extends Collection<ConfigurationSection>
7373
/**
7474
* Creates an empty {@code AbstractSection}.
7575
*/
76-
AbstractSection() {}
76+
AbstractSection() {
77+
super();
78+
}
7779

7880
/**
7981
* Creates a new {@code AbstractSection} instance populated with the provided map.
@@ -92,7 +94,9 @@ static final class SetImpl extends AbstractSection<java.util.Set<ConfigurationSe
9294
/**
9395
* Creates an empty {@code SetImpl} instance.
9496
*/
95-
SetImpl() {}
97+
SetImpl() {
98+
super();
99+
}
96100

97101
/**
98102
* Creates a {@code SetImpl} instance populated with the provided map.
@@ -175,7 +179,9 @@ static final class ListImpl extends AbstractSection<java.util.List<Configuration
175179
/**
176180
* Creates an empty {@code ListImpl} instance.
177181
*/
178-
ListImpl() {}
182+
ListImpl() {
183+
super();
184+
}
179185

180186
/**
181187
* Creates a {@code ListImpl} instance populated with the provided map.
@@ -268,7 +274,9 @@ static abstract class AbstractUnit<U extends ConfigurableUnit, C extends Collect
268274
/**
269275
* Creates an empty {@code AbstractUnit}.
270276
*/
271-
AbstractUnit() {}
277+
AbstractUnit() {
278+
super();
279+
}
272280

273281
/**
274282
* Creates a new {@code AbstractUnit} instance populated with the provided map.
@@ -289,7 +297,9 @@ static final class SetImpl<U extends ConfigurableUnit> extends AbstractUnit<U, j
289297
/**
290298
* Creates an empty {@code SetImpl} instance.
291299
*/
292-
SetImpl() {}
300+
SetImpl() {
301+
super();
302+
}
293303

294304
/**
295305
* Creates a {@code SetImpl} instance populated with the provided map.
@@ -358,7 +368,9 @@ static final class ListImpl<U extends ConfigurableUnit> extends AbstractUnit<U,
358368
/**
359369
* Creates an empty {@code ListImpl} instance.
360370
*/
361-
ListImpl() {}
371+
ListImpl() {
372+
super();
373+
}
362374

363375
/**
364376
* Creates a {@code ListImpl} instance populated with the provided map.

0 commit comments

Comments
 (0)