You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2016/11/04 22:05:11 UTC
svn commit: r1768111 - in
/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl:
FeatureImpl.java TypeImpl.java
Author: schor
Date: Fri Nov 4 22:05:11 2016
New Revision: 1768111
URL: http://svn.apache.org/viewvc?rev=1768111&view=rev
Log:
no Jira - make compareTo, hashcode, equals for TypeImpl and FeatureImpl work across type systems (e.g., filtering types).
Modified:
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FeatureImpl.java
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeImpl.java
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FeatureImpl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FeatureImpl.java?rev=1768111&r1=1768110&r2=1768111&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FeatureImpl.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FeatureImpl.java Fri Nov 4 22:05:11 2016
@@ -162,18 +162,6 @@ public class FeatureImpl implements Feat
// return (isGet ? "get" : "set") + shortName1stLetterUpperCase;
// }
- /**
- * Note: you can only compare features from the same type system. If you compare features from
- * different type systems, the result is undefined.
- */
- public int compareTo(Feature o) {
- if (this == o) {
- return 0;
- }
- FeatureImpl f = (FeatureImpl) o;
- return (this.featureCode < f.featureCode) ? -1 : 1;
- }
-
public boolean isMultipleReferencesAllowed() {
return this.isMultipleRefsAllowed;
}
@@ -255,10 +243,15 @@ public class FeatureImpl implements Feat
*/
public final static FeatureImpl singleton = new FeatureImpl();
+ /**
+ * Hashcode and equals are used, possibly for features in different type systems,
+ * where the features should be "equal". Example: fitering during serialization.
+ */
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
+// return this.featureCode; // can't use this across different type systems
result = prime * result + ((highestDefiningType == null) ? 0 : highestDefiningType.getName().hashCode());
result = prime * result + (isMultipleRefsAllowed ? 1231 : 1237);
result = prime * result + ((rangeType == null) ? 0 : rangeType.getName().hashCode());
@@ -266,6 +259,30 @@ public class FeatureImpl implements Feat
return result;
}
+ /**
+ * This should work across different type systems, for instance
+ * when using filtered serialization
+ */
+ @Override
+ public int compareTo(Feature o) {
+ if (this == o) {
+ return 0;
+ }
+ FeatureImpl other = (FeatureImpl) o;
+
+ int c;
+ c = this.shortName.compareTo(other.shortName);
+ if (c != 0) return c;
+ c = highestDefiningType.getName().compareTo(other.highestDefiningType.getName());
+ if (c != 0) return c;
+ c = Boolean.compare(this.isMultipleRefsAllowed, other.isMultipleRefsAllowed);
+ if (c != 0) return c;
+ c = rangeType.getName().compareTo(other.rangeType.getName());
+ if (c != 0) return c;
+
+ return 0;
+ }
+
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
@@ -273,6 +290,7 @@ public class FeatureImpl implements Feat
if (!(obj instanceof FeatureImpl)) return false;
FeatureImpl other = (FeatureImpl) obj;
+// return this.featureCode == other.featureCode; // can't use this across different type systems
if (!highestDefiningType.getName().equals(other.highestDefiningType.getName())) return false;
if (isMultipleRefsAllowed != other.isMultipleRefsAllowed) return false;
if (!rangeType.getName().equals(other.rangeType.getName())) return false;
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeImpl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeImpl.java?rev=1768111&r1=1768110&r2=1768111&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeImpl.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeImpl.java Fri Nov 4 22:05:11 2016
@@ -406,22 +406,6 @@ public class TypeImpl implements Type, C
return this.tsi;
}
-
-
- /**
- * Note: you can only compare types from the same type system. If you compare types from different
- * type systems, the result is undefined.
- */
- @Override
- public int compareTo(TypeImpl t) {
- if (this == t) {
- return 0;
- }
-
- return Integer.compare(System.identityHashCode(this),
- System.identityHashCode(t));
- }
-
/**
* @see org.apache.uima.cas.Type#getFeatureByBaseName(String)
*/
@@ -976,6 +960,10 @@ public class TypeImpl implements Type, C
}
}
+ /**
+ * works across type systems
+ * @return -
+ */
private int computeHashCode() {
final int prime = 31;
int result = 1;
@@ -994,6 +982,7 @@ public class TypeImpl implements Type, C
/**
* Equal TypeImpl
+ * Works across type systems.
*/
@Override
public boolean equals(Object obj) {
@@ -1013,18 +1002,80 @@ public class TypeImpl implements Type, C
}
if (directSubtypes.size() != other.directSubtypes.size()) return false;
- for (int i = 0; i < directSubtypes.size(); i++) {
- if (!directSubtypes.get(i).name.equals(other.directSubtypes.get(i).name)) return false;
- }
if (isFeatureFinal != other.isFeatureFinal) return false;
if (isInheritanceFinal != other.isInheritanceFinal) return false;
+ if (this.getNumberOfFeatures() != other.getNumberOfFeatures()) return false;
+
final FeatureImpl[] fis1 = getFeatureImpls();
final FeatureImpl[] fis2 = other.getFeatureImpls();
- return Arrays.equals(fis1, fis2);
+ if (!Arrays.equals(fis1, fis2)) return false;
+
+ for (int i = 0; i < directSubtypes.size(); i++) {
+ if (!directSubtypes.get(i).name.equals(other.directSubtypes.get(i).name)) return false;
+ }
+
+ return true;
}
+ /**
+ * compareTo must return 0 for "equal" types
+ *
+ * use the fully qualified names as the comparison
+ * Note: you can only compare types from the same type system. If you compare types from different
+ * type systems, the result is undefined.
+ */
+ @Override
+ public int compareTo(TypeImpl t) {
+ if (this == t) {
+ return 0;
+ }
+
+ int c = this.name.compareTo(t.name);
+ if (c != 0) return c;
+
+ c = Integer.compare(this.hashCode(), t.hashCode());
+ if (c != 0) return c;
+
+ // if get here, we have two types of the same name, and same hashcode
+ // may or may not be equal
+ // check the parts.
+
+ if (this.superType == null || t.superType == null) {
+ Misc.internalError();
+ };
+
+ c = this.superType.name.compareTo(t.superType.name);
+ if (c != 0) return c;
+
+ c = Integer.compare(this.directSubtypes.size(), t.directSubtypes.size());
+ if (c != 0) return c;
+
+ c = Integer.compare(this.getNumberOfFeatures(), t.getNumberOfFeatures());
+ if (c != 0) return c;
+
+ c = Boolean.compare(this.isFeatureFinal, t.isFeatureFinal);
+ if (c != 0) return c;
+ c = Boolean.compare(this.isInheritanceFinal, t.isInheritanceFinal);
+ if (c != 0) return c;
+
+ final FeatureImpl[] fis1 = getFeatureImpls();
+ final FeatureImpl[] fis2 = t.getFeatureImpls();
+
+ for (int i = 0; i < fis1.length; i++) {
+ c = fis1[i].compareTo(fis2[i]);
+ if (c != 0) return c;
+ }
+
+ for (int i = 0; i < directSubtypes.size(); i++) {
+ c = this.directSubtypes.get(i).compareTo(t.directSubtypes.get(i));
+ if (c != 0) return c;
+ }
+
+ return 0;
+ }
+
boolean isPrimitiveArrayType() {
if (!isArray()) {
return false;