Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use simpler Table and MultiMap instead of Guava's
- Loading branch information
Showing
12 changed files
with
264 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package mezz.jei.collect; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public class ListMultiMap<K, V> extends MultiMap<K, V, List<V>> { | ||
public ListMultiMap() { | ||
this(ArrayList::new); | ||
} | ||
|
||
public ListMultiMap(Supplier<List<V>> collectionSupplier) { | ||
super(collectionSupplier); | ||
} | ||
|
||
public ListMultiMap(Map<K, List<V>> map, Supplier<List<V>> collectionSupplier) { | ||
super(map, collectionSupplier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package mezz.jei.collect; | ||
|
||
import com.google.common.collect.ImmutableMultimap; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class MultiMap<K, V, T extends Collection<V>> { | ||
protected final Map<K, T> map; | ||
private final Function<K, T> collectionMappingFunction; | ||
|
||
public MultiMap(Supplier<T> collectionSupplier) { | ||
this(new HashMap<>(), collectionSupplier); | ||
} | ||
|
||
public MultiMap(Map<K, T> map, Supplier<T> collectionSupplier) { | ||
this.map = map; | ||
this.collectionMappingFunction = (k -> collectionSupplier.get()); | ||
} | ||
|
||
public T get(K key) { | ||
return map.computeIfAbsent(key, collectionMappingFunction); | ||
} | ||
|
||
public boolean put(K key, V value) { | ||
return get(key).add(value); | ||
} | ||
|
||
public boolean contains(K key, V value) { | ||
return get(key).contains(value); | ||
} | ||
|
||
public Set<Map.Entry<K, T>> entrySet() { | ||
return map.entrySet(); | ||
} | ||
|
||
public int getTotalSize() { | ||
int size = 0; | ||
for (T value : map.values()) { | ||
size += value.size(); | ||
} | ||
return size; | ||
} | ||
|
||
public ImmutableMultimap<K, V> toImmutable() { | ||
ImmutableMultimap.Builder<K, V> builder = ImmutableMultimap.builder(); | ||
for (Map.Entry<K, T> entry : map.entrySet()) { | ||
K key = entry.getKey(); | ||
for (V value : entry.getValue()) { | ||
builder.put(key, value); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package mezz.jei.collect; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
public class SetMultiMap<K, V> extends MultiMap<K, V, Set<V>> { | ||
public SetMultiMap() { | ||
this(HashSet::new); | ||
} | ||
|
||
public SetMultiMap(Supplier<Set<V>> collectionSupplier) { | ||
super(collectionSupplier); | ||
} | ||
|
||
public SetMultiMap(Map<K, Set<V>> map, Supplier<Set<V>> collectionSupplier) { | ||
super(map, collectionSupplier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package mezz.jei.collect; | ||
|
||
import com.google.common.collect.ImmutableTable; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class Table<R, C, V> { | ||
public static <R, C, V> Table<R, C, V> hashBasedTable() { | ||
return new Table<>(new HashMap<>(), HashMap::new); | ||
} | ||
|
||
private final Map<R, Map<C, V>> table; | ||
private final Function<R, Map<C, V>> rowMappingFunction; | ||
|
||
public Table(Map<R, Map<C, V>> table, Supplier<Map<C, V>> rowSupplier) { | ||
this.table = table; | ||
this.rowMappingFunction = (k -> rowSupplier.get()); | ||
} | ||
|
||
@Nullable | ||
public V get(R row, C col) { | ||
Map<C, V> rowMap = getRow(row); | ||
return rowMap.get(col); | ||
} | ||
|
||
public V computeIfAbsent(R row, C col, Supplier<V> valueSupplier) { | ||
Map<C, V> rowMap = getRow(row); | ||
return rowMap.computeIfAbsent(col, k -> valueSupplier.get()); | ||
} | ||
|
||
@Nullable | ||
public V put(R row, C col, V val) { | ||
Map<C, V> rowMap = getRow(row); | ||
return rowMap.put(col, val); | ||
} | ||
|
||
public Map<C, V> getRow(R row) { | ||
return table.computeIfAbsent(row, rowMappingFunction); | ||
} | ||
|
||
public ImmutableTable<R, C, V> toImmutable() { | ||
ImmutableTable.Builder<R, C, V> builder = ImmutableTable.builder(); | ||
for (Map.Entry<R, Map<C, V>> entry : table.entrySet()) { | ||
R row = entry.getKey(); | ||
for (Map.Entry<C, V> rowEntry : entry.getValue().entrySet()) { | ||
C col = rowEntry.getKey(); | ||
V val = rowEntry.getValue(); | ||
builder.put(row, col, val); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} |
17 changes: 8 additions & 9 deletions
17
src/main/java/mezz/jei/plugins/vanilla/brewing/BrewingRecipeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
src/main/java/mezz/jei/recipes/InternalRecipeRegistryPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.