Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/org/verapdf/cos/COSArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,49 @@ private COSObject _at(final int i) {
return this.entries.get(i);
}

@Override
public boolean isEquivalentTo(COSBase o) {
if (this == o) return true;
if (o == null) return false;

if (o.isIndirect()) {
return this.isEquivalentTo(o.getDirectBase());
}

if (!(o instanceof COSArray)) return false;
List<COSBasePair> checkedObjects = new LinkedList<>();
return isEquivalentTo(o, checkedObjects);
}

@Override
boolean isEquivalentTo(COSBase o, List<COSBasePair> checkedObjects) {
if (this == o) return true;
if (o == null) return false;

if (!(o instanceof COSArray)) return false;

COSArray that = (COSArray) o;
if (COSBasePair.listContainsPair(checkedObjects, this, that)) {
return true;
}
COSBasePair.addPairToList(checkedObjects, this, that);

if (!Objects.equals(this.size(), that.size())) return false;

for (int i = 0; i < this.size(); ++i) {
COSBase thisElem = this.at(i).getDirectBase();
COSBase thatElem = that.at(i).getDirectBase();

if (thisElem == null && thatElem == null) continue;
if (thisElem == null || thatElem == null) return false;

if (!thisElem.isEquivalentTo(thatElem, checkedObjects)) {
return false;
}
}
return true;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/verapdf/cos/COSBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ public void setObjectKey(COSKey indirectKey) {

public abstract void mark();

public abstract boolean isEquivalentTo(COSBase obj);

boolean isEquivalentTo(COSBase o, List<COSBasePair> checkedObjects) {
return isEquivalentTo(o);
}

boolean equals(Object obj, List<COSBasePair> checkedObjects) {
return this.equals(obj);
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/verapdf/cos/COSBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ public boolean setBoolean(final boolean value) {
return true;
}

@Override
public boolean isEquivalentTo(COSBase o) {
if (this == o) return true;
if (o == null) return false;

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

if (!(o instanceof COSBoolean)) return false;

COSBoolean that = (COSBoolean) o;

return value == that.value;

}

public boolean get() {
return this.value;
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/verapdf/cos/COSDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,73 @@ public Collection<COSObject> getValues() {
return this.entries.values();
}

private Set<ASAtom> getNonNullKeySet() {
Set<ASAtom> nonNullKeys = new HashSet<>();
for (ASAtom key : getKeySet()) {
COSBase value = getKey(key).getDirectBase();
if (value != null) {
nonNullKeys.add(key);
}
}
return nonNullKeys;
}

@Override
public boolean isEquivalentTo(COSBase o) {
if (this == o) return true;
if (o == null) return false;

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

if (!(o instanceof COSDictionary)) return false;

List<COSBasePair> checkedObjects = new LinkedList<>();
return isEquivalentTo(o, checkedObjects);
}

/**
* Internal recursive comparison with cycle detection.
*/
boolean isEquivalentTo(COSBase o, List<COSBasePair> checkedObjects) {
if (this == o) return true;
if (o == null) return false;
if (!(o instanceof COSDictionary)) return false;
Comment thread
LonelyMidoriya marked this conversation as resolved.

COSDictionary that = (COSDictionary) o;

if (COSBasePair.listContainsPair(checkedObjects, this, that)) {
return true;
}
COSBasePair.addPairToList(checkedObjects, this, that);

Set<ASAtom> thisKeys = this.getNonNullKeySet();
Set<ASAtom> thatKeys = that.getNonNullKeySet();

if (!thisKeys.equals(thatKeys)) {
return false;
}

return isEquivalentKeys(that, thisKeys, checkedObjects);
}

protected boolean isEquivalentKeys(COSDictionary that, Set<ASAtom> thisKeys, List<COSBasePair> checkedObjects) {
for (ASAtom key : thisKeys) {
COSBase thisVal = this.getKey(key).getDirectBase();
COSBase thatVal = that.getKey(key).getDirectBase();

if (thisVal == null && thatVal == null) continue;
if (thisVal == null || thatVal == null) return false;

if (!thisVal.isEquivalentTo(thatVal, checkedObjects)) {
return false;
}
}

return true;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/verapdf/cos/COSIndirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ public void mark() {
}
}

@Override
public boolean isEquivalentTo(COSBase o) {
if (this == o) return true;

COSBase thisBase = this.getDirectBase();
if (thisBase == null && o == null) return true;
if (thisBase == null || o == null) return false;

return thisBase.isEquivalentTo(o.getDirectBase());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/verapdf/cos/COSInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.verapdf.cos.visitor.ICOSVisitor;
import org.verapdf.cos.visitor.IVisitor;

import java.math.BigDecimal;

/**
* @author Timur Kamalov
*/
Expand Down Expand Up @@ -75,6 +77,27 @@ public boolean setReal(final double value) {
return true;
}

@Override
public boolean isEquivalentTo(COSBase o) {
if (this == o) return true;
if (o == null) return false;

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

if (o instanceof COSInteger) {
return this.value == ((COSInteger) o).value;
}

if (o instanceof COSReal) {
COSReal thatReal = (COSReal) o;
return BigDecimal.valueOf(this.value).compareTo(thatReal.getDecimalValue()) == 0;
}

return false;
}

public long get() {
return this.value;
}
Expand All @@ -97,4 +120,9 @@ public boolean equals(Object o) {
return value == that.value;

}

@Override
public BigDecimal getDecimalValue() {
return BigDecimal.valueOf(value);
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/verapdf/cos/COSName.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.verapdf.cos.visitor.IVisitor;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;

/**
Expand Down Expand Up @@ -91,6 +92,22 @@ public boolean setName(final ASAtom value) {
return true;
}

@Override
public boolean isEquivalentTo(COSBase o) {
if (this == o) return true;
if (o == null) return false;

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

if (!(o instanceof COSName)) return false;

COSName that = (COSName) o;

return Objects.equals(value, that.value);
}

Comment thread
LonelyMidoriya marked this conversation as resolved.
public ASAtom get() {
return value;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/verapdf/cos/COSNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public Object accept(final ICOSVisitor visitor) {
return visitor.visitFromNull(this);
}

@Override
public boolean isEquivalentTo(COSBase o) {
return equals(o);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/verapdf/cos/COSNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package org.verapdf.cos;

import java.math.BigDecimal;

/**
* @author Timur Kamalov
*/
Expand All @@ -29,4 +31,6 @@ public COSNumber() {
super();
}

public abstract BigDecimal getDecimalValue();

}
12 changes: 12 additions & 0 deletions src/main/java/org/verapdf/cos/COSObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,18 @@ public void setIsHeaderFormatComplyPDFA(Boolean isHeaderFormatComplyPDFA) {
this.isHeaderFormatComplyPDFA = isHeaderFormatComplyPDFA;
}

public boolean isEquivalentTo(COSObject o) {
if (o == this) return true;
if (o == null) return false;

COSBase thisBase = this.getDirectBase();
COSBase otherBase = o.getDirectBase();
if (thisBase == null && otherBase == null) return true;
if (thisBase == null || otherBase == null) return false;

return thisBase.isEquivalentTo(otherBase);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/verapdf/cos/COSReal.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.verapdf.cos.visitor.ICOSVisitor;
import org.verapdf.cos.visitor.IVisitor;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

Expand All @@ -45,6 +46,11 @@ public class COSReal extends COSNumber {
protected COSReal() {
}

@Override
public BigDecimal getDecimalValue() {
return BigDecimal.valueOf(value);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

protected COSReal(final double value) {
this.value = value;
}
Expand Down Expand Up @@ -88,6 +94,27 @@ public boolean setReal(final double value) {
return true;
}

@Override
public boolean isEquivalentTo(COSBase o) {
if (this == o) return true;

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

if (o instanceof COSReal) {
COSReal thatReal = (COSReal) o;
return this.getDecimalValue().compareTo(thatReal.getDecimalValue()) == 0;
}

if (o instanceof COSInteger) {
COSInteger thatInt = (COSInteger) o;
return this.getDecimalValue().compareTo(thatInt.getDecimalValue()) == 0;
}

return false;
}

public double get() {
return this.value;
}
Expand Down
Loading
Loading