Changeset 7833


Ignore:
Timestamp:
Jun 26, 2020, 11:39:44 AM (19 months ago)
Author:
Nicklas Nordborg
Message:

References #2216: Improve API for adding and removing items to an item list

Added methods to ItemList. The new methods are overloaded variants of the existing add() and removeItems() methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/ItemList.java

    r7610 r7833  
    557557 
    558558  /**
     559    Add all items that are returned by the query to this list. The query is
     560    executed with the {@link ItemQuery#idList(DbControl)} method so this
     561    should be a lot more efficient than {@link #add(Iterator)} when adding
     562    lots of items to a list.
     563    @param query An query that returns all items to be added
     564    @return The numer of added items
     565    @throws PermissionDeniedException If the logged in user doesn't have
     566      write permission
     567    @throws InvalidDataException If the query returns an item
     568      of a different type than {@link #getMemberType()}
     569    @since 3.16.3
     570  */
     571  public int add(ItemQuery<? extends Listable> query)
     572  {
     573    checkPermission(Permission.WRITE);
     574    checkManualMembersAllowed();
     575    if (query == null) return 0;
     576    checkMemberType(query.getItemType());
     577   
     578    return add(query.idList(getDbControl()));
     579  }
     580 
     581  /**
     582    Add all items in the collection to this list.
     583   
     584    @param items A collection with ID for the items to add
     585    @return The numer of added items
     586    @throws PermissionDeniedException If the logged in user doesn't have
     587      write permission
     588    @since 3.16.3
     589  */
     590  public int add(Collection<? extends Integer> items)
     591  {
     592    checkPermission(Permission.WRITE);
     593    checkManualMembersAllowed();
     594    if (items == null || items.isEmpty()) return 0;
     595   
     596    Set<Integer> members = getData().getMembers();
     597    int sizeBefore = members.size();
     598    members.addAll(items);
     599    int sizeAfter = members.size();
     600   
     601    getData().setSize(sizeAfter);
     602    return sizeAfter-sizeBefore;
     603  }
     604 
     605  /**
    559606    Remove an item from the list.
    560607    @param item The item to remove.
     
    619666  }
    620667
    621  
     668  /**
     669    Remove all items that are returned by the query from this list. The query is
     670    executed with the {@link ItemQuery#idList(DbControl)} method so this
     671    should be a lot more efficient than {@link #removeItems(Iterator)} when removing
     672    lots of items from a list.
     673    @param query An query that returns all items to be removed
     674    @return The numer of removed items
     675    @throws PermissionDeniedException If the logged in user doesn't have
     676      write permission
     677    @throws InvalidDataException If the query returns an item
     678      of a different type than {@link #getMemberType()}
     679    @since 3.16.3
     680  */
     681  public int removeItems(ItemQuery<? extends Listable> query)
     682  {
     683    checkPermission(Permission.WRITE);
     684    checkManualMembersAllowed();
     685    if (query == null) return 0;
     686    checkMemberType(query.getItemType());
     687   
     688    return removeItems(query.idList(getDbControl()));
     689  }
     690 
     691  /**
     692    Remove all items in the collection from this list.
     693   
     694    @param items A collection with ID for the items to remove
     695    @return The numer of removed items
     696    @throws PermissionDeniedException If the logged in user doesn't have
     697      write permission
     698    @since 3.16.3
     699  */
     700  public int removeItems(Collection<? extends Integer> items)
     701  {
     702    checkPermission(Permission.WRITE);
     703    checkManualMembersAllowed();
     704    if (items == null || items.isEmpty()) return 0;
     705   
     706    Set<Integer> members = getData().getMembers();
     707    int sizeBefore = members.size();
     708    members.removeAll(items);
     709    int sizeAfter = members.size();
     710   
     711    getData().setSize(sizeAfter);
     712    return sizeBefore - sizeAfter;
     713  }
     714
    622715  /**
    623716    Remove an item from the list.
Note: See TracChangeset for help on using the changeset viewer.