Skip to content

Commit

Permalink
Merge pull request #49 from SkolkovoLab/pr/linked-hash-map-builders
Browse files Browse the repository at this point in the history
feat: replacing HashMap with LinkedHashMap
  • Loading branch information
yusshu authored Apr 19, 2024
2 parents d54d9cf + 98b3c0a commit 8cab051
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 55 deletions.
6 changes: 3 additions & 3 deletions api/src/main/java/team/unnamed/creative/ResourcePackImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@
import team.unnamed.creative.metadata.Metadata;
import team.unnamed.creative.metadata.MetadataPart;
import team.unnamed.creative.metadata.overlays.OverlayEntry;
import team.unnamed.creative.resources.MergeStrategy;
import team.unnamed.creative.overlay.Overlay;
import team.unnamed.creative.overlay.ResourceContainer;
import team.unnamed.creative.overlay.ResourceContainerImpl;
import team.unnamed.creative.resources.MergeStrategy;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;

import static java.util.Objects.requireNonNull;

final class ResourcePackImpl extends ResourceContainerImpl implements ResourcePack {

private final Map<String, Overlay> overlays = new HashMap<>();
private final Map<String, Overlay> overlays = new LinkedHashMap<>();

private @Nullable Writable icon;
private Metadata metadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import net.kyori.examination.string.StringExaminer;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
Expand Down Expand Up @@ -121,7 +121,7 @@ public Builder advances(Map<String, Integer> entries) {

public Builder advance(String character, int value) {
if (this.advances == null) {
this.advances = new HashMap<>();
this.advances = new LinkedHashMap<>();
}
this.advances.put(character, value);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
Expand Down Expand Up @@ -99,7 +99,7 @@ public int hashCode() {
}

static final class BuilderImpl implements Builder {
private final Map<Class<?>, MetadataPart> parts = new HashMap<>();
private final Map<Class<?>, MetadataPart> parts = new LinkedHashMap<>();

@Override
public @NotNull Builder parts(final @NotNull Collection<MetadataPart> parts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import team.unnamed.creative.base.CubeFace;
import team.unnamed.creative.base.Vector3Float;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
Expand Down Expand Up @@ -138,7 +138,7 @@ static final class BuilderImpl implements Builder {
private Vector3Float to;
private ElementRotation rotation = null;
private boolean shade = DEFAULT_SHADE;
private Map<CubeFace, ElementFace> faces = new HashMap<>();
private Map<CubeFace, ElementFace> faces = new LinkedHashMap<>();


@Override
Expand Down Expand Up @@ -167,7 +167,7 @@ static final class BuilderImpl implements Builder {

@Override
public @NotNull Builder faces(final @NotNull Map<CubeFace, ElementFace> faces) {
this.faces = new HashMap<>(requireNonNull(faces, "faces"));
this.faces = new LinkedHashMap<>(requireNonNull(faces, "faces"));
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions api/src/main/java/team/unnamed/creative/model/ModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.jetbrains.annotations.Unmodifiable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -152,7 +152,7 @@ static final class BuilderImpl implements Builder {
private Key key;
private Key parent;
private boolean ambientOcclusion = DEFAULT_AMBIENT_OCCLUSION;
private Map<ItemTransform.Type, ItemTransform> display = new HashMap<>();
private Map<ItemTransform.Type, ItemTransform> display = new LinkedHashMap<>();
private ModelTextures textures = ModelTextures.EMPTY;
private GuiLight guiLight;
private List<Element> elements = new ArrayList<>();
Expand All @@ -179,7 +179,7 @@ static final class BuilderImpl implements Builder {
@Override
public @NotNull Builder display(final @NotNull Map<ItemTransform.Type, ItemTransform> display) {
requireNonNull(display, "display");
this.display = new HashMap<>(display);
this.display = new LinkedHashMap<>(display);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -237,7 +237,7 @@ private Builder() {
@Contract("_ -> this")
public @NotNull Builder variables(final @NotNull Map<String, ModelTexture> variables) {
requireNonNull(variables, "variables");
this.variables = new HashMap<>(variables);
this.variables = new LinkedHashMap<>(variables);
return this;
}

Expand All @@ -246,7 +246,7 @@ private Builder() {
requireNonNull(id, "id");
requireNonNull(texture, "texture");
if (variables == null) {
variables = new HashMap<>();
variables = new LinkedHashMap<>();
}
variables.put(id, texture);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -56,17 +56,17 @@
@ApiStatus.Internal
public class ResourceContainerImpl implements ResourceContainer {

private final Map<Key, Atlas> atlases = new HashMap<>();
private final Map<Key, BlockState> blockStates = new HashMap<>();
private final Map<Key, Font> fonts = new HashMap<>();
private final Map<Key, Language> languages = new HashMap<>();
private final Map<Key, Model> models = new HashMap<>();
private final Map<String, SoundRegistry> soundRegistries = new HashMap<>();
private final Map<Key, Sound> sounds = new HashMap<>();
private final Map<Key, Texture> textures = new HashMap<>();
private final Map<Key, Atlas> atlases = new LinkedHashMap<>();
private final Map<Key, BlockState> blockStates = new LinkedHashMap<>();
private final Map<Key, Font> fonts = new LinkedHashMap<>();
private final Map<Key, Language> languages = new LinkedHashMap<>();
private final Map<Key, Model> models = new LinkedHashMap<>();
private final Map<String, SoundRegistry> soundRegistries = new LinkedHashMap<>();
private final Map<Key, Sound> sounds = new LinkedHashMap<>();
private final Map<Key, Texture> textures = new LinkedHashMap<>();

// Unknown files we don't know how to parse
private final Map<String, Writable> files = new HashMap<>();
private final Map<String, Writable> files = new LinkedHashMap<>();

//#region Atlases (Keyed)
@Override
Expand Down Expand Up @@ -348,7 +348,7 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
continue;
}

final Map<String, String> translations = new HashMap<>(oldLanguage.translations());
final Map<String, String> translations = new LinkedHashMap<>(oldLanguage.translations());
for (final Map.Entry<String, String> translation : language.translations().entrySet()) {
final String replaced = translations.put(translation.getKey(), translation.getValue());
if (replaced != null && strategy == MergeStrategy.mergeAndFailOnError()) {
Expand Down Expand Up @@ -386,7 +386,7 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
continue;
}

final Map<Key, SoundEvent> soundEvents = new HashMap<>();
final Map<Key, SoundEvent> soundEvents = new LinkedHashMap<>();
for (final SoundEvent soundEvent : oldSoundRegistry.sounds()) {
soundEvents.put(soundEvent.key(), soundEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
import team.unnamed.creative.util.Keys;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -55,7 +56,7 @@ final class SoundRegistryImpl implements SoundRegistry {
) {
this.namespace = requireNonNull(namespace, "namespace");
requireNonNull(sounds, "sounds");
this.sounds = new HashMap<>();
this.sounds = new LinkedHashMap<>();
for (SoundEvent soundEvent : sounds) {
this.sounds.put(soundEvent.key(), soundEvent);
}
Expand Down Expand Up @@ -138,7 +139,7 @@ static final class BuilderImpl implements Builder {
@Override
public @NotNull Builder sounds(final @NotNull Collection<? extends SoundEvent> sounds) {
requireNonNull(sounds, "sounds");
this.sounds = new HashSet<>(sounds);
this.sounds = new LinkedHashSet<>(sounds);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -49,7 +49,7 @@ public static <T> List<T> immutableListOf(List<T> list) {
public static <K, V> Map<K, V> immutableMapOf(Map<K, V> map) {
return map.isEmpty()
? Collections.emptyMap()
: Collections.unmodifiableMap(new HashMap<>(map));
: Collections.unmodifiableMap(new LinkedHashMap<>(map));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Queue;

Expand Down Expand Up @@ -77,7 +79,7 @@ private MinecraftResourcePackReaderImpl(
// waiting for textures (because we can't know the order
// they come in)
// (null key means it is root resource pack)
Map<@Nullable String, Map<Key, Texture>> incompleteTextures = new HashMap<>();
Map<@Nullable String, Map<Key, Texture>> incompleteTextures = new LinkedHashMap<>();

while (reader.hasNext()) {
String path = reader.next();
Expand Down Expand Up @@ -212,7 +214,7 @@ private MinecraftResourcePackReaderImpl(
Key key = Key.key(namespace, keyOfMetadata);
Metadata metadata = MetadataSerializer.INSTANCE.readFromTree(parseJson(reader.stream()));

Map<Key, Texture> incompleteTexturesThisContainer = incompleteTextures.computeIfAbsent(overlayDir, k -> new HashMap<>());
Map<Key, Texture> incompleteTexturesThisContainer = incompleteTextures.computeIfAbsent(overlayDir, k -> new LinkedHashMap<>());
Texture texture = incompleteTexturesThisContainer.remove(key);
if (texture == null) {
// metadata was found first, put
Expand All @@ -224,7 +226,7 @@ private MinecraftResourcePackReaderImpl(
} else {
Key key = Key.key(namespace, categoryPath);
Writable data = reader.content().asWritable();
Map<Key, Texture> incompleteTexturesThisContainer = incompleteTextures.computeIfAbsent(overlayDir, k -> new HashMap<>());
Map<Key, Texture> incompleteTexturesThisContainer = incompleteTextures.computeIfAbsent(overlayDir, k -> new LinkedHashMap<>());
Texture waiting = incompleteTexturesThisContainer.remove(key);

if (waiting == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
import team.unnamed.creative.serialize.minecraft.sound.SoundSerializer;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class ResourceCategories {

private static final Map<String, ResourceCategory<?>> CATEGORIES;

static {
CATEGORIES = new HashMap<>();
CATEGORIES = new LinkedHashMap<>();
registerCategory(AtlasSerializer.CATEGORY);
registerCategory(SoundSerializer.CATEGORY);
registerCategory(ModelSerializer.CATEGORY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -216,7 +216,7 @@ static AtlasSource deserialize(JsonObject node) {
@Subst("minecraft:resource")
String paletteKeyStr = node.get("palette_key").getAsString();
Key paletteKey = Key.key(paletteKeyStr);
Map<String, Key> permutations = new HashMap<>();
Map<String, Key> permutations = new LinkedHashMap<>();
for (Map.Entry<String, JsonElement> entry : node.getAsJsonObject("permutations").entrySet()) {
@Subst("minecraft:resource")
String value = entry.getValue().getAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -95,7 +95,7 @@ public BlockState deserializeFromJson(JsonElement node, Key key) {

JsonObject objectNode = node.getAsJsonObject();

Map<String, MultiVariant> variants = new HashMap<>();
Map<String, MultiVariant> variants = new LinkedHashMap<>();
List<Selector> multipart = new ArrayList<>();

// read variants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -194,7 +194,7 @@ private static void writeSpace(JsonWriter writer, SpaceFontProvider provider) th

private static SpaceFontProvider readSpace(JsonObject node) {
JsonObject advancesNode = node.getAsJsonObject("advances");
Map<String, Integer> advances = new HashMap<>();
Map<String, Integer> advances = new LinkedHashMap<>();
for (Map.Entry<String, JsonElement> advanceEntryNode : advancesNode.entrySet()) {
String character = advanceEntryNode.getKey();
int advance = advanceEntryNode.getValue().getAsInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import team.unnamed.creative.serialize.minecraft.io.JsonResourceSerializer;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public final class LanguageSerializer implements JsonResourceSerializer<Language>, JsonResourceDeserializer<Language> {
Expand Down Expand Up @@ -70,7 +70,7 @@ public void serializeToJson(Language language, JsonWriter writer) throws IOExcep
@Override
public Language deserializeFromJson(JsonElement node, Key key) {
JsonObject objectNode = node.getAsJsonObject();
Map<String, String> translations = new HashMap<>();
Map<String, String> translations = new LinkedHashMap<>();

for (Map.Entry<String, JsonElement> translationEntry : objectNode.entrySet()) {
String translationKey = translationEntry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import team.unnamed.creative.serialize.minecraft.GsonUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

final class LanguageMetaCodec implements MetadataPartCodec<LanguageMeta> {
Expand All @@ -49,7 +49,7 @@ final class LanguageMetaCodec implements MetadataPartCodec<LanguageMeta> {

@Override
public @NotNull LanguageMeta read(final @NotNull JsonObject node) {
Map<String, LanguageEntry> languages = new HashMap<>();
Map<String, LanguageEntry> languages = new LinkedHashMap<>();

for (Map.Entry<String, JsonElement> entry : node.entrySet()) {
String code = entry.getKey();
Expand Down
Loading

0 comments on commit 8cab051

Please sign in to comment.