Skip to content

Commit

Permalink
Added new helper type
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schnell committed Mar 31, 2024
1 parent e433a07 commit eb926a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.fuin.esc.api;

/**
* Maps a type to a class.
*
* @param type Type that is used as unique name for the class.
* @param clasz Class that is represented by the type.
*/
public record SerializedDataType2ClassMapping(SerializedDataType type, Class<?> clasz) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,26 @@ public SimpleSerializedDataTypeRegistry() {
/**
* Adds a new type/class combination to the registry.
*
* @param type
* Type of the data.
* @param clasz
* Class for the type.
* @param type Type of the data.
* @param clasz Class for the type.
*/
public void add(@NotNull final SerializedDataType type, final Class<?> clasz) {
Contract.requireArgNotNull("type", type);
Contract.requireArgNotNull("clasz", clasz);
map.put(type, clasz);
}

/**
* Adds a new type/class combination to the registry.
*
* @param mapping Type to class mapping.
*/
public void add(@NotNull final SerializedDataType2ClassMapping mapping) {
Contract.requireArgNotNull("mapping", mapping);
map.put(mapping.type(), mapping.clasz());
}


@Override
@NotNull
public Class<?> findClass(@NotNull final SerializedDataType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ public class SimpleSerializedDataTypeRegistryTest {
public void testFindClass() {

final SimpleSerializedDataTypeRegistry testee = new SimpleSerializedDataTypeRegistry();
final SerializedDataType type = new SerializedDataType("String");
testee.add(type, String.class);

assertThat(testee.findClass(type)).isEqualTo(String.class);
final SerializedDataType stringType = new SerializedDataType("String");
testee.add(stringType, String.class);

final SerializedDataType integerType = new SerializedDataType("Integer");
testee.add(new SerializedDataType2ClassMapping(integerType, Integer.class));

assertThat(testee.findClass(stringType)).isEqualTo(String.class);
assertThat(testee.findClass(integerType)).isEqualTo(Integer.class);

try {
testee.findClass(new SerializedDataType("NotExists"));
Expand Down

0 comments on commit eb926a4

Please sign in to comment.