Skip to content

Commit

Permalink
Optimized ArrayList usage by replacing it with sequential lists when …
Browse files Browse the repository at this point in the history
…possible
  • Loading branch information
OmerBenGera committed Jun 24, 2022
1 parent 39676eb commit fb5b0fd
Show file tree
Hide file tree
Showing 174 changed files with 1,270 additions and 1,111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,76 @@
import com.bgsoftware.superiorskyblock.api.objects.Pair;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

public class DatabaseFilter {
public abstract class DatabaseFilter {

private final Collection<Pair<String, Object>> filters;
private static DatabaseFilterEmpty EMPTY_FILTER;

public DatabaseFilter(Collection<Pair<String, Object>> filters) {
this.filters = Collections.unmodifiableCollection(filters);
public static DatabaseFilter fromFilter(String filterKey, Object filterValue) {
return new DatabaseFilterSingle(filterKey, filterValue);
}

public Collection<Pair<String, Object>> getFilters() {
return filters;
public static DatabaseFilter fromFilters(List<Pair<String, Object>> filters) {
if (filters.isEmpty()) {
if (EMPTY_FILTER == null)
EMPTY_FILTER = new DatabaseFilterEmpty();

return EMPTY_FILTER;
} else if (filters.size() == 1) {
Pair<String, Object> filter = filters.get(0);
return fromFilter(filter.getKey(), filter.getValue());
} else {
return new DatabaseFilterList(filters);
}
}

protected DatabaseFilter() {
}

public abstract void forEach(BiConsumer<String, Object> consumer);

private static class DatabaseFilterList extends DatabaseFilter {

private final Collection<Pair<String, Object>> filters;

DatabaseFilterList(Collection<Pair<String, Object>> filters) {
this.filters = filters;
}

@Override
public void forEach(BiConsumer<String, Object> consumer) {
filters.forEach(pair -> consumer.accept(pair.getKey(), pair.getValue()));
}

}

private static class DatabaseFilterEmpty extends DatabaseFilter {

@Override
public void forEach(BiConsumer<String, Object> consumer) {
// Do nothing.
}

}

private static class DatabaseFilterSingle extends DatabaseFilter {

private final String filterKey;
private final Object filterValue;

DatabaseFilterSingle(String filterKey, Object filterValue) {
this.filterKey = filterKey;
this.filterValue = filterValue;
}

@Override
public void forEach(BiConsumer<String, Object> consumer) {
consumer.accept(filterKey, filterValue);
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
Expand Down Expand Up @@ -47,7 +47,7 @@ public IslandUpgradeEvent(@Nullable SuperiorPlayer superiorPlayer, Island island
this.superiorPlayer = superiorPlayer;
this.upgrade = Preconditions.checkNotNull(upgrade, "upgrade cannot be null");
this.upgradeLevel = Preconditions.checkNotNull(island.getUpgradeLevel(upgrade), "upgradeLevel cannot be null");
this.commands = new ArrayList<>(Preconditions.checkNotNull(commands, "commands cannot be null"));
this.commands = new LinkedList<>(Preconditions.checkNotNull(commands, "commands cannot be null"));
this.upgradeCost = upgradeCost;
}

Expand All @@ -69,7 +69,7 @@ public IslandUpgradeEvent(@Nullable SuperiorPlayer superiorPlayer, Island island
this.superiorPlayer = superiorPlayer;
this.upgrade = Preconditions.checkNotNull(upgrade, "upgrade cannot be null");
this.upgradeLevel = Preconditions.checkNotNull(upgradeLevel, "upgradeLevel cannot be null");
this.commands = new ArrayList<>(Preconditions.checkNotNull(commands, "commands cannot be null"));
this.commands = new LinkedList<>(Preconditions.checkNotNull(commands, "commands cannot be null"));
this.upgradeCost = upgradeCost;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ void createIsland(SuperiorPlayer superiorPlayer, String schemName, BigDecimal bo
*
* @param sortingType The sorting type to order the list by.
* @return A list of uuids of the island owners.
* @deprecated See {@link #getIslands(SortingType)}
*/
@Deprecated
List<UUID> getAllIslands(SortingType sortingType);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -20,8 +22,8 @@

public abstract class Mission<V> {

private final List<String> requiredMissions = new ArrayList<>();
private final List<String> requiredChecks = new ArrayList<>();
private final List<String> requiredMissions = new LinkedList<>();
private final List<String> requiredChecks = new LinkedList<>();
private final Map<SuperiorPlayer, V> missionData = new ConcurrentHashMap<>();

private String name = null;
Expand Down Expand Up @@ -115,14 +117,14 @@ public void addRequiredCheck(String... checks) {
* Get the required missions for completing this mission.
*/
public List<String> getRequiredMissions() {
return new ArrayList<>(requiredMissions);
return Collections.unmodifiableList(requiredMissions);
}

/**
* Get the required checks for completing this mission.
*/
public List<String> getRequiredChecks() {
return new ArrayList<>(requiredChecks);
return Collections.unmodifiableList(requiredChecks);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.SortingType;
import com.bgsoftware.superiorskyblock.core.SequentialListBuilder;
import com.bgsoftware.superiorskyblock.island.top.SortingTypes;
import me.robin.leaderheads.datacollectors.DataCollector;
import me.robin.leaderheads.objects.BoardType;
Expand All @@ -11,7 +12,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.function.Function;

@SuppressWarnings("unused")
public class LeaderHeadsHook {
Expand Down Expand Up @@ -80,6 +81,23 @@ public Double getValue(Island island) {

private static abstract class SuperiorDataController extends DataCollector {

private final Function<Island, Map.Entry<?, Double>> ISLAND_MAPPER = island -> new Map.Entry<UUID, Double>() {
@Override
public UUID getKey() {
return island.getOwner().getUniqueId();
}

@Override
public Double getValue() {
return SuperiorDataController.this.getValue(island);
}

@Override
public Double setValue(Double value) {
return null;
}
};

private final SortingType sortingType;

SuperiorDataController(String name, String command, SortingType sortingType) {
Expand All @@ -98,22 +116,8 @@ private static abstract class SuperiorDataController extends DataCollector {

@Override
public List<Map.Entry<?, Double>> requestAll() {
return plugin.getGrid().getIslands(sortingType).stream().map(island -> new Map.Entry<UUID, Double>() {
@Override
public UUID getKey() {
return island.getOwner().getUniqueId();
}

@Override
public Double getValue() {
return SuperiorDataController.this.getValue(island);
}

@Override
public Double setValue(Double value) {
return null;
}
}).collect(Collectors.toList());
return new SequentialListBuilder<Map.Entry<?, Double>>()
.build(plugin.getGrid().getIslands(sortingType), ISLAND_MAPPER);
}

public abstract Double getValue(Island island);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

Expand All @@ -26,7 +26,7 @@ public class OraxenHook {
private static final List<Pair<MechanicFactory, SetBlockModelFunction>> AVAILABLE_MECHANICS;

static {
List<Pair<MechanicFactory, SetBlockModelFunction>> availableMechanics = new ArrayList<>();
List<Pair<MechanicFactory, SetBlockModelFunction>> availableMechanics = new LinkedList<>();

try {
Class.forName("io.th0rgal.oraxen.mechanics.provided.gameplay.block.BlockMechanicFactory");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
import javax.annotation.Nullable;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Optional;

public class SuperiorSkyblockPlugin extends JavaPlugin implements SuperiorSkyblock {
Expand Down Expand Up @@ -439,9 +438,8 @@ private void loadGeneratorFromFile() {
if (generatorsFilesList != null) {
for (File file : generatorsFilesList) {
//noinspection deprecation
List<Class<?>> generatorClasses = JarFiles.getClasses(file.toURL(), ChunkGenerator.class);
if (!generatorClasses.isEmpty()) {
Class<?> generatorClass = generatorClasses.get(0);
Class<?> generatorClass = JarFiles.getClass(file.toURL(), ChunkGenerator.class);
if (generatorClass != null) {
for (Constructor<?> constructor : generatorClass.getConstructors()) {
if (constructor.getParameterCount() == 0) {
worldGenerator = (ChunkGenerator) generatorClass.newInstance();
Expand Down
Loading

0 comments on commit fb5b0fd

Please sign in to comment.