Skip to content

Commit

Permalink
Refactoring. Replace switch with map
Browse files Browse the repository at this point in the history
  • Loading branch information
ullenius committed Nov 24, 2022
1 parent 02cf890 commit 31e3ff9
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/main/java/se/anosh/spctag/dao/Xid6Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.IntConsumer;

import static java.lang.Integer.toHexString;
Expand Down Expand Up @@ -76,20 +77,16 @@ private static boolean isAscii(int ch) {

final BiConsumer<Id, byte[]> oneByteData = (id, data) -> setData(id.tag, data[0]);

private void setData(Xid6Tag tag, byte b) {
switch (tag) {
case EMULATOR:
xid6.setEmulator(b);
break;
case OST_DISC:
xid6.setOstDisc(b);
break;
case LOOP_TIMES:
xid6.setLoop(b);
break;
default:
throw new IllegalArgumentException("no mapping found for: " + tag);
private void setData(final Xid6Tag tag, final byte b) {
final Map<Xid6Tag, Consumer<Byte>> mappings = Map.of(
Xid6Tag.EMULATOR, xid6::setEmulator,
Xid6Tag.OST_DISC, xid6::setOstDisc,
Xid6Tag.LOOP_TIMES, xid6::setLoop
);
if (!mappings.containsKey(tag)) {
throw new IllegalArgumentException("no mapping found for: " + tag);
}
mappings.get(tag).accept(b);
}

private final Map<Type, BiConsumer<Id, byte[]>> mappedBehaviourDataStoredInHeader = Map.of(
Expand Down

0 comments on commit 31e3ff9

Please sign in to comment.