Skip to content

Commit

Permalink
Merge pull request #119 from InteractiveAdvertisingBureau/issue-118
Browse files Browse the repository at this point in the history
When encoding using an existing tcf string, added changes to not igno…
  • Loading branch information
srinivas81 authored Dec 7, 2020
2 parents f2b5573 + 06e42e1 commit fb922e6
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ private int fillPublisherRestrictions(
BitSet bs = new BitSet();
currentPointer = vendorIdsFromRange(bbv, bs, currentPointer, Optional.empty());
PublisherRestriction publisherRestriction =
new PublisherRestriction(purposeId, restrictionType, BitSetIntIterable.from(bs));
PublisherRestriction.newBuilder()
.purposeId(purposeId)
.restrictionType(restrictionType)
.addVendor(BitSetIntIterable.from(bs)).build();
publisherRestrictions.add(publisherRestriction);
}
return currentPointer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Objects;
import java.util.StringJoiner;

import com.iabtcf.utils.BitSetIntIterable;
import com.iabtcf.utils.IntIterable;
import com.iabtcf.utils.IntIterator;

Expand All @@ -32,7 +33,7 @@ public class PublisherRestriction {
private final RestrictionType restrictionType;
private final IntIterable vendorIds;

public PublisherRestriction(
private PublisherRestriction(
int purposeId, RestrictionType restrictionType, IntIterable vendorIds) {
Objects.requireNonNull(vendorIds);
Objects.requireNonNull(restrictionType);
Expand Down Expand Up @@ -88,4 +89,86 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(purposeId, restrictionType, vendorIds);
}

public static class Builder {
private int purposeId;
private RestrictionType restrictionType;
private final BitSetIntIterable.Builder vendorIds = BitSetIntIterable.newBuilder();

public Builder() {
}

public Builder(PublisherRestriction prototype) {
purposeId = prototype.purposeId;
restrictionType = prototype.restrictionType;
vendorIds.add(prototype.vendorIds);
}

public Builder(Builder prototype) {
purposeId = prototype.purposeId;
restrictionType = prototype.restrictionType;
vendorIds.add(prototype.vendorIds);
}

public Builder purposeId(int purposeId) {
this.purposeId = purposeId;
return this;
}

public Builder restrictionType(RestrictionType restrictionType) {
this.restrictionType = restrictionType;
return this;
}

public Builder addVendor(int vendorId) {
if (vendorId < 1) {
throw new IllegalArgumentException("vendor id must be > 0: " + vendorId);
}

vendorIds.add(vendorId);
return this;
}

public Builder addVendor(int... vendorIds) {
for (int i = 0; i < vendorIds.length; i++) {
addVendor(vendorIds[i]);
}
return this;
}

public Builder addVendor(IntIterable vendorIds) {
for (IntIterator ii = vendorIds.intIterator(); ii.hasNext();) {
addVendor(ii.nextInt());
}
return this;
}

public Builder clearVendors() {
vendorIds.clear();
return this;
}

public PublisherRestriction build() {
if (purposeId <= 0) {
throw new IllegalArgumentException("purposeId must be positive: " + purposeId);
}

return new PublisherRestriction(purposeId,
restrictionType,
vendorIds.build());
}

}

public static Builder newBuilder() {
return new Builder();
}

public static Builder newBuilder(PublisherRestriction prototype) {
return new Builder(prototype);
}

public static Builder newBuilder(Builder prototype) {
return new Builder(prototype);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,18 @@ public void testPublisherRestrictions() {
TCString tcModel = parse(base64FromBitString(bitString));

List<PublisherRestriction> actual = tcModel.getPublisherRestrictions();
PublisherRestriction publisherRestriction =
PublisherRestriction.newBuilder()
.purposeId(1)
.restrictionType(RestrictionType.REQUIRE_CONSENT)
.build();
List<PublisherRestriction> expected =
Arrays.asList(
new PublisherRestriction(1, RestrictionType.REQUIRE_CONSENT, BitSetIntIterable.EMPTY),
new PublisherRestriction(2, RestrictionType.NOT_ALLOWED, BitSetIntIterable.EMPTY),
new PublisherRestriction(
3,
RestrictionType.REQUIRE_LEGITIMATE_INTEREST,
BitSetIntIterable.EMPTY));
publisherRestriction,
PublisherRestriction.newBuilder(publisherRestriction).purposeId(2)
.restrictionType(RestrictionType.NOT_ALLOWED).build(),
PublisherRestriction.newBuilder(publisherRestriction).purposeId(3)
.restrictionType(RestrictionType.REQUIRE_LEGITIMATE_INTEREST).build());

assertEquals(expected, actual);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class PublisherRestrictionTest {
@Test
public void testToString() {
PublisherRestriction publisherRestriction =
new PublisherRestriction(1, RestrictionType.NOT_ALLOWED, BitSetIntIterable.from(1, 2));
PublisherRestriction.newBuilder().purposeId(1).restrictionType(RestrictionType.NOT_ALLOWED)
.addVendor(BitSetIntIterable.from(1, 2)).build();
assertEquals(
"PublisherRestriction{purposeId=1, restrictionType=NOT_ALLOWED, vendorIds=[1, 2]}",
publisherRestriction.toString());
Expand All @@ -39,9 +40,11 @@ public void testToString() {
@Test
public void testEquals() {
PublisherRestriction pub1 =
new PublisherRestriction(1, RestrictionType.NOT_ALLOWED, BitSetIntIterable.from(1, 2));
PublisherRestriction.newBuilder().purposeId(1).restrictionType(RestrictionType.NOT_ALLOWED)
.addVendor(BitSetIntIterable.from(1, 2)).build();
PublisherRestriction pub2 =
new PublisherRestriction(1, RestrictionType.NOT_ALLOWED, BitSetIntIterable.from(2, 1));
PublisherRestriction.newBuilder().purposeId(1).restrictionType(RestrictionType.NOT_ALLOWED)
.addVendor(BitSetIntIterable.from(2, 1)).build();

assertEquals(pub1, pub2);
assertEquals(pub1, pub1);
Expand All @@ -51,9 +54,11 @@ public void testEquals() {
@Test
public void testHash() {
PublisherRestriction pub1 =
new PublisherRestriction(1, RestrictionType.NOT_ALLOWED, BitSetIntIterable.from(1, 2));
PublisherRestriction.newBuilder().purposeId(1).restrictionType(RestrictionType.NOT_ALLOWED)
.addVendor(BitSetIntIterable.from(1, 2)).build();
PublisherRestriction pub2 =
new PublisherRestriction(1, RestrictionType.NOT_ALLOWED, BitSetIntIterable.from(1, 2));
PublisherRestriction.newBuilder().purposeId(1).restrictionType(RestrictionType.NOT_ALLOWED)
.addVendor(BitSetIntIterable.from(1, 2)).build();

assertEquals(pub1.hashCode(), pub2.hashCode());
}
Expand Down
3 changes: 2 additions & 1 deletion iabtcf-encoder/src/main/java/com/iabtcf/encoder/Bounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import com.iabtcf.encoder.exceptions.ValueOverflowException;
import com.iabtcf.utils.BitSetIntIterable;
import com.iabtcf.utils.FieldDefs;
import com.iabtcf.v2.PublisherRestriction;

class Bounds {
/**
* Verifies that number of entries are within bounds.
*/
public static List<PublisherRestrictionEntry> checkBounds(List<PublisherRestrictionEntry> value) {
public static List<PublisherRestriction> checkBounds(List<PublisherRestriction> value) {
checkBounds(value.size(), (1L << FieldDefs.CORE_NUM_PUB_RESTRICTION.getLength()) - 1,
FieldDefs.CORE_NUM_PUB_RESTRICTION);

Expand Down

This file was deleted.

Loading

0 comments on commit fb922e6

Please sign in to comment.