Merge "Integration of MDSAL into distribution"
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / match / MatchField.java
index 175a11718bff431f80e113da0da837ccadd47cc2..54be4c67189412a208dc5cfebb6fef2ff49f0301 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.controller.sal.match;
 
 import java.io.Serializable;
-
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -20,19 +19,18 @@ import org.slf4j.LoggerFactory;
 
 /**
  * Represents the generic matching field
- * 
+ *
  */
 
 @XmlRootElement
 @XmlAccessorType(XmlAccessType.NONE)
 public class MatchField implements Cloneable, Serializable {
     private static final long serialVersionUID = 1L;
-    private static final Logger logger = LoggerFactory
-            .getLogger(MatchField.class);
+    private static final Logger logger = LoggerFactory.getLogger(MatchField.class);
     private MatchType type; // the field we want to match
     private Object value; // the value of the field we want to match
     private Object mask; // the value of the mask we want to match on the
-                         // specified field
+    // specified field
     private transient boolean isValid;
 
     // To satisfy JAXB
@@ -42,7 +40,7 @@ public class MatchField implements Cloneable, Serializable {
 
     /**
      * Mask based match constructor
-     * 
+     *
      * @param type
      * @param value
      * @param mask
@@ -59,7 +57,7 @@ public class MatchField implements Cloneable, Serializable {
 
     /**
      * Full match constructor
-     * 
+     *
      * @param type
      * @param value
      */
@@ -73,7 +71,7 @@ public class MatchField implements Cloneable, Serializable {
 
     /**
      * Returns the value set for this match field
-     * 
+     *
      * @return
      */
     public Object getValue() {
@@ -87,7 +85,7 @@ public class MatchField implements Cloneable, Serializable {
 
     /**
      * Returns the type field this match field object is for
-     * 
+     *
      * @return
      */
     public MatchType getType() {
@@ -102,7 +100,7 @@ public class MatchField implements Cloneable, Serializable {
     /**
      * Returns the mask value set for this field match A null mask means this is
      * a full match
-     * 
+     *
      * @return
      */
     public Object getMask() {
@@ -116,7 +114,7 @@ public class MatchField implements Cloneable, Serializable {
 
     /**
      * Returns the bitmask set for this field match
-     * 
+     *
      * @return
      */
     public long getBitMask() {
@@ -125,7 +123,7 @@ public class MatchField implements Cloneable, Serializable {
 
     /**
      * Returns whether the field match configuration is valid or not
-     * 
+     *
      * @return
      */
     public boolean isValid() {
@@ -134,16 +132,11 @@ public class MatchField implements Cloneable, Serializable {
 
     private boolean checkValueType() {
         if (type.isCongruentType(value, mask) == false) {
-            String valueClass = (value == null) ? "null" : value.getClass()
-                    .getSimpleName();
-            String maskClass = (mask == null) ? "null" : mask.getClass()
-                    .getSimpleName();
-            String error = "Invalid match field's value or mask types.For field: "
-                    + type.id()
-                    + " Expected:"
-                    + type.dataType().getSimpleName()
-                    + " or equivalent,"
-                    + " Got:(" + valueClass + "," + maskClass + ")";
+            String valueClass = (value == null) ? "null" : value.getClass().getSimpleName();
+            String maskClass = (mask == null) ? "null" : mask.getClass().getSimpleName();
+            String error = "Invalid match field's value or mask types.For field: " + type.id() + " Expected:"
+                    + type.dataType().getSimpleName() + " or equivalent," + " Got:(" + valueClass + "," + maskClass
+                    + ")";
             throwException(error);
             return false;
         }
@@ -154,13 +147,9 @@ public class MatchField implements Cloneable, Serializable {
         if (type.isValid(value, mask) == false) {
             String maskString = (mask == null) ? "null" : ("0x" + Integer
                     .toHexString(Integer.parseInt(mask.toString())));
-            String error = "Invalid match field's value or mask assignement.For field: "
-                    + type.id()
-                    + " Expected: "
-                    + type.getRange()
-                    + ", Got:(0x"
-                    + Integer.toHexString(Integer.parseInt(value.toString()))
-                    + "," + maskString + ")";
+            String error = "Invalid match field's value or mask assignement.For field: " + type.id() + " Expected: "
+                    + type.getRange() + ", Got:(0x" + Integer.toHexString(Integer.parseInt(value.toString())) + ","
+                    + maskString + ")";
 
             throwException(error);
             return false;
@@ -188,24 +177,20 @@ public class MatchField implements Cloneable, Serializable {
                 }
             }
         } catch (CloneNotSupportedException e) {
-            e.printStackTrace();
+            logger.error("", e);
         }
         return cloned;
     }
 
     @Override
     public String toString() {
-        return type + "(" + getValueString() + "," + getMaskString() + ")";
+        return (mask == null) ? String.format("%s(%s)", getTypeString(), getValueString()) :
+            String.format("%s(%s,%s)", getTypeString(), getValueString(), getMaskString());
     }
 
     @Override
     public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ((mask == null) ? 0 : mask.hashCode());
-        result = prime * result + ((type == null) ? 0 : type.hashCode());
-        result = prime * result + ((value == null) ? 0 : value.hashCode());
-        return result;
+        return type.hashCode(value, mask);
     }
 
     @Override
@@ -223,7 +208,6 @@ public class MatchField implements Cloneable, Serializable {
         if (type != other.type) {
             return false;
         }
-        return (type.equalValues(this.value, other.value) && type.equalMasks(
-                this.mask, other.mask));
+        return type.equals(this.value, other.value, this.mask, other.mask);
     }
 }