public void unEquipItem(Item item) { if (inventory.contains(item)) { if (item.equals(equipment.armor)) { equipment.armor = new Armor("", "", "", 0); GameGUI.addToConsole("You slip off your " + item); } else if (item.equals(equipment.weapon)) { equipment.weapon = new Weapon("", "", "", 0); GameGUI.addToConsole("You put your " + item + " away"); } else { GameGUI.addToConsole(item + " is not equipped"); } } else { GameGUI.addToConsole("You don't have that item"); } GameGUI.updateGUI(); }
/* * Exercise1_3_26 */ public void remove(Item item) { // Store all the indexes of the nodes equal to item and then delete using delete method LinkedList<Integer> idx = new LinkedList<Integer>(); int i = 1; for (Item x : this) { if (x.equals(item)) idx.prepend(i); i++; } for (int k : idx) delete(k); }
@Test public void testMergeMultipleEntityCopiesAllowed() { Item item1 = new Item(); item1.setName("item1"); Hoarder hoarder = new Hoarder(); hoarder.setName("joe"); EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin(); em.persist(item1); em.persist(hoarder); em.getTransaction().commit(); em.close(); // Get another representation of the same Item from a different EntityManager. em = getOrCreateEntityManager(); Item item1_1 = em.find(Item.class, item1.getId()); em.close(); // item1_1 and item1_2 are unmodified representations of the same persistent entity. assertFalse(item1 == item1_1); assertTrue(item1.equals(item1_1)); // Update hoarder (detached) to references both representations. hoarder.getItems().add(item1); hoarder.setFavoriteItem(item1_1); em = getOrCreateEntityManager(); em.getTransaction().begin(); // the merge should succeed because it does not have Category copies. // (CustomEntityCopyObserver does not allow Category copies; it does allow Item copies) hoarder = em.merge(hoarder); assertEquals(1, hoarder.getItems().size()); assertSame(hoarder.getFavoriteItem(), hoarder.getItems().iterator().next()); assertEquals(item1.getId(), hoarder.getFavoriteItem().getId()); assertEquals(item1.getCategory(), hoarder.getFavoriteItem().getCategory()); em.getTransaction().commit(); em.close(); em = getOrCreateEntityManager(); em.getTransaction().begin(); hoarder = em.find(Hoarder.class, hoarder.getId()); assertEquals(1, hoarder.getItems().size()); assertSame(hoarder.getFavoriteItem(), hoarder.getItems().iterator().next()); assertEquals(item1.getId(), hoarder.getFavoriteItem().getId()); assertEquals(item1.getCategory(), hoarder.getFavoriteItem().getCategory()); em.getTransaction().commit(); em.close(); cleanup(); }
public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final CategorizedItemComponent that = (CategorizedItemComponent) o; if (!category.equals(that.category)) return false; if (!dateAdded.equals(that.dateAdded)) return false; if (!item.equals(that.item)) return false; if (!username.equals(that.username)) return false; return true; }
@Test public void testCascadeFromDetachedToNonDirtyRepresentations() { Item item1 = new Item(); item1.setName("item1"); Hoarder hoarder = new Hoarder(); hoarder.setName("joe"); Session s = openSession(); Transaction tx = session.beginTransaction(); s.persist(item1); s.persist(hoarder); tx.commit(); s.close(); // Get another representation of the same Item from a different session. s = openSession(); Item item1_1 = (Item) s.get(Item.class, item1.getId()); s.close(); // item1_1 and item1_2 are unmodified representations of the same persistent entity. assertFalse(item1 == item1_1); assertTrue(item1.equals(item1_1)); // Update hoarder (detached) to references both representations. hoarder.getItems().add(item1); hoarder.setFavoriteItem(item1_1); s = openSession(); tx = s.beginTransaction(); hoarder = (Hoarder) s.merge(hoarder); assertEquals(1, hoarder.getItems().size()); assertSame(hoarder.getFavoriteItem(), hoarder.getItems().iterator().next()); assertEquals(item1.getId(), hoarder.getFavoriteItem().getId()); assertEquals(item1.getCategory(), hoarder.getFavoriteItem().getCategory()); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); hoarder = (Hoarder) s.merge(hoarder); assertEquals(1, hoarder.getItems().size()); assertSame(hoarder.getFavoriteItem(), hoarder.getItems().iterator().next()); assertEquals(item1.getId(), hoarder.getFavoriteItem().getId()); assertEquals(item1.getCategory(), hoarder.getFavoriteItem().getCategory()); tx.commit(); s.close(); cleanup(); }
public void addItem(Item item) { if (item == null) return; else if (item instanceof EmptyItem) { _allowEmpty = true; return; } else if (item instanceof ChoiceItem) { ChoiceItem choice = (ChoiceItem) item; if (choice._allowEmpty) _allowEmpty = true; for (int i = 0; i < choice._items.size(); i++) addItem(choice._items.get(i)); return; } for (int i = 0; i < _items.size(); i++) { Item subItem = _items.get(i); if (item.equals(subItem)) return; if (item instanceof InElementItem && subItem instanceof InElementItem) { InElementItem elt1 = (InElementItem) item; InElementItem elt2 = (InElementItem) subItem; if (elt1.getElementItem().equals(elt2.getElementItem())) { subItem = InElementItem.create( elt1.getElementItem(), create(elt1.getContinuationItem(), elt2.getContinuationItem())); _items.remove(i); addItem(subItem); return; } } if (item instanceof GroupItem && subItem instanceof GroupItem) { GroupItem group1 = (GroupItem) item; GroupItem group2 = (GroupItem) subItem; if (group1.getFirst().equals(group2.getFirst())) { subItem = GroupItem.create(group1.getFirst(), create(group1.getSecond(), group2.getSecond())); _items.remove(i); addItem(subItem); return; } } } _items.add(item); }
public static void main(String[] args) throws Exception { for (int i = 0; i < 20; i++) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(bout); Item item = new Item(); oout.writeObject(item); oout.close(); ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray())); Item itemcopy = (Item) oin.readObject(); if (!item.equals(itemcopy)) { throw new Error(); } } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Lot lot = (Lot) o; if (item != null ? !item.equals(lot.item) : lot.item != null) return false; if (owner != null ? !owner.equals(lot.owner) : lot.owner != null) return false; if (datePlaced != null ? !datePlaced.equals(lot.datePlaced) : lot.datePlaced != null) return false; if (dateEnd != null ? !dateEnd.equals(lot.dateEnd) : lot.dateEnd != null) return false; if (startPrice != null ? !startPrice.equals(lot.startPrice) : lot.startPrice != null) return false; if (buyer != null ? !buyer.equals(lot.buyer) : lot.buyer != null) return false; return !(currentPrice != null ? !currentPrice.equals(lot.currentPrice) : lot.currentPrice != null); }