@SuppressWarnings("rawtypes") public JsonSet(Collection existing) { super(); for (Object o : existing) { JsonElement fromObject = fromObject(o); if (!contains(fromObject)) { add(fromObject); } } }
/** * Variant of add that takes a string instead of a JsonElement. The inherited add only supports JsonElement. * * @param s * element */ @Override public boolean add(final String s) { JsonPrimitive primitive = primitive(s); if (!contains(primitive)) { return add(primitive); } else { return false; } }
/** * Variant of add that adds multiple strings. * * @param elements * elements */ @Override public void add(final String... elements) { for (String s : elements) { JsonPrimitive primitive = primitive(s); if (!contains(primitive)) { add(primitive); } } }
/** * Changes the strategy on the current set. * @param strategy id strategy * @return the current set. */ public JsonSet withIdStrategy(IdStrategy strategy) { this.strategy = strategy; if(size()>0) { JsonSet seen=new JsonSet().withIdStrategy(strategy); Iterator<JsonElement> iterator = this.iterator(); while (iterator.hasNext()) { JsonElement e = iterator.next(); if(seen.contains(e)) { iterator.remove(); } else { seen.add(e); } } } return this; }
public JsonSetAssert shouldNotContain(Object...elements) { isNotNull(); for(Object e:elements) { if(actual.contains(fromObject(e))) { failWithMessage("Expected JsonSet to not contain <%s> but was <%s>", e, actual.toString()); } } return this; }
public JsonSetAssert shouldContain(Object...elements) { isNotNull(); for(Object e:elements) { if(!actual.contains(fromObject(e))) { failWithMessage("Expected JsonSet to contain <%s> but was <%s>", e, actual.toString()); } } return this; }
@Override public boolean add(JsonElement e) { if (e == null) { e = nullValue(); } if (!contains(e)) { super.add(e); return true; } else if(strategy != null){ remove(e); // remove the old element that is identical according to the strategy super.add(e); } return false; }
public void shouldRemoveStringValue() { JsonSet set = set("foo","bar"); set.remove("foo"); assertThat(set.contains("foo")).isFalse(); assertThat(set.toString().contains("foo")).isFalse(); }
public void shouldSupportNulls() { JsonSet set = set(null, nullValue()); assertTrue(set.contains(nullValue()),"should contain null"); assertThat(set.size(), is(1)); }
public void setWithIdStrategyShouldContain() { JsonSet set = set(); set.withIdStrategy("id"); set.add(object(field("id","1"),field("name","foo"))); assertThat(set.contains(object(field("id","1"),field("name","bar")))).isTrue(); }