Skip to content

Commit

Permalink
fix (core): Fix @immutable in StringToLongBiMap (TX #gemini)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Jan 14, 2025
1 parent 10ce834 commit 2b37ec3
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
package dev.enola.common.string2long;

import static com.google.common.collect.ImmutableList.copyOf;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;

Expand All @@ -27,12 +30,11 @@
@Immutable
public class ImmutableStringToLongBiMap implements StringToLongBiMap {

@SuppressWarnings("Immutable") // TODO Is there a way to remove this?
private final String[] symbols;

private final ImmutableMap<String, Integer> symbolsMap;
private final ImmutableList<String> symbols;

private ImmutableStringToLongBiMap(ImmutableMap<String, Integer> symbolsMap, String[] symbols) {
private ImmutableStringToLongBiMap(
ImmutableMap<String, Integer> symbolsMap, ImmutableList<String> symbols) {
this.symbolsMap = symbolsMap;
this.symbols = symbols;
}
Expand All @@ -46,15 +48,15 @@ public long get(String symbol) throws IllegalArgumentException {

@Override
public String get(long id) throws IllegalArgumentException {
if (id >= 0 && id < symbols.length) return symbols[(int) id];
if (id >= 0 && id < symbols.size()) return symbols.get((int) id);
else
throw new IllegalArgumentException(
Long.toUnsignedString(id)); // TODO String.valueOf(id) ?
}

@Override
public long size() {
return symbols.length;
return symbols.size();
}

public static Builder builder() {
Expand Down Expand Up @@ -88,7 +90,7 @@ public StringToLongBiMap build() {
if (array[id] != null) throw new IllegalStateException();
array[id] = symbol;
});
return new ImmutableStringToLongBiMap(immutableMapBuilder.build(), array);
return new ImmutableStringToLongBiMap(immutableMapBuilder.build(), copyOf(array));
}
}
}

0 comments on commit 2b37ec3

Please sign in to comment.