/** * Registers a new Item by its ID to the underlying container and to the * ordering. * * @param itemId * the ID of the Item to be created. * @return the added Item or <code>null</code> if the operation failed * @throws UnsupportedOperationException * if the addItem is not supported. */ @Override public Item addItem(Object itemId) throws UnsupportedOperationException { final Item item = container.addItem(itemId); if (!ordered && item != null) { addToOrderWrapper(itemId); } return item; }
/** * Registers the specified Item after the specified itemId in the wrapper's * internal ordering. The underlying container is not modified. Given item * id must be in the container, or must be null. * * @param id * the ID of the Item to be added to the ordering. * @param previousItemId * the Id of the previous item. */ private void addToOrderWrapper(Object id, Object previousItemId) { if (last == previousItemId || last == null) { addToOrderWrapper(id); } else { if (previousItemId == null) { next.put(id, first); prev.put(first, id); first = id; } else { prev.put(id, previousItemId); next.put(id, next.get(previousItemId)); prev.put(next.get(previousItemId), id); next.put(previousItemId, id); } } }
/** * Creates a new Item into the Container, assigns it an automatic ID, and * adds it to the ordering. * * @return the autogenerated ID of the new Item or <code>null</code> if the * operation failed * @throws UnsupportedOperationException * if the addItem is not supported. */ @Override public Object addItem() throws UnsupportedOperationException { final Object id = container.addItem(); if (!ordered && id != null) { addToOrderWrapper(id); } return id; }
@Override public Item addItemAfter(Object previousItemId, Object newItemId) throws UnsupportedOperationException { // If the previous item is not in the container, fail if (previousItemId != null && !containsId(previousItemId)) { return null; } // Adds the item to container final Item item = container.addItem(newItemId); // Puts the new item to its correct place if (!ordered && item != null) { addToOrderWrapper(newItemId, previousItemId); } return item; }
@Override public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException { // If the previous item is not in the container, fail if (previousItemId != null && !containsId(previousItemId)) { return null; } // Adds the item to container final Object id = container.addItem(); // Puts the new item to its correct place if (!ordered && id != null) { addToOrderWrapper(id, previousItemId); } return id; }