Commit 728131e4 authored by Mihai Nita's avatar Mihai Nita

Merged in mihai_equal_and_hash (pull request #13)

Fix for issue 22, fails in Unit parsing
parents 3f549e62 b2f2b206
Pipeline #55359828 (#53) passed with stages
in 4 minutes and 55 seconds
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.changeTracking;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.Const;
import net.sf.okapi.lib.xliff2.Util;
import net.sf.okapi.lib.xliff2.core.DataWithExtAttributes;
......@@ -153,4 +155,38 @@ public class Item extends DataWithExtAttributes implements IWithExtAttributes {
return "</" + Const.PREFIXCOL_TRACKINGSd + TAG_NAME + ">";
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), property, text);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Item other = (Item) obj;
if (property == null) {
if (other.property != null) {
return false;
}
} else if (!property.equals(other.property)) {
return false;
}
if (text == null) {
if (other.text != null) {
return false;
}
} else if (!text.equals(other.text)) {
return false;
}
return true;
}
}
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.InvalidParameterException;
import net.sf.okapi.lib.xliff2.Util;
......@@ -102,27 +104,27 @@ public class CTag extends Tag {
}
@Override
public boolean equals (Tag tag) {
if ( tag == null ) return false;
if ( this == tag ) return true;
if ( !(tag instanceof CTag) ) return false;
CTag ctag = (CTag)tag;
if ( tagType.compareTo(ctag.getTagType()) != 0 ) return false;
if ( Util.compareAllowingNull(getId(), ctag.getId()) != 0 ) return false;
if ( !cc.equals(ctag.cc) ) return false;
if ( Util.compareAllowingNull(data, ctag.getData()) != 0 ) return false;
if ( Util.compareAllowingNull(dataRef, ctag.getDataRef()) != 0 ) return false;
if ( Util.compareAllowingNull(subFlows, ctag.getSubFlows()) != 0 ) return false;
if ( Util.compareAllowingNull(disp, ctag.getDisp()) != 0 ) return false;
if ( Util.compareAllowingNull(equiv, ctag.getEquiv()) != 0 ) return false;
if ( !getCanReorder().equals(ctag.getCanReorder()) ) return false;
if ( initialWithData != ctag.initialWithData ) return false;
if ( dataDir != ctag.dataDir ) return false;
public boolean equals(Object other) {
if (other == null) return false;
if (this == other) return true;
if (!(other instanceof CTag)) return false;
if (!super.equals(other)) return false;
CTag cTag = (CTag) other;
return initialWithData == cTag.initialWithData &&
Objects.equals(cc, cTag.cc) &&
Objects.equals(data, cTag.data) &&
Objects.equals(dataRef, cTag.dataRef) &&
dataDir == cTag.dataDir &&
Objects.equals(disp, cTag.disp) &&
Objects.equals(equiv, cTag.equiv) &&
Objects.equals(subFlows, cTag.subFlows) &&
canReorder == cTag.canReorder;
}
return true;
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), cc, data, initialWithData, dataRef, dataDir, disp, equiv, subFlows, canReorder);
}
@Override
......
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.Const;
import net.sf.okapi.lib.xliff2.InvalidParameterException;
import net.sf.okapi.lib.xliff2.Util;
......@@ -134,18 +136,29 @@ class CTagCommon {
this.dir = dir;
}
public boolean equals (CTagCommon other) {
if ( other == null ) return false;
if ( this == other) return true;
if ( Util.compareAllowingNull(getId(), other.getId()) != 0 ) return false;
if ( Util.compareAllowingNull(getType(), other.getType()) != 0 ) return false;
if ( Util.compareAllowingNull(getSubType(), other.getSubType()) != 0 ) return false;
if ( Util.compareAllowingNull(getCopyOf(), other.getCopyOf()) != 0 ) return false;
if ( getDir() != other.getDir() ) return false;
if ( getCanCopy() != other.getCanCopy() ) return false;
if ( getCanDelete() != other.getCanDelete() ) return false;
if ( getCanOverlap() != other.getCanOverlap() ) return false;
return true;
/**
* Indicates if this object is equals to a given one.
* @param other the other object to compare.
* @return true if the two objects are identical.
*/
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (this == other) return true;
if (!(other instanceof CTagCommon)) return false;
CTagCommon that = (CTagCommon) other;
return hints == that.hints &&
canOverlap == that.canOverlap &&
Objects.equals(id, that.id) &&
Objects.equals(type, that.type) &&
Objects.equals(subType, that.subType) &&
Objects.equals(copyOf, that.copyOf) &&
dir == that.dir;
}
@Override
public int hashCode() {
return Objects.hash(id, type, hints, canOverlap, subType, copyOf, dir);
}
}
......@@ -16,6 +16,9 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Iterator;
import java.util.Objects;
/**
* Implements the {@link IWithExtAttributes} interface.
*/
......@@ -66,5 +69,36 @@ public class DataWithExtAttributes implements IWithExtAttributes {
if ( xattrs == null ) return null;
return xattrs.getAttributeValue(namespaceURI, localName);
}
/**
* Indicates if this object is equals to a given one.
* @param other the other object to compare.
* @return true if the two objects are identical.
*/
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (this == other) return true;
if (!(other instanceof DataWithExtAttributes)) return false;
DataWithExtAttributes that = (DataWithExtAttributes) other;
if (this.xattrs == that.xattrs) return true;
if (this.xattrs == null || that.xattrs == null) return false;
Iterator<ExtAttribute> thisIter = this.xattrs.iterator();
Iterator<ExtAttribute> thatIter = that.xattrs.iterator();
while (thisIter.hasNext() ) {
if (!thatIter.hasNext() ) return false;
ExtAttribute thisObj = thisIter.next();
ExtAttribute thatObj = thatIter.next();
if (!Objects.equals(thisObj, thatObj)) return false;
}
return true;
}
@Override
public int hashCode() {
return Objects.hash(xattrs);
}
}
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
import javax.xml.namespace.QName;
import net.sf.okapi.lib.xliff2.Const;
......@@ -103,5 +105,20 @@ public class ExtAttribute {
public boolean isModule () {
return qName.getNamespaceURI().startsWith(Const.NS_XLIFF_MODSTART);
}
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (this == other) return true;
if (!(other instanceof ExtAttribute)) return false;
ExtAttribute that = (ExtAttribute) other;
return Objects.equals(qName, that.qName) &&
Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(qName, value);
}
}
......@@ -18,6 +18,7 @@ package net.sf.okapi.lib.xliff2.core;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.xml.namespace.QName;
......@@ -202,4 +203,38 @@ public class ExtElement extends DataWithExtAttributes implements IExtChild {
return qName.getNamespaceURI().startsWith(Const.NS_XLIFF_MODSTART);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), children, qName);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ExtElement other = (ExtElement) obj;
if (children == null) {
if (other.children != null) {
return false;
}
} else if (!children.equals(other.children)) {
return false;
}
if (qName == null) {
if (other.qName != null) {
return false;
}
} else if (!qName.equals(other.qName)) {
return false;
}
return true;
}
}
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.its.ITSItems;
import net.sf.okapi.lib.xliff2.its.IWithITSAttributes;
......@@ -155,13 +157,19 @@ public class MTag extends Tag implements IWithITSAttributes {
}
@Override
public boolean equals (Tag tag) {
if ( tag == null ) return false;
if ( this == tag ) return true;
if ( !(tag instanceof MTag) ) return false;
MTag mtag = (MTag)tag;
if ( tagType.compareTo(mtag.getTagType()) != 0 ) return false;
return mc.equals(mtag.mc);
public boolean equals(Object other) {
if ( other == null ) return false;
if (this == other) return true;
if ( !(other instanceof MTag) ) return false;
if (!super.equals(other)) return false;
MTag mTag = (MTag) other;
return Objects.equals(mc, mTag.mc);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), mc);
}
@Override
......
......@@ -16,15 +16,16 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.InvalidParameterException;
import net.sf.okapi.lib.xliff2.Util;
import net.sf.okapi.lib.xliff2.its.ITSItems;
import net.sf.okapi.lib.xliff2.its.IWithITSAttributes;
/**
* Represents common data for the opening and closing {@link MTag}.
*/
class MTagCommon implements IWithITSAttributes {
public class MTagCommon implements IWithITSAttributes {
private String id;
private String type;
......@@ -114,17 +115,24 @@ class MTagCommon implements IWithITSAttributes {
* @param other the other object to compare.
* @return true if the two objects are identical.
*/
public boolean equals (MTagCommon other) {
if ( other == null ) return false;
if ( this == other ) return true;
if ( Util.compareAllowingNull(type, other.getType()) != 0 ) return false;
if ( Util.compareAllowingNull(id, other.getId()) != 0 ) return false;
if ( Util.compareAllowingNull(ref, other.getRef()) != 0 ) return false;
if ( Util.compareAllowingNull(value, other.getValue()) != 0 ) return false;
// Note that translate can be null, and it's OK
if ( translate != other.getTranslate() ) return false;
//TODO: compare the ITS items
return true;
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (this == other) return true;
if (!(other instanceof MTagCommon)) return false;
MTagCommon that = (MTagCommon) other;
return Objects.equals(id, that.id) &&
Objects.equals(type, that.type) &&
Objects.equals(value, that.value) &&
Objects.equals(ref, that.ref) &&
Objects.equals(translate, that.translate) &&
Objects.equals(itsItems, that.itsItems);
}
@Override
public int hashCode() {
return Objects.hash(id, type, value, ref, translate, itsItems);
}
@Override
......
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.InvalidParameterException;
import net.sf.okapi.lib.xliff2.Util;
......@@ -192,4 +194,51 @@ public class Note extends DataWithExtAttributes {
this.priority = priority;
}
@Override
public int hashCode () {
return Objects.hash(super.hashCode(), appliesTo, category, content, id, priority);
}
@Override
public boolean equals (Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Note other = (Note)obj;
if (appliesTo != other.appliesTo) {
return false;
}
if (category == null) {
if (other.category != null) {
return false;
}
} else if (!category.equals(other.category)) {
return false;
}
if (content == null) {
if (other.content != null) {
return false;
}
} else if (!content.equals(other.content)) {
return false;
}
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
if (priority != other.priority) {
return false;
}
return true;
}
}
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.InvalidParameterException;
import net.sf.okapi.lib.xliff2.Util;
......@@ -99,4 +101,45 @@ public class StartXliffData extends DataWithExtAttributes {
getExtAttributes().setNamespace(prefix, namespaceURI);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), sourceLang, targetLang, version);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
StartXliffData other = (StartXliffData) obj;
if (sourceLang == null) {
if (other.sourceLang != null) {
return false;
}
} else if (!sourceLang.equals(other.sourceLang)) {
return false;
}
if (targetLang == null) {
if (other.targetLang != null) {
return false;
}
} else if (!targetLang.equals(other.targetLang)) {
return false;
}
if (version == null) {
if (other.version != null) {
return false;
}
} else if (!version.equals(other.version)) {
return false;
}
return true;
}
}
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.core;
import java.util.Objects;
/**
* Provides the common methods for code and marker tags.
* <p>Any object deriving from this class must also provide an implementation
......@@ -85,14 +87,6 @@ abstract public class Tag extends DataWithExtAttributes {
*/
abstract public void setType (String type);
/**
* Indicates if this tag is equal to another.
* <p>Use the <code>==</code> operator to test if two tags are the same.
* @param tag the other tag to compare to this one.
* @return true if both tags are equals.
*/
abstract boolean equals (Tag tag);
/**
* Indicates if this tag is for a marker ({@link MTag}).
* @return true if this tag is one for a marker ({@link MTag}), false if it is for a code ({@link CTag}).
......@@ -105,4 +99,25 @@ abstract public class Tag extends DataWithExtAttributes {
*/
abstract public boolean isCode ();
/**
* Indicates if this tag is equal to another.
* <p>Use the <code>==</code> operator to test if two tags are the same.
* @param other the other tag to compare to this one.
* @return true if both tags are equals.
*/
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (this == other) return true;
if (!(other instanceof Tag)) return false;
if (!super.equals(other)) return false;
Tag tag = (Tag) other;
return tagType == tag.tagType;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), tagType);
}
}
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.glossary;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.core.DataWithExtAttributes;
/**
......@@ -75,5 +77,39 @@ abstract class BaseGlossaryField extends DataWithExtAttributes {
public void setSource (String source) {
this.source = source;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), source, text);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BaseGlossaryField other = (BaseGlossaryField) obj;
if (source == null) {
if (other.source != null) {
return false;
}
} else if (!source.equals(other.source)) {
return false;
}
if (text == null) {
if (other.text != null) {
return false;
}
} else if (!text.equals(other.text)) {
return false;
}
return true;
}
}
......@@ -16,6 +16,8 @@
package net.sf.okapi.lib.xliff2.its;
import java.util.Objects;
import net.sf.okapi.lib.xliff2.InvalidParameterException;
import net.sf.okapi.lib.xliff2.XLIFFException;
import net.sf.okapi.lib.xliff2.core.MTag;
......@@ -233,4 +235,20 @@ public class TermTag extends MTag {
setValue(null);
}
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (this == other) return true;
if (!(other instanceof TermTag)) return false;
if (!super.equals(other)) return false;
TermTag termTag = (TermTag) other;
return Objects.equals(annotatorRef, termTag.annotatorRef) &&
Objects.equals(termConfidence, termTag.termConfidence);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), annotatorRef, termConfidence);
}
}
......@@ -3585,6 +3585,10 @@ public class XLIFFReader implements Closeable {
}
unit.setMatches(matches);
popSpecialIds();
// Clear the list of isolated element because the one found in matches may conflict
// with the ones in the segments 9and we check only the ones in segments)
srcIsolated.clear();
trgIsolated.clear();
return;
}
}
......
......@@ -13,6 +13,7 @@ import net.sf.okapi.lib.xliff2.core.Note.AppliesTo;
import net.sf.okapi.lib.xliff2.matches.Match;
import net.sf.okapi.lib.xliff2.reader.Event;
import net.sf.okapi.lib.xliff2.reader.XLIFFReader;
import net.sf.okapi.lib.xliff2.reader.XLIFFReaderException;
import net.sf.okapi.lib.xliff2.test.U;
import org.junit.Test;
......@@ -796,6 +797,24 @@ public class UnitTest {
assertTrue(sameFragments(s1.getSource(), s2.getSource()));
}
@Test
public void testMatchWithIsolatedInMatch () {
String xlfStr = getTestXlfWithIsolatedInMatch();
Unit unit = getUnit(xlfStr, 1);
assertNotNull("There seems to be no unit in this xlf doc. ", unit);
}
@Test
public void testMissingIsolatedAttribute () {
try {
String xlfStr = getTestXlfWithMissingIsolatedAttribute();
getUnit(xlfStr, 1);
}
catch ( XLIFFReaderException e ) {
assertTrue(e.getMessage().contains("Missing isolated='yes' for opening code id='1'"));
}
}
@Test
public void testGetMatches() {
String xlfStr = getTestXlf();
......@@ -859,6 +878,46 @@ public class UnitTest {
assertEquals(0, unit.getMatches().size());
}