Skip to content

Commit

Permalink
Performance/placeholders (#97)
Browse files Browse the repository at this point in the history
Co-authored-by: Peaches_MLG <[email protected]>
  • Loading branch information
boiscljo and PeachesMLG authored Dec 27, 2024
1 parent bb1c0e0 commit 94b9620
Showing 1 changed file with 31 additions and 7 deletions.
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;
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;
}

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

0 comments on commit 94b9620

Please sign in to comment.