Skip to content

Commit

Permalink
🔧 Optimize hashCode() for datatypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Nov 10, 2023
1 parent d52c26c commit d0d3eb9
Show file tree
Hide file tree
Showing 29 changed files with 64 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build/
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/sonarlint/
*.iws
*.iml
*.ipr
Expand Down
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
project_version=1.2.1+patch.1
project_version=1.2.2
4 changes: 2 additions & 2 deletions src/main/java/com/ultreon/data/DataIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static <T extends IType<?>> T readCompressed(InputStream stream, T... typ
GZIPInputStream gzipStream = new GZIPInputStream(stream);
return read(gzipStream, type);
}

public static void write(IType<?> type, File file) throws IOException {
try (FileOutputStream stream = new FileOutputStream(file)) {
write(type, stream);
Expand All @@ -89,7 +89,7 @@ public static void write(IType<?> type, OutputStream stream) throws IOException
type.write(outputStream);
outputStream.flush();
}

public static void writeCompressed(IType<?> type, URL file) throws IOException {
try (OutputStream stream = file.openConnection().getOutputStream()) {
writeCompressed(type, stream);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/FutureVersionException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ultreon.data;

public class FutureVersionException extends RuntimeException {
public class FutureVersionException extends IllegalStateException {
private final short read;
private final short current;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/IReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ultreon.data.types.IType;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.IOException;

@FunctionalInterface
public interface IReader<T extends IType<?>> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/TypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ultreon.data.types.*;

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

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/BigDecType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Objects;

public class BigDecType implements IType<BigDecimal> {
private BigDecimal obj;
Expand Down Expand Up @@ -58,12 +57,12 @@ public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof BigDecType)) return false;
BigDecType that = (BigDecType) other;
return Objects.equals(obj, that.obj);
return obj.equals(that.obj);
}

@Override
public int hashCode() {
return Objects.hash(obj);
return obj.hashCode();
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/BigIntType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Objects;

public class BigIntType implements IType<BigInteger> {
private BigInteger obj;
Expand Down Expand Up @@ -55,12 +54,12 @@ public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof BigIntType)) return false;
BigIntType that = (BigIntType) other;
return Objects.equals(obj, that.obj);
return obj.equals(that.obj);
}

@Override
public int hashCode() {
return Objects.hash(obj);
return obj.hashCode();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ultreon/data/types/BitSetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void write(DataOutputStream stream) throws IOException {
public static BitSetType read(DataInputStream stream) throws IOException {
int len = stream.readUnsignedShort();
byte[] arr = new byte[len];
for (int i = 0; i < len; i++) {
for (int i = 0; i < len; i++) {
arr[i] = stream.readByte();
}
return new BitSetType(arr);
Expand All @@ -63,7 +63,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return obj.hashCode();
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/ultreon/data/types/BooleanType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Objects;

public class BooleanType implements IType<Boolean> {
private boolean obj;
Expand Down Expand Up @@ -49,7 +48,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return obj ? 1231 : 1237;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ultreon/data/types/ByteArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class ByteArrayType implements IType<byte[]> {
Expand Down Expand Up @@ -41,7 +41,7 @@ public void write(DataOutputStream stream) throws IOException {
public static ByteArrayType read(DataInputStream stream) throws IOException {
int len = stream.readInt();
byte[] arr = new byte[len];
for (int i = 0; i < len; i++) {
for (int i = 0; i < len; i++) {
arr[i] = stream.readByte();
}
return new ByteArrayType(arr);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/ByteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Objects;
import java.io.IOException;

public class ByteType implements IType<Byte> {
private byte obj;
Expand Down Expand Up @@ -49,7 +48,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return obj;
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/CharType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Objects;
import java.io.IOException;

public class CharType implements IType<Character> {
private char obj;
Expand Down Expand Up @@ -49,7 +48,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return obj;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/types/DoubleArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class DoubleArrayType implements IType<double[]> {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/DoubleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Objects;
import java.io.IOException;

public class DoubleType implements IType<Double> {
private double obj;
Expand Down Expand Up @@ -49,7 +48,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return Double.hashCode(obj);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/types/FloatArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class FloatArrayType implements IType<float[]> {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/FloatType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Objects;
import java.io.IOException;

public class FloatType implements IType<Float> {
private float obj;
Expand Down Expand Up @@ -49,7 +48,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return Float.hashCode(obj);
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/ultreon/data/types/IType.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.ultreon.data.types;

import java.io.IOException;
import java.io.DataOutputStream;
import java.io.IOException;

public interface IType<T> {
T getValue();

void setValue(T obj);

int id();

void write(DataOutputStream stream) throws IOException;

boolean equals(Object other);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/types/IntArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class IntArrayType implements IType<int[]> {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/IntType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Objects;
import java.io.IOException;

public class IntType implements IType<Integer> {
private int obj;
Expand Down Expand Up @@ -49,7 +48,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return obj;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ultreon/data/types/ListType.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static ListType<?> read(DataInputStream stream) throws IOException {
for (int i = 0; i < len; i++) {
list.add(TypeRegistry.read(id, stream));
}

return new ListType<>(list, id);
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public int hashCode() {
@Override
@SuppressWarnings("unchecked")
public ListType<T> copy() {
return new ListType<T>(obj.stream().map(t -> (T) t.copy()).collect(Collectors.toList()));
return new ListType<>(obj.stream().map(t -> (T) t.copy()).collect(Collectors.toList()));
}

public int size() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/ultreon/data/types/LongArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;

public class LongArrayType implements IType<long[]> {
private long[] obj;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ultreon/data/types/LongType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.ultreon.data.Types;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Objects;
import java.io.IOException;

public class LongType implements IType<Long> {
private long obj;
Expand Down Expand Up @@ -49,7 +48,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(obj);
return (int) (obj ^ obj >>> 32);
}

@Override
Expand Down
Loading

0 comments on commit d0d3eb9

Please sign in to comment.