Implement context merging, fixes #85

This commit is contained in:
Draycia committed 2020-09-28 02:11:14 -07:00
1 parent c79c1bcbee
commit 2ee3392308
2 files changed
+68 -11

No files matched your search

@@ -70,10 +70,29 @@ public class ChannelOptions {
return this.color;
}
@Nullable
public Context context(final @NonNull String key) {
final Context localContext;
if (this.contexts != null) {
localContext = this.contexts.get(key);
} else {
localContext = null;
}
if (localContext == null) {
if (this.defaultOptions().contexts() != null) {
return this.defaultOptions().contexts().get(key);
} else {
return null;
}
}
return localContext;
}
@Nullable
public Map<String, Context> contexts() {
// TODO: properly implement default context inheritence
// TODO: perhaps add an option to make it so overriding any context overrides all?
if (this.contexts == null) {
return this.defaultOptions().contexts();
}
@@ -82,18 +101,40 @@ public class ChannelOptions {
}
@Nullable
public String format(final String key) {
if (this.formats == null) {
return this.defaultOptions().formats().get(key);
public Map<String, Context> contextsAndDefault() {
if (this.contexts == null) {
return this.defaultOptions().contexts();
}
final String format = this.formats.get(key);
if (format != null) {
return format;
if (this.defaultOptions().contexts() == null) {
return this.contexts;
}
return this.defaultOptions().formats().get(key);
final Map<String, Context> contexts = new HashMap<>(this.defaultOptions().contexts());
contexts.putAll(this.contexts);
return contexts;
}
@Nullable
public String format(final @NonNull String key) {
final String localFormat;
if (this.formats() != null) {
localFormat = this.formats().get(key);
} else {
localFormat = null;
}
if (localFormat == null) {
if (this.defaultOptions().formats() != null) {
return this.defaultOptions().formats().get(key);
} else {
return null;
}
}
return localFormat;
}
@Nullable
@@ -105,6 +146,22 @@ public class ChannelOptions {
return this.formats;
}
@Nullable
public Map<String, String> formatsAndDefault() {
if (this.formats == null) {
return this.defaultOptions().formats();
}
if (this.defaultOptions().formats() == null) {
return this.formats;
}
final Map<String, String> formats = new HashMap<>(this.defaultOptions().formats());
formats.putAll(this.formats);
return formats;
}
@Nullable
public String defaultFormatName() {
if (this.defaultFormatName == null || this.defaultFormatName.isEmpty()) {
@@ -515,7 +515,7 @@ public class CarbonChatChannel implements ChatChannel {
@Override
@Nullable
public Context context(final @NonNull String key) {
return this.options().contexts().get(key);
return this.options().context(key);
}
@Override