Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance/placeholders #97

Merged
merged 7 commits into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.iridium.iridiumcore.utils;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import lombok.Getter;
Expand All @@ -11,8 +13,10 @@
public class Placeholder {

private final String key;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semi breaking change, nothing should reference the key directly, but key is without percent signs, formattedKey is with signs

private final String formattedKey;
private String value;
private Supplier<String> supplier;
private Function<Integer, String> int_supplier;

/**
* The default constructor.
Expand All @@ -21,7 +25,8 @@ public class Placeholder {
* @param value The actual value of the placeholder
*/
public Placeholder(String key, String value) {
this.key = "%" + key + "%";
this.key = key;
this.formattedKey = "%" + key + "%";
this.value = value;
}

Expand All @@ -32,15 +37,34 @@ public Placeholder(String key, String value) {
* @param value The actual value of the placeholder
*/
public Placeholder(String key, Supplier<String> supplier) {
this.key = "%" + key + "%";
this.key = key;
this.formattedKey = "%" + key + "%";
this.supplier = supplier;
}

public String getValue()
{
/**
* The default constructor.
*
* @param key The placeholder without curly brackets.
* @param value The actual value of the placeholder
*/
public Placeholder(String key, Function<Integer, String> supplier) {
this.key = key;
this.formattedKey = "%" + key + "%";
this.int_supplier = supplier;
}

public String getValue() {
if(value==null && supplier!=null) value = supplier.get();
return value;
return value;
boiscljo marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines 58 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a case where both value and supplier can be null here now no? Is this an issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an issue with normal placeholder it will just return null without throwing

}

public String getValue(int i) {
if(this.int_supplier!=null)
return this.int_supplier.apply(i);
return getValue();
}

/**
* Replaces this placeholder in the provided line with the value of this
* placeholder.
Expand All @@ -52,8 +76,8 @@ public String getValue()
public String process(String line) {
if (line == null || line.isEmpty())
return "";
if (line.contains(key))
return line.replace(key, getValue());
if (line.contains(formattedKey))
return line.replace(formattedKey, getValue());
else
return line;
}
Expand Down
Loading