mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2024-12-27 07:26:48 +01:00
Make layout map actually immutable
- Also implement hashCode and equals on *ElementTypeImpl, Layout, and Layout.Element
This commit is contained in:
parent
060bf624a0
commit
eda75078af
4 changed files with 128 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
|||
package com.jozufozu.flywheel.impl.layout;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -9,20 +9,27 @@ import org.jetbrains.annotations.Unmodifiable;
|
|||
import com.jozufozu.flywheel.api.layout.ElementType;
|
||||
import com.jozufozu.flywheel.api.layout.Layout;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
||||
|
||||
final class LayoutImpl implements Layout {
|
||||
@Unmodifiable
|
||||
private final List<Element> elements;
|
||||
@Unmodifiable
|
||||
private final Map<String, ElementType> map;
|
||||
private final int byteSize;
|
||||
|
||||
LayoutImpl(List<Element> elements) {
|
||||
LayoutImpl(@Unmodifiable List<Element> elements) {
|
||||
this.elements = elements;
|
||||
|
||||
map = new HashMap<>();
|
||||
Object2ObjectOpenHashMap<String, ElementType> map = new Object2ObjectOpenHashMap<>();
|
||||
int byteSize = 0;
|
||||
for (Element element : this.elements) {
|
||||
map.put(element.name(), element.type());
|
||||
byteSize += element.type().byteSize();
|
||||
}
|
||||
map.trim();
|
||||
|
||||
this.map = Collections.unmodifiableMap(map);
|
||||
this.byteSize = byteSize;
|
||||
}
|
||||
|
||||
|
@ -43,6 +50,52 @@ final class LayoutImpl implements Layout {
|
|||
return byteSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + elements.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
LayoutImpl other = (LayoutImpl) obj;
|
||||
return elements.equals(other.elements);
|
||||
}
|
||||
|
||||
record ElementImpl(String name, ElementType type) implements Element {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + name.hashCode();
|
||||
result = prime * result + type.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
ElementImpl other = (ElementImpl) obj;
|
||||
return name.equals(other.name) && type.equals(other.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,4 +48,29 @@ final class MatrixElementTypeImpl implements MatrixElementType {
|
|||
public int byteSize() {
|
||||
return byteSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + repr.hashCode();
|
||||
result = prime * result + rows;
|
||||
result = prime * result + columns;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MatrixElementTypeImpl other = (MatrixElementTypeImpl) obj;
|
||||
return repr == other.repr && rows == other.rows && columns == other.columns;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,4 +21,27 @@ final class ScalarElementTypeImpl implements ScalarElementType {
|
|||
public int byteSize() {
|
||||
return byteSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + repr.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
ScalarElementTypeImpl other = (ScalarElementTypeImpl) obj;
|
||||
return repr == other.repr;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,4 +36,28 @@ final class VectorElementTypeImpl implements VectorElementType {
|
|||
public int byteSize() {
|
||||
return byteSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + repr.hashCode();
|
||||
result = prime * result + size;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
VectorElementTypeImpl other = (VectorElementTypeImpl) obj;
|
||||
return repr == other.repr && size == other.size;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue