Browse Source

Switch over to using optional-fetch for Items, instead of relying on null checks

Sam Jaffe 8 năm trước cách đây
mục cha
commit
a0a8535d83

+ 5 - 5
src/main/lombok/org/leumasjaffe/charsheet/controller/EquipItemController.java

@@ -42,7 +42,7 @@ public class EquipItemController {
 
 		private boolean selectToReplaceAllOf(final EquipmentSlot base,
 				final EquipmentSlot slot1, final EquipmentSlot slot2) {
-			if (inv.get(slot1).getSlot() == base) {
+			if (inv.get(slot1).get().getSlot() == base) {
 				return selectToReplace(slot1);
 			} else if (JOptionPane.showConfirmDialog(null, createDialogTwoSlots(REPLACE_BOTH, slot1, slot2), 
 					QUERY_REPLACE, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
@@ -78,19 +78,19 @@ public class EquipItemController {
 		}
 
 		private void doUnequip(final EquipmentSlot slot) {
-			inv.get(slot).adjustCounEquipped(-1);
+			inv.get(slot).get().adjustCounEquipped(-1);
 			inv.unequip(slot);
 		}		
 
 		private EquipItemDialog createDialogOneSlot(final EquipmentSlot slot) {
-			return new EquipItemDialog(REPLACE_ONE, slot, inv.get(slot));
+			return new EquipItemDialog(REPLACE_ONE, slot, inv.get(slot).get());
 		}
 		
 		private EquipItemDialog createDialogTwoSlots(final String message, 
 				final EquipmentSlot slot1, final EquipmentSlot slot2) {
 			return new EquipItemDialog(message,
-					new EquipItemDialog.Tuple(slot1, inv.get(slot1)),
-					new EquipItemDialog.Tuple(slot2, inv.get(slot2)));
+					new EquipItemDialog.Tuple(slot1, inv.get(slot1).get()),
+					new EquipItemDialog.Tuple(slot2, inv.get(slot2).get()));
 		}
 	}
 }

+ 3 - 2
src/main/lombok/org/leumasjaffe/charsheet/model/inventory/DDEquipment.java

@@ -3,6 +3,7 @@ package org.leumasjaffe.charsheet.model.inventory;
 import java.util.Collections;
 import java.util.EnumMap;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.function.BiConsumer;
 
@@ -98,8 +99,8 @@ public class DDEquipment extends Observable {
 		return Collections.unmodifiableSet(equipment.keySet());
 	}
 	
-	public DDItem get(EquipmentSlot slot) {
-		return equipment.get(slot);
+	public Optional<DDItem> get(EquipmentSlot slot) {
+		return Optional.ofNullable(equipment.get(slot));
 	}
 	
 	public boolean containsKey(EquipmentSlot slot) {

+ 19 - 20
src/main/lombok/org/leumasjaffe/charsheet/view/inventory/EquipmentPanel.java

@@ -86,56 +86,55 @@ public class EquipmentPanel extends JPanel {
 	private void updateModel(final DDEquipment equip) {
 		final Set<EquipmentSlot> manual = EnumSet.noneOf(EquipmentSlot.class);
 		equipment.removeAll();	
-		final DDItem armor = equip.get(BODY);
-		if (armor != null && armor.getArmor().isPresent()) {
+		final Optional<DDItem> armor = equip.get(BODY);
+		armor.flatMap(DDItem::getArmor).ifPresent(a -> {
 			manual.add(BODY);
-			createWithRightClickMenu(ArmorPanel::new, equip, BODY, BODY);
-		}
+			createWithRightClickMenu(ArmorPanel::new, equip, armor.get(), BODY);
+		});
 		
-		final DDItem main = equip.get(MAIN_HAND);
-		final DDItem off = equip.get(OFF_HAND);
+		final Optional<DDItem> main = equip.get(MAIN_HAND);
+		final Optional<DDItem> off = equip.get(OFF_HAND);
 	
 		final Optional<Function<DDItem, JPanel>> makeMain = getEquipmentRightClickPanelFactory(main);
 		final Optional<Function<DDItem, JPanel>> makeOff = getEquipmentRightClickPanelFactory(off);
 		
-		if (main == off && !couldDualWieldThis(main)) {
+		if (main.equals(off) && !couldDualWieldThis(main)) {
 			makeMain.ifPresent(f -> {
 				manual.add(MAIN_HAND);
 				manual.add(OFF_HAND);
-				createWithRightClickMenu(f, equip, TWO_HANDS, MAIN_HAND);
+				createWithRightClickMenu(f, equip, main.get(), TWO_HANDS);
 			});
 		} else {
 			makeMain.ifPresent(f -> {
 				manual.add(MAIN_HAND);
-				createWithRightClickMenu(f, equip, MAIN_HAND, MAIN_HAND);
+				createWithRightClickMenu(f, equip, main.get(), MAIN_HAND);
 			});
 			makeOff.ifPresent(f -> {
 				manual.add(OFF_HAND);
-				createWithRightClickMenu(f, equip, OFF_HAND, OFF_HAND);
+				createWithRightClickMenu(f, equip, off.get(), OFF_HAND);
 			});
 		}
 		
-		equip.keySet().stream().filter( slot -> equip.get(slot) != null && !manual.contains(slot) )
+		equip.keySet().stream().filter( slot -> !manual.contains(slot) )
 		.forEach( slot -> {
-			createWithRightClickMenu(null, equip, slot, slot);
+			createWithRightClickMenu(null, equip, equip.get(slot).get(), slot);
 		});
 		repaint();
 	}
 
-	private Optional<Function<DDItem, JPanel>> getEquipmentRightClickPanelFactory(final DDItem item) {
-		if (item == null) { return Optional.empty(); }
-		else if (item.getWeapon().isPresent()) { return Optional.of(WeaponPanel::new); }
-		else if (item.getArmor().isPresent()) { return Optional.of(ShieldPanel::new); }
+	private Optional<Function<DDItem, JPanel>> getEquipmentRightClickPanelFactory(final Optional<DDItem> item) {
+		if (!item.isPresent()) { return Optional.empty(); }
+		else if (item.get().getWeapon().isPresent()) { return Optional.of(WeaponPanel::new); }
+		else if (item.get().getArmor().isPresent()) { return Optional.of(ShieldPanel::new); }
 		else { return Optional.empty(); }
 	}
 
-	private boolean couldDualWieldThis(final DDItem item) {
-		return item != null && item.getSlot() == EquipmentSlot.ONE_HAND && item.getCount().value() > 1;
+	private boolean couldDualWieldThis(final Optional<DDItem> item) {
+		return item.map(i -> i.getSlot() == EquipmentSlot.ONE_HAND && i.getCount().value() > 1).orElse(false);
 	}
 	
 	private void createWithRightClickMenu(final Function<DDItem, JPanel> make,
-			final DDEquipment equip, final EquipmentSlot slot, EquipmentSlot get) {
-		final DDItem item = equip.get(get);
+			final DDEquipment equip, final DDItem item, final EquipmentSlot slot) {
 		final JPanel panel = make.apply(item);
 		equipment.add(panel);
 		panel.addMouseListener(new PopClickListener(

+ 14 - 35
src/main/lombok/org/leumasjaffe/charsheet/view/summary/ArmorLine.java

@@ -359,60 +359,39 @@ public class ArmorLine extends JPanel {
 		
 		armorTotalObserver = new IndirectObservableListener<>(total, (c, v) -> {
 			final DDInventory inv = v.getInventory();
-			IntValue iarmor = new IntValue(0);
-			IntValue ishield = new IntValue(0);
+			IntValue iarmor = new IntValue(0), ishield = new IntValue(0);
 			IntValue dex = new IntValue(v.getAbilities().getDex().modifier());
-			int isize = v.getSize().value().modifier;
-			int inatural = 0;
-			int ideflect = 0;
-			int imisc = 0;
+			int isize = v.getSize().value().modifier, inatural = 0, ideflect = 0, imisc = 0;
 			
-			{
-				final DDItem body = inv.getEquipment().get(EquipmentSlot.BODY);
-				if ( body != null ) {
-					body.getArmor().ifPresent( a -> {
-						iarmor.value(a.getActualAcBonus());
-						dex.value(Math.min(dex.value(), a.getMaxDex()));
-					});
-				}
-			}
-			{
-				final DDItem offHand = inv.getEquipment().get(EquipmentSlot.OFF_HAND);
-				if ( offHand != null ) {
-					offHand.getArmor().ifPresent(a -> ishield.value(a.getActualAcBonus()));
-				}
-			}
+			inv.getEquipment().get(EquipmentSlot.BODY).flatMap(DDItem::getArmor).ifPresent( a -> {
+				iarmor.value(a.getActualAcBonus());
+				dex.value(Math.min(dex.value(), a.getMaxDex()));
+			});
+			inv.getEquipment().get(EquipmentSlot.OFF_HAND).flatMap(DDItem::getArmor).ifPresent(
+					a -> ishield.value(a.getActualAcBonus()));
 			c.setText(StringHelper.toString(10 + iarmor.value() + ishield.value() + 
 					dex.value() + isize + inatural + ideflect + imisc));
 		});
 		
 		armorArmorObserver = new ObservableListener<>(armor, (c, v) -> {
 			IntValue iarmor = new IntValue(0);
-			final DDItem body = v.getEquipment().get(EquipmentSlot.BODY);
-			if ( body != null ) {
-				body.getArmor().ifPresent(a -> iarmor.value(a.getActualAcBonus()));
-			}
+			v.getEquipment().get(EquipmentSlot.BODY).flatMap(DDItem::getArmor).ifPresent(
+					a -> iarmor.value(a.getActualAcBonus()));
 			c.setText(StringHelper.toString(iarmor));
 		});
 		
 		armorShieldObserver = new ObservableListener<>(shield, (c, v) -> {
 			IntValue iarmor = new IntValue(0);
-			final DDItem offHand = v.getEquipment().get(EquipmentSlot.OFF_HAND);
-			if ( offHand != null ) {
-				offHand.getArmor().ifPresent(a -> iarmor.value(a.getActualAcBonus()));
-			}
+			v.getEquipment().get(EquipmentSlot.OFF_HAND).flatMap(DDItem::getArmor).ifPresent(
+					a -> iarmor.value(a.getActualAcBonus()));
 			c.setText(StringHelper.toString(iarmor));
 		});
 		
 		armorDexObserver = new IndirectObservableListener<>(dexterity, (c, v) -> {
 			final DDInventory inv = v.getInventory();
 			IntValue dex = new IntValue(v.getAbilities().getDex().modifier());
-			{
-				final DDItem body = inv.getEquipment().get(EquipmentSlot.BODY);
-				if ( body != null ) {
-					body.getArmor().ifPresent(a -> dex.value(Math.min(dex.value(), a.getMaxDex())));
-				}
-			}
+			inv.getEquipment().get(EquipmentSlot.BODY).flatMap(DDItem::getArmor).ifPresent(
+					a -> dex.value(Math.min(dex.value(), a.getMaxDex())));
 			c.setText(StringHelper.toString(dex));
 		});
 	}