org.apache.commons.collections4.Bag Java Examples

The following examples show how to use org.apache.commons.collections4.Bag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BagUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenBag_whenBagAddAPILikeCollectionAPI_thenFalse() {
    Collection<Integer> collection = new ArrayList<>();

    // Collection contract defines that add() should return true
    assertThat(collection.add(9), is(true));

    // Even when element is already in the collection
    collection.add(1);
    assertThat(collection.add(1), is(true));

    Bag<Integer> bag = new HashBag<>();

    // Bag returns true on adding a new element
    assertThat(bag.add(9), is(true));

    bag.add(1);
    // But breaks the contract with false when it has to increment the count
    assertThat(bag.add(1), is(not(true)));
}
 
Example #2
Source File: ApacheHashBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Разберем текст на слова
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Создаем Multiset
    Bag bag = new HashBag(Arrays.asList(INPUT_TEXT.split(" ")));

    // Выводим кол-вом вхождений слов
    System.out.println(bag); // напечатает [1:Hi,2:Hello,2:World!,1:All!] - в произвольном порядке
    // Выводим все уникальные слова
    System.out.println(bag.uniqueSet());    // напечатает [Hi, Hello, World!, All!] - в произвольном порядке

    // Выводим количество по каждому слову
    System.out.println("Hello = " + bag.getCount("Hello"));    // напечатает 2
    System.out.println("World = " + bag.getCount("World!"));    // напечатает 2
    System.out.println("All = " + bag.getCount("All!"));    // напечатает 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // напечатает 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // напечатает 0

    // Выводим общее количества всех слов в тексте
    System.out.println(bag.size());    //напечатает 6

    // Выводим общее количество всех уникальных слов
    System.out.println(bag.uniqueSet().size());    //напечатает 4
}
 
Example #3
Source File: ApacheTreeBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Разберем текст на слова
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Создаем Multiset
    Bag bag = new TreeBag(Arrays.asList(INPUT_TEXT.split(" ")));

    // Выводим кол-вом вхождений слов
    System.out.println(bag); // напечатает [1:All!,2:Hello,1:Hi,2:World!]- в алфавитном порядке
    // Выводим все уникальные слова
    System.out.println(bag.uniqueSet());    // напечатает [All!, Hello, Hi, World!]- в алфавитном порядке

    // Выводим количество по каждому слову
    System.out.println("Hello = " + bag.getCount("Hello"));    // напечатает 2
    System.out.println("World = " + bag.getCount("World!"));    // напечатает 2
    System.out.println("All = " + bag.getCount("All!"));    // напечатает 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // напечатает 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // напечатает 0

    // Выводим общее количества всех слов в тексте
    System.out.println(bag.size());    //напечатает 6

    // Выводим общее количество всех уникальных слов
    System.out.println(bag.uniqueSet().size());    //напечатает 4
}
 
Example #4
Source File: ApacheSynchronizedBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Разберем текст на слова
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Создаем Multiset
    Bag bag = SynchronizedBag.synchronizedBag(new HashBag(Arrays.asList(INPUT_TEXT.split(" "))));

    // Выводим кол-вом вхождений слов
    System.out.println(bag); // напечатает [1:Hi,2:Hello,2:World!,1:All!] - в произвольном порядке
    // Выводим все уникальные слова
    System.out.println(bag.uniqueSet());    // напечатает [Hi, Hello, World!, All!] - в произвольном порядке

    // Выводим количество по каждому слову
    System.out.println("Hello = " + bag.getCount("Hello"));    // напечатает 2
    System.out.println("World = " + bag.getCount("World!"));    // напечатает 2
    System.out.println("All = " + bag.getCount("All!"));    // напечатает 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // напечатает 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // напечатает 0

    // Выводим общее количества всех слов в тексте
    System.out.println(bag.size());    //напечатает 6

    // Выводим общее количество всех уникальных слов
    System.out.println(bag.uniqueSet().size());    //напечатает 4
}
 
Example #5
Source File: ApacheSynchronizedSortedBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Разберем текст на слова
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Создаем Multiset
    Bag bag = SynchronizedSortedBag.synchronizedBag(new TreeBag(Arrays.asList(INPUT_TEXT.split(" "))));

    // Выводим кол-вом вхождений слов
    System.out.println(bag); // напечатает [1:All!,2:Hello,1:Hi,2:World!]- в алфавитном порядке
    // Выводим все уникальные слова
    System.out.println(bag.uniqueSet());    // напечатает [All!, Hello, Hi, World!]- в алфавитном порядке


    // Выводим количество по каждому слову
    System.out.println("Hello = " + bag.getCount("Hello"));    // напечатает 2
    System.out.println("World = " + bag.getCount("World!"));    // напечатает 2
    System.out.println("All = " + bag.getCount("All!"));    // напечатает 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // напечатает 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // напечатает 0

    // Выводим общее количества всех слов в тексте
    System.out.println(bag.size());    //напечатает 6

    // Выводим общее количество всех уникальных слов
    System.out.println(bag.uniqueSet().size());    //напечатает 4
}
 
Example #6
Source File: ApacheHashBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Bag bag = new HashBag(Arrays.asList(INPUT_TEXT.split(" ")));

    // Print count words
    System.out.println(bag); // print [1:Hi,2:Hello,2:World!,1:All!] - in random orders
    // Print all unique words
    System.out.println(bag.uniqueSet());    // print [Hi, Hello, World!, All!] - in random orders

    // Print count occurrences of words
    System.out.println("Hello = " + bag.getCount("Hello"));    // print 2
    System.out.println("World = " + bag.getCount("World!"));    // print 2
    System.out.println("All = " + bag.getCount("All!"));    // print 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // print 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // print 0

    // Print count all words
    System.out.println(bag.size());    //print 6

    // Print count unique words
    System.out.println(bag.uniqueSet().size());    //print 4
}
 
Example #7
Source File: ApacheTreeBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Bag bag = new TreeBag(Arrays.asList(INPUT_TEXT.split(" ")));

    // Print count words
    System.out.println(bag); // print [1:All!,2:Hello,1:Hi,2:World!]- in natural (alphabet) order
    // Print all unique words
    System.out.println(bag.uniqueSet());    // print [All!, Hello, Hi, World!]- in natural (alphabet) order

    // Print count occurrences of words
    System.out.println("Hello = " + bag.getCount("Hello"));    // print 2
    System.out.println("World = " + bag.getCount("World!"));    // print 2
    System.out.println("All = " + bag.getCount("All!"));    // print 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // print 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // print 0

    // Print count all words
    System.out.println(bag.size());    //print 6

    // Print count unique words
    System.out.println(bag.uniqueSet().size());    //print 4
}
 
Example #8
Source File: ApacheSynchronizedBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Bag bag = SynchronizedBag.synchronizedBag(new HashBag(Arrays.asList(INPUT_TEXT.split(" "))));

    // Print count words
    System.out.println(bag); // print [1:Hi,2:Hello,2:World!,1:All!] - in random orders
    // Print all unique words
    System.out.println(bag.uniqueSet());    // print [Hi, Hello, World!, All!] - in random orders

    // Print count occurrences of words
    System.out.println("Hello = " + bag.getCount("Hello"));    // print 2
    System.out.println("World = " + bag.getCount("World!"));    // print 2
    System.out.println("All = " + bag.getCount("All!"));    // print 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // print 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // print 0

    // Print count all words
    System.out.println(bag.size());    //print 6

    // Print count unique words
    System.out.println(bag.uniqueSet().size());    //print 4
}
 
Example #9
Source File: ApacheSynchronizedSortedBagTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Bag bag = SynchronizedSortedBag.synchronizedBag(new TreeBag(Arrays.asList(INPUT_TEXT.split(" "))));

    // Print count words
    System.out.println(bag); // print [1:All!,2:Hello,1:Hi,2:World!]- in natural (alphabet) order
    // Print all unique words
    System.out.println(bag.uniqueSet());    // print [All!, Hello, Hi, World!]- in natural (alphabet) order


    // Print count occurrences of words
    System.out.println("Hello = " + bag.getCount("Hello"));    // print 2
    System.out.println("World = " + bag.getCount("World!"));    // print 2
    System.out.println("All = " + bag.getCount("All!"));    // print 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // print 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // print 0

    // Print count all words
    System.out.println(bag.size());    //print 6

    // Print count unique words
    System.out.println(bag.uniqueSet().size());    //print 4
}
 
Example #10
Source File: BagUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMultipleCopies_whenRemove_allAreRemoved() {
    Bag<Integer> bag = new HashBag<>(Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 }));

    // From 3 we delete 1, 2 remain
    bag.remove(3, 1);
    assertThat(bag.getCount(3), equalTo(2));

    // From 2 we delete all
    bag.remove(1);
    assertThat(bag.getCount(1), equalTo(0));
}
 
Example #11
Source File: BagUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenAdd_whenCountOfElementsDefined_thenCountAreAdded() {
    Bag<Integer> bag = new HashBag<>();

    // Adding 1 for 5 times
    bag.add(1, 5);
    assertThat(bag.getCount(1), equalTo(5));
}
 
Example #12
Source File: BagUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDecoratedBag_whenBagAddAPILikeCollectionAPI_thenTrue() {
    Bag<Integer> bag = CollectionBag.collectionBag(new HashBag<>());

    bag.add(1);
    // This time the behavior is compliant to the Java Collection
    assertThat(bag.add(1), is((true)));
}
 
Example #13
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Bag<Emote> getEmotesBag()
{
    unsupported();
    return null;
}
 
Example #14
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Bag<Role> getMentionedRolesBag()
{
    unsupported();
    return null;
}
 
Example #15
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Bag<TextChannel> getMentionedChannelsBag()
{
    unsupported();
    return null;
}
 
Example #16
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Bag<User> getMentionedUsersBag()
{
    unsupported();
    return null;
}
 
Example #17
Source File: ReceivedMessage.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Bag<Emote> getEmotesBag()
{
    return processMentions(MentionType.EMOTE, new HashBag<>(), false, this::matchEmote);
}
 
Example #18
Source File: ReceivedMessage.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Bag<Role> getMentionedRolesBag()
{
    return processMentions(MentionType.ROLE, new HashBag<>(), false, this::matchRole);
}
 
Example #19
Source File: ReceivedMessage.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Bag<TextChannel> getMentionedChannelsBag()
{
    return processMentions(MentionType.CHANNEL, new HashBag<>(), false, this::matchTextChannel);
}
 
Example #20
Source File: ReceivedMessage.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Bag<User> getMentionedUsersBag()
{
    return processMentions(MentionType.USER, new HashBag<>(), false, this::matchUser);
}
 
Example #21
Source File: BagUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenMultipleCopies_whenAdded_theCountIsKept() {
    Bag<Integer> bag = new HashBag<>(Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 }));

    assertThat(bag.getCount(1), equalTo(2));
}
 
Example #22
Source File: Message.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * A {@link org.apache.commons.collections4.Bag Bag} of emotes used in this message.
 * <br>This can be used to retrieve the amount of times an emote was used in this message.
 *
 * <h2>Example</h2>
 * <pre>{@code
 * public void sendCount(Message msg)
 * {
 *     List<Emote> emotes = msg.getEmotes(); // distinct list, in order of appearance
 *     Bag<Emote> count = msg.getEmotesBag();
 *     StringBuilder content = new StringBuilder();
 *     for (Emote emote : emotes)
 *     {
 *         content.append(emote.getName())
 *                .append(": ")
 *                .append(count.getCount(role))
 *                .append("\n");
 *     }
 *     msg.getChannel().sendMessage(content.toString()).queue();
 * }
 * }</pre>
 *
 * @return {@link org.apache.commons.collections4.Bag Bag} of used emotes
 *
 * @see    #getEmotes()
 */
@Nonnull
Bag<Emote> getEmotesBag();
 
Example #23
Source File: Message.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * A {@link org.apache.commons.collections4.Bag Bag} of mentioned roles.
 * <br>This can be used to retrieve the amount of times a role was mentioned in this message. This only
 * counts direct mentions of the role and not mentions through the everyone tag.
 * If a role is not {@link net.dv8tion.jda.api.entities.Role#isMentionable() mentionable} it will not be included.
 *
 * <h2>Example</h2>
 * <pre>{@code
 * public void sendCount(Message msg)
 * {
 *     List<Role> mentions = msg.getMentionedRoles(); // distinct list, in order of appearance
 *     Bag<Role> count = msg.getMentionedRolesBag();
 *     StringBuilder content = new StringBuilder();
 *     for (Role role : mentions)
 *     {
 *         content.append(role.getName())
 *                .append(": ")
 *                .append(count.getCount(role))
 *                .append("\n");
 *     }
 *     msg.getChannel().sendMessage(content.toString()).queue();
 * }
 * }</pre>
 *
 * @return {@link org.apache.commons.collections4.Bag Bag} of mentioned roles
 *
 * @see    #getMentionedRoles()
 */
@Nonnull
Bag<Role> getMentionedRolesBag();
 
Example #24
Source File: Message.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * A {@link org.apache.commons.collections4.Bag Bag} of mentioned channels.
 * <br>This can be used to retrieve the amount of times a channel was mentioned in this message.
 *
 * <h2>Example</h2>
 * <pre>{@code
 * public void sendCount(Message msg)
 * {
 *     List<TextChannel> mentions = msg.getMentionedTextChannels(); // distinct list, in order of appearance
 *     Bag<TextChannel> count = msg.getMentionedTextChannelsBag();
 *     StringBuilder content = new StringBuilder();
 *     for (TextChannel channel : mentions)
 *     {
 *         content.append("#")
 *                .append(channel.getName())
 *                .append(": ")
 *                .append(count.getCount(channel))
 *                .append("\n");
 *     }
 *     msg.getChannel().sendMessage(content.toString()).queue();
 * }
 * }</pre>
 *
 * @return {@link org.apache.commons.collections4.Bag Bag} of mentioned channels
 *
 * @see    #getMentionedChannels()
 */
@Nonnull
Bag<TextChannel> getMentionedChannelsBag();
 
Example #25
Source File: Message.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * A {@link org.apache.commons.collections4.Bag Bag} of mentioned users.
 * <br>This can be used to retrieve the amount of times a user was mentioned in this message. This only
 * counts direct mentions of the user and not mentions through roles or the everyone tag.
 *
 * <h2>Example</h2>
 * <pre>{@code
 * public void sendCount(Message msg)
 * {
 *     List<User> mentions = msg.getMentionedUsers(); // distinct list, in order of appearance
 *     Bag<User> count = msg.getMentionedUsersBag();
 *     StringBuilder content = new StringBuilder();
 *     for (User user : mentions)
 *     {
 *         content.append(user.getAsTag())
 *                .append(": ")
 *                .append(count.getCount(user))
 *                .append("\n");
 *     }
 *     msg.getChannel().sendMessage(content.toString()).queue();
 * }
 * }</pre>
 *
 * @return {@link org.apache.commons.collections4.Bag Bag} of mentioned users
 *
 * @see    #getMentionedUsers()
 */
@Nonnull
Bag<User> getMentionedUsersBag();