Make DeclarationInTextSource implement hashCode/equals 97/88997/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Apr 2020 13:59:55 +0000 (15:59 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 9 Apr 2020 20:12:55 +0000 (20:12 +0000)
In order to de-duplicate warnings, we will want to use
StatementSourceReference as a Set member, for which we need all
implementations to behave correctly w.r.t comparison.

JIRA: YANGTOOLS-1090
Change-Id: I18971e7681c02f56300a64cb08fda4bf4eef613a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/DeclarationInTextSource.java

index 3d340e0e69915ff786857a693927a81c31daca24..a8e7f0198d6249a2a1f31d05b87e02d0f910328d 100644 (file)
@@ -7,24 +7,23 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.source;
 
+import java.util.Objects;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
 
 /**
  * Reference of statement source present in textual source format. Utility implementation
- * of {@link StatementSourceReference} for textual sources, this is prefered {@link StatementSourceReference}
+ * of {@link StatementSourceReference} for textual sources, this is preferred {@link StatementSourceReference}
  * for implementations of YANG / YIN statement stream sources.
  *
  * <p>
  * To create source reference use one of this static factories:
  * <ul>
- * <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location,
- * this is most prefered since it provides most context to debug YANG model.
- * </li>
- * <li>{@link #atLine(String, int)}- provides source and line of statement location.
- * </li>
- * <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous
- * references are unable to create / derive from source.
- * </li>
+ *   <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location, this is most
+ *       preferred since it provides most context to debug YANG model.</li>
+ *   <li>{@link #atLine(String, int)}- provides source and line of statement location.</li>
+ *   <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous references are
+ *       unable to create / derive from source.</li>
  * </ul>
  */
 public abstract class DeclarationInTextSource implements StatementSourceReference {
@@ -32,11 +31,6 @@ public abstract class DeclarationInTextSource implements StatementSourceReferenc
         InSource(final String sourceName) {
             super(sourceName);
         }
-
-        @Override
-        public String toString() {
-            return getSourceName();
-        }
     }
 
     private static class AtLine extends InSource {
@@ -48,12 +42,18 @@ public abstract class DeclarationInTextSource implements StatementSourceReferenc
         }
 
         @Override
-        public String toString() {
-            return getSourceName() + ':' + line;
+        int hashCodeImpl() {
+            return super.hashCodeImpl() * 31 + line;
+        }
+
+        @Override
+        boolean equalsImpl(final DeclarationInTextSource obj) {
+            return line == ((AtLine) obj).line && super.equalsImpl(obj);
         }
 
-        int getLine() {
-            return line;
+        @Override
+        public String toString() {
+            return super.toString() + ':' + line;
         }
     }
 
@@ -65,9 +65,19 @@ public abstract class DeclarationInTextSource implements StatementSourceReferenc
             this.character = character;
         }
 
+        @Override
+        int hashCodeImpl() {
+            return super.hashCodeImpl() * 31 + character;
+        }
+
+        @Override
+        boolean equalsImpl(final DeclarationInTextSource obj) {
+            return character == ((AtPosition) obj).character && super.equalsImpl(obj);
+        }
+
         @Override
         public String toString() {
-            return getSourceName() + ':' + getLine() + ':' + character;
+            return super.toString() + ':' + character;
         }
     }
 
@@ -77,27 +87,49 @@ public abstract class DeclarationInTextSource implements StatementSourceReferenc
         this.sourceName = sourceName;
     }
 
-    public static DeclarationInTextSource inSource(final String sourceName) {
+    public static @NonNull DeclarationInTextSource inSource(final String sourceName) {
         return new InSource(sourceName);
     }
 
-    public static DeclarationInTextSource atLine(final String sourceName, final int line) {
+    public static @NonNull DeclarationInTextSource atLine(final String sourceName, final int line) {
         return new AtLine(sourceName, line);
     }
 
-    public static DeclarationInTextSource atPosition(final String sourceName, final int line, final int position) {
+    public static @NonNull DeclarationInTextSource atPosition(final String sourceName, final int line,
+            final int position) {
         return new AtPosition(sourceName, line, position);
     }
 
-    public String getSourceName() {
+    public final String getSourceName() {
         return sourceName;
     }
 
     @Override
-    public StatementSource getStatementSource() {
+    public final StatementSource getStatementSource() {
         return StatementSource.DECLARATION;
     }
 
     @Override
-    public abstract String toString();
+    public final int hashCode() {
+        return hashCodeImpl();
+    }
+
+    @Override
+    public final boolean equals(final Object obj) {
+        return this == obj
+                || obj != null && getClass().equals(obj.getClass()) && equalsImpl((DeclarationInTextSource) obj);
+    }
+
+    @Override
+    public String toString() {
+        return sourceName == null ? "null" : sourceName;
+    }
+
+    int hashCodeImpl() {
+        return Objects.hashCode(sourceName);
+    }
+
+    boolean equalsImpl(final DeclarationInTextSource obj) {
+        return Objects.equals(sourceName, obj.sourceName);
+    }
 }