org.apache.commons.collections.Bag.add()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(143)

本文整理了Java中org.apache.commons.collections.Bag.add()方法的一些代码示例,展示了Bag.add()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bag.add()方法的具体详情如下:
包路径:org.apache.commons.collections.Bag
类名称:Bag
方法名:add

Bag.add介绍

Adds one copy the specified object to the Bag.

If the object is already in the #uniqueSet() then increment its count as reported by #getCount(Object). Otherwise add it to the #uniqueSet() and report its count as 1.

Since this method always increases the size of the bag, according to the Collection#add(Object) contract, it should always return true. Since it sometimes returns false, this method violates the contract.
[中](违反)将指定对象的一个副本添加到包中。
如果对象已在#uniqueSet()中,则增加其计数,如#getCount(对象)报告的那样。否则,将其添加到#uniqueSet()并将其计数报告为1。
由于此方法始终会增加袋子的大小,因此根据Collection#add(Object)契约,它应始终返回[$0$]。由于它有时返回false,因此此方法违反了约定。

代码示例

代码示例来源:origin: commons-collections/commons-collections

public boolean add(Object object, int count) {
  synchronized (lock) {
    return getBag().add(object, count);
  }
}

代码示例来源:origin: commons-collections/commons-collections

public boolean add(Object object, int count) {
  return getBag().add(object, count);
}

代码示例来源:origin: wildfly/wildfly

public boolean add(Object object, int count) {
  synchronized (lock) {
    return getBag().add(object, count);
  }
}

代码示例来源:origin: commons-collections/commons-collections

public boolean add(Object object, int nCopies) {
  object = transform(object);
  return getBag().add(object, nCopies);
}

代码示例来源:origin: commons-collections/commons-collections

public boolean add(Object object, int count) {
  validate(object);
  return getBag().add(object, count);
}

代码示例来源:origin: wildfly/wildfly

public boolean add(Object object, int nCopies) {
  object = transform(object);
  return getBag().add(object, nCopies);
}

代码示例来源:origin: commons-collections/commons-collections

public void testRetainAll() {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("A");
  bag.add("B");
  bag.add("B");
  bag.add("C");
  List retains = new ArrayList();
  retains.add("B");
  retains.add("C");
  bag.retainAll(retains);
  assertEquals("Should have 2 total items", 2, bag.size());
}

代码示例来源:origin: commons-collections/commons-collections

public void testIteratorFailNoMore() {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("B");
  Iterator it = bag.iterator();
  it.next();
  it.next();
  it.next();
  try {
    it.next();
    fail("Should throw NoSuchElementException");
  } catch (NoSuchElementException ex) {
    // expected
  }
}

代码示例来源:origin: commons-collections/commons-collections

public void testBagEqualsSelf() {
  Bag bag = makeBag();
  assertTrue(bag.equals(bag));
  bag.add("elt");
  assertTrue(bag.equals(bag));
  bag.add("elt"); // again
  assertTrue(bag.equals(bag));
  bag.add("elt2");
  assertTrue(bag.equals(bag));
}

代码示例来源:origin: commons-collections/commons-collections

public void testIteratorFail() {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("B");
  Iterator it = bag.iterator();
  it.next();
  bag.remove("A");
  try {
    it.next();
    fail("Should throw ConcurrentModificationException");
  } catch (ConcurrentModificationException e) {
    // expected
  }
}

代码示例来源:origin: commons-collections/commons-collections

public void testFullBagSerialization() throws IOException, ClassNotFoundException {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("B");
  bag.add("B");
  bag.add("C");
  int size = bag.size();
  if (!(bag instanceof Serializable && isTestSerialization())) return;
  
  byte[] objekt = writeExternalFormToBytes((Serializable) bag);
  Bag bag2 = (Bag) readExternalFormFromBytes(objekt);
  assertEquals("Bag should be same size", size, bag.size());
  assertEquals("Bag should be same size", size, bag2.size());
}

代码示例来源:origin: commons-collections/commons-collections

public void testIllegalAdd() {
  Bag bag = makeTestBag();
  Integer i = new Integer(3);
  try {
    bag.add(i);
    fail("Integer should fail type check.");
  } catch (IllegalArgumentException e) {
    // expected
  }
  assertTrue("Collection shouldn't contain illegal element", 
   !bag.contains(i));   
}

代码示例来源:origin: commons-collections/commons-collections

public void testIllegalAdd() {
  Bag bag = makeTestBag();
  Integer i = new Integer(3);
  try {
    bag.add(i);
    fail("Integer should fail string predicate.");
  } catch (IllegalArgumentException e) {
    // expected
  }
  assertTrue("Collection shouldn't contain illegal element", 
   !bag.contains(i));   
}

代码示例来源:origin: commons-collections/commons-collections

public void testRemoveAll() {
  Bag bag = makeBag();
  bag.add("A", 2);
  assertEquals("Should have count of 2", 2, bag.getCount("A"));
  bag.add("B");
  bag.add("C");
  assertEquals("Should have count of 4", 4, bag.size());
  List delete = new ArrayList();
  delete.add("A");
  delete.add("B");
  bag.removeAll(delete);
  assertEquals("Should have count of 1", 1, bag.getCount("A"));
  assertEquals("Should have count of 0", 0, bag.getCount("B"));
  assertEquals("Should have count of 1", 1, bag.getCount("C"));
  assertEquals("Should have count of 2", 2, bag.size());
}

代码示例来源:origin: commons-collections/commons-collections

public void testBagAdd() {
  Bag bag = makeBag();
  bag.add("A");
  assertTrue("Should contain 'A'", bag.contains("A"));
  assertEquals("Should have count of 1", 1, bag.getCount("A"));
  bag.add("A");
  assertTrue("Should contain 'A'", bag.contains("A"));
  assertEquals("Should have count of 2", 2, bag.getCount("A"));
  bag.add("B");
  assertTrue(bag.contains("A"));
  assertTrue(bag.contains("B"));
}

代码示例来源:origin: commons-collections/commons-collections

public void testTransformedBag() {
  Bag bag = TransformedSortedBag.decorate(new TreeBag(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(new Integer((String) els[i])));
  }
  
  assertEquals(true, bag.remove(new Integer((String) els[0])));
  
}

代码示例来源:origin: commons-collections/commons-collections

public void testContains() {
  Bag bag = makeBag();
  
  assertEquals("Bag does not have at least 1 'A'", false, bag.contains("A"));
  assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
  
  bag.add("A");  // bag 1A
  assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
  assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
  
  bag.add("A");  // bag 2A
  assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
  assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
  
  bag.add("B");  // bag 2A,1B
  assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
  assertEquals("Bag has at least 1 'B'", true, bag.contains("B"));
}

代码示例来源:origin: commons-collections/commons-collections

public void testTransformedBag() {
  Bag bag = TransformedBag.decorate(new HashBag(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(new Integer((String) els[i])));
    assertEquals(false, bag.contains(els[i]));
  }
  
  assertEquals(false, bag.remove(els[0]));
  assertEquals(true, bag.remove(new Integer((String) els[0])));
  
}

代码示例来源:origin: commons-collections/commons-collections

public void testlegalAddRemove() {
  Bag bag = makeTestBag();
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(els[i]));
  }
  Set set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set contains the first element",set.contains(els[0]));
  assertEquals(true, bag.remove(els[0])); 
  set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set now does not contain the first element",
    !set.contains(els[0])); 
}

代码示例来源:origin: commons-collections/commons-collections

public void testlegalAddRemove() {
  Bag bag = makeTestBag();
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(els[i]));
  }
  Set set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set contains the first element",set.contains(els[0]));
  assertEquals(true, bag.remove(els[0])); 
  set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set now does not contain the first element",
    !set.contains(els[0])); 
}

相关文章