Skip to content

Commit 358aee2

Browse files
committedDec 12, 2024
fixed context issues
1 parent 656392e commit 358aee2

9 files changed

+104
-41
lines changed
 

‎src/main/java/codes/laivy/serializable/context/ArrayContext.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codes.laivy.serializable.context;
22

33
import codes.laivy.serializable.Serializer;
4+
import codes.laivy.serializable.annotations.Concrete;
45
import codes.laivy.serializable.config.Config;
56
import org.jetbrains.annotations.NotNull;
67
import org.jetbrains.annotations.Nullable;
@@ -10,6 +11,7 @@
1011
import java.util.Objects;
1112

1213
// todo: writeAll
14+
@Concrete(type = ArrayContextImpl.class)
1315
public interface ArrayContext extends Context, Collection<Context> {
1416

1517
// Static initializers
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package codes.laivy.serializable.context;
22

33
import codes.laivy.serializable.Serializer;
4+
import codes.laivy.serializable.annotations.serializers.MethodSerialization;
5+
import codes.laivy.serializable.json.JsonSerializer;
46
import org.jetbrains.annotations.NotNull;
57
import org.jetbrains.annotations.Nullable;
68

@@ -10,17 +12,23 @@
1012
import java.util.LinkedList;
1113
import java.util.Objects;
1214

15+
@MethodSerialization
1316
final class ArrayContextImpl implements ArrayContext {
1417

1518
// Object
1619

1720
private final @NotNull Object lock = new Object();
1821
private final @NotNull Serializer serializer;
1922

20-
private final @NotNull LinkedList<Context> contexts = new LinkedList<>();
23+
private final @NotNull LinkedList<Context> list;
2124

2225
public ArrayContextImpl(@NotNull Serializer serializer) {
2326
this.serializer = serializer;
27+
this.list = new LinkedList<>();
28+
}
29+
public ArrayContextImpl(@NotNull LinkedList<Context> list) {
30+
this.serializer = JsonSerializer.getInstance();
31+
this.list = list;
2432
}
2533

2634
// Modules
@@ -35,11 +43,11 @@ public ArrayContextImpl(@NotNull Serializer serializer) {
3543
@Override
3644
public @NotNull Context readContext() throws EOFException {
3745
synchronized (lock) {
38-
if (contexts.isEmpty()) {
46+
if (list.isEmpty()) {
3947
throw new EOFException();
4048
}
4149

42-
return contexts.poll();
50+
return list.poll();
4351
}
4452
}
4553
@Override
@@ -51,61 +59,70 @@ public void write(@NotNull Context context) {
5159

5260
@Override
5361
public int size() {
54-
return contexts.size();
62+
return list.size();
5563
}
5664
@Override
5765
public boolean isEmpty() {
58-
return contexts.isEmpty();
66+
return list.isEmpty();
5967
}
6068

6169
@Override
6270
public boolean contains(Object o) {
63-
return contexts.contains(o);
71+
return list.contains(o);
6472
}
6573

6674
@Override
6775
public @NotNull Iterator<Context> iterator() {
68-
return contexts.iterator();
76+
return list.iterator();
6977
}
7078

7179
@Override
7280
public @Nullable Object @NotNull [] toArray() {
73-
return contexts.toArray();
81+
return list.toArray();
7482
}
7583
@Override
7684
public <T> @NotNull T @NotNull [] toArray(@NotNull T @NotNull [] a) {
77-
return contexts.toArray(a);
85+
return list.toArray(a);
7886
}
7987

8088
@Override
8189
public boolean add(@NotNull Context context) {
82-
return contexts.add(context);
90+
return list.add(context);
8391
}
8492
@Override
8593
public boolean remove(Object o) {
86-
return contexts.remove(o);
94+
return list.remove(o);
8795
}
8896

8997
@Override
9098
public boolean containsAll(@NotNull Collection<?> c) {
91-
return contexts.containsAll(c);
99+
return list.containsAll(c);
92100
}
93101
@Override
94102
public boolean addAll(@NotNull Collection<? extends Context> c) {
95-
return contexts.addAll(c);
103+
return list.addAll(c);
96104
}
97105
@Override
98106
public boolean removeAll(@NotNull Collection<?> c) {
99-
return contexts.removeAll(c);
107+
return list.removeAll(c);
100108
}
101109
@Override
102110
public boolean retainAll(@NotNull Collection<?> c) {
103-
return contexts.retainAll(c);
111+
return list.retainAll(c);
104112
}
105113

106114
@Override
107115
public void clear() {
108-
contexts.clear();
116+
list.clear();
117+
}
118+
119+
// Serializers
120+
121+
private static @NotNull LinkedList<Context> serialize(@NotNull ArrayContextImpl context) {
122+
return context.list;
123+
}
124+
private static @NotNull ArrayContextImpl deserialize(@NotNull LinkedList<Context> list) {
125+
return new ArrayContextImpl(list);
109126
}
110127

111128
// Implementations
@@ -115,19 +132,16 @@ public boolean equals(@Nullable Object object) {
115132
if (this == object) return true;
116133
if (!(object instanceof ArrayContextImpl)) return false;
117134
@NotNull ArrayContextImpl contexts1 = (ArrayContextImpl) object;
118-
return Objects.equals(getSerializer(), contexts1.getSerializer()) && Objects.equals(contexts, contexts1.contexts);
135+
return Objects.equals(getSerializer(), contexts1.getSerializer()) && Objects.equals(list, contexts1.list);
119136
}
120137
@Override
121138
public int hashCode() {
122-
return Objects.hash(getSerializer(), contexts);
139+
return Objects.hash(getSerializer(), list);
123140
}
124141

125142
@Override
126143
public @NotNull String toString() {
127-
return "ArrayContextImpl{" +
128-
"serializer=" + serializer +
129-
", contexts=" + contexts +
130-
'}';
144+
return list.toString();
131145
}
132146

133147
}

‎src/main/java/codes/laivy/serializable/context/Context.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package codes.laivy.serializable.context;
22

3+
import codes.laivy.serializable.annotations.Concrete;
34
import org.jetbrains.annotations.NotNull;
45

6+
@Concrete(type = ArrayContextImpl.class)
7+
@Concrete(type = MapContextImpl.class)
8+
@Concrete(type = NullContextImpl.class)
9+
@Concrete(type = PrimitiveContextImpl.class)
510
public interface Context {
611

712
// Object
@@ -10,7 +15,7 @@ public interface Context {
1015
if (this instanceof ArrayContext) {
1116
return (ArrayContext) this;
1217
} else {
13-
throw new IllegalStateException("this instance isn't an array context");
18+
throw new IllegalStateException("this instance isn't an array context: " + this.getClass().getName());
1419
}
1520
}
1621
default boolean isArray() {
@@ -21,7 +26,7 @@ default boolean isArray() {
2126
if (this instanceof PrimitiveContext) {
2227
return (PrimitiveContext) this;
2328
} else {
24-
throw new IllegalStateException("this instance isn't a primitive context");
29+
throw new IllegalStateException("this instance isn't a primitive context: " + this.getClass().getName());
2530
}
2631
}
2732
default boolean isPrimitive() {
@@ -32,7 +37,7 @@ default boolean isPrimitive() {
3237
if (this instanceof MapContext) {
3338
return (MapContext) this;
3439
} else {
35-
throw new IllegalStateException("this instance isn't a map context");
40+
throw new IllegalStateException("this instance isn't a map context: " + this.getClass().getName());
3641
}
3742
}
3843
default boolean isMap() {
@@ -43,7 +48,7 @@ default boolean isMap() {
4348
if (this instanceof NullContext) {
4449
return (NullContext) this;
4550
} else {
46-
throw new IllegalStateException("this instance isn't a null context");
51+
throw new IllegalStateException("this instance isn't a null context: " + this.getClass().getName());
4752
}
4853
}
4954
default boolean isNull() {

‎src/main/java/codes/laivy/serializable/context/MapContext.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codes.laivy.serializable.context;
22

33
import codes.laivy.serializable.Serializer;
4+
import codes.laivy.serializable.annotations.Concrete;
45
import codes.laivy.serializable.config.Config;
56
import org.jetbrains.annotations.NotNull;
67
import org.jetbrains.annotations.Nullable;
@@ -9,6 +10,7 @@
910
import java.util.Map.Entry;
1011
import java.util.Set;
1112

13+
@Concrete(type = MapContextImpl.class)
1214
public interface MapContext extends Context {
1315

1416
// Static initializers
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
package codes.laivy.serializable.context;
22

33
import codes.laivy.serializable.Serializer;
4+
import codes.laivy.serializable.annotations.serializers.MethodSerialization;
5+
import codes.laivy.serializable.json.JsonSerializer;
46
import org.jetbrains.annotations.NotNull;
57
import org.jetbrains.annotations.Nullable;
68

79
import java.util.*;
810

11+
@MethodSerialization
912
final class MapContextImpl implements MapContext {
1013

1114
// Object
1215

1316
private final @NotNull Object lock = new Object();
1417
private final @NotNull Serializer serializer;
1518

16-
private final @NotNull Map<String, Context> contextMap = new LinkedHashMap<>();
19+
private final @NotNull Map<String, Context> map;
1720

1821
public MapContextImpl(@NotNull Serializer serializer) {
1922
this.serializer = serializer;
23+
this.map = new LinkedHashMap<>();
24+
}
25+
public MapContextImpl(@NotNull Map<String, Context> map) {
26+
this.serializer = JsonSerializer.getInstance();
27+
this.map = map;
2028
}
2129

2230
// Modules
@@ -29,41 +37,50 @@ public MapContextImpl(@NotNull Serializer serializer) {
2937
@Override
3038
public void setContext(@NotNull String name, @NotNull Context context) {
3139
synchronized (lock) {
32-
contextMap.put(name, context);
40+
map.put(name, context);
3341
}
3442
}
3543
@Override
3644
public @NotNull Context getContext(@NotNull String name) {
37-
if (contextMap.containsKey(name)) {
38-
return contextMap.get(name);
45+
if (map.containsKey(name)) {
46+
return map.get(name);
3947
} else {
4048
throw new IllegalArgumentException("there's no context with name '" + name + "'");
4149
}
4250
}
4351

4452
@Override
4553
public @Nullable Context remove(@NotNull String name) {
46-
return contextMap.remove(name);
54+
return map.remove(name);
4755
}
4856

4957
@Override
5058
public boolean contains(@NotNull String name) {
51-
return contextMap.containsKey(name);
59+
return map.containsKey(name);
5260
}
5361

5462
@Override
5563
public @NotNull Set<@NotNull String> keySet() {
56-
return contextMap.keySet();
64+
return map.keySet();
5765
}
5866

5967
@Override
6068
public @NotNull Set<Map.@NotNull Entry<@NotNull String, @NotNull Context>> entrySet() {
61-
return contextMap.entrySet();
69+
return map.entrySet();
6270
}
6371

6472
@Override
6573
public @NotNull Collection<@NotNull Context> values() {
66-
return contextMap.values();
74+
return map.values();
75+
}
76+
77+
// Serializers
78+
79+
private static @NotNull Map<String, Context> serialize(@NotNull MapContextImpl context) {
80+
return context.map;
81+
}
82+
private static @NotNull MapContextImpl deserialize(@NotNull Map<String, Context> map) {
83+
return new MapContextImpl(map);
6784
}
6885

6986
// Implementations
@@ -73,19 +90,16 @@ public boolean equals(@Nullable Object object) {
7390
if (this == object) return true;
7491
if (!(object instanceof MapContextImpl)) return false;
7592
@NotNull MapContextImpl that = (MapContextImpl) object;
76-
return Objects.equals(getSerializer(), that.getSerializer()) && Objects.equals(contextMap, that.contextMap);
93+
return Objects.equals(getSerializer(), that.getSerializer()) && Objects.equals(map, that.map);
7794
}
7895
@Override
7996
public int hashCode() {
80-
return Objects.hash(getSerializer(), contextMap);
97+
return Objects.hash(getSerializer(), map);
8198
}
8299

83100
@Override
84101
public @NotNull String toString() {
85-
return "MapContextImpl{" +
86-
"serializer=" + serializer +
87-
", contextMap=" + contextMap +
88-
'}';
102+
return map.toString();
89103
}
90104

91105
}

‎src/main/java/codes/laivy/serializable/context/NullContext.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package codes.laivy.serializable.context;
22

3+
import codes.laivy.serializable.annotations.Concrete;
34
import org.jetbrains.annotations.NotNull;
45

6+
@Concrete(type = NullContextImpl.class)
57
public interface NullContext extends Context {
68

79
static @NotNull NullContext create() {

‎src/main/java/codes/laivy/serializable/context/NullContextImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package codes.laivy.serializable.context;
22

3+
import codes.laivy.serializable.annotations.serializers.MethodSerialization;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56

7+
@MethodSerialization
68
final class NullContextImpl implements NullContext {
79

810
// Object
911

1012
public NullContextImpl() {
1113
}
1214

15+
// Serializers
16+
17+
private static @Nullable Object serialize(@NotNull NullContextImpl context) {
18+
return null;
19+
}
20+
private static @NotNull NullContextImpl deserialize(@Nullable Object object) {
21+
return new NullContextImpl();
22+
}
23+
1324
// Implementations
1425

1526
@Override

‎src/main/java/codes/laivy/serializable/context/PrimitiveContext.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package codes.laivy.serializable.context;
22

3+
import codes.laivy.serializable.annotations.Concrete;
34
import org.jetbrains.annotations.NotNull;
45

6+
@Concrete(type = PrimitiveContextImpl.class)
57
public interface PrimitiveContext extends Context {
68

79
// Static initializers

‎src/main/java/codes/laivy/serializable/context/PrimitiveContextImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package codes.laivy.serializable.context;
22

3+
import codes.laivy.serializable.annotations.serializers.MethodSerialization;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56

67
import java.util.Objects;
78

9+
@MethodSerialization
810
final class PrimitiveContextImpl implements PrimitiveContext {
911

1012
// Object
@@ -59,6 +61,15 @@ public void setAsString(@NotNull String string) {
5961
return object;
6062
}
6163

64+
// Serializers
65+
66+
private static @NotNull Object serialize(@NotNull PrimitiveContextImpl context) {
67+
return context.getObject();
68+
}
69+
private static @NotNull PrimitiveContextImpl deserialize(@NotNull Object object) {
70+
return new PrimitiveContextImpl(object);
71+
}
72+
6273
// Implementations
6374

6475
@Override

0 commit comments

Comments
 (0)
Please sign in to comment.