Example usage for java.lang Object equals

List of usage examples for java.lang Object equals

Introduction

In this page you can find the example usage for java.lang Object equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:ObjectUtils.java

/**
 * Determine if the given objects are equal, returning <code>true</code>
 * if both are <code>null</code> or <code>false</code> if only one is
 * <code>null</code>.// w  ww.j  a  v a2s .c o m
 * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
 * check based on the array elements rather than the array reference.
 * @param o1 first Object to compare
 * @param o2 second Object to compare
 * @return whether the given objects are equal
 * @see java.util.Arrays#equals
 */
public static boolean nullSafeEquals(Object o1, Object o2) {
    if (o1 == o2) {
        return true;
    }
    if (o1 == null || o2 == null) {
        return false;
    }
    if (o1.equals(o2)) {
        return true;
    }
    if (o1 instanceof Object[] && o2 instanceof Object[]) {
        return Arrays.equals((Object[]) o1, (Object[]) o2);
    }
    if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
        return Arrays.equals((boolean[]) o1, (boolean[]) o2);
    }
    if (o1 instanceof byte[] && o2 instanceof byte[]) {
        return Arrays.equals((byte[]) o1, (byte[]) o2);
    }
    if (o1 instanceof char[] && o2 instanceof char[]) {
        return Arrays.equals((char[]) o1, (char[]) o2);
    }
    if (o1 instanceof double[] && o2 instanceof double[]) {
        return Arrays.equals((double[]) o1, (double[]) o2);
    }
    if (o1 instanceof float[] && o2 instanceof float[]) {
        return Arrays.equals((float[]) o1, (float[]) o2);
    }
    if (o1 instanceof int[] && o2 instanceof int[]) {
        return Arrays.equals((int[]) o1, (int[]) o2);
    }
    if (o1 instanceof long[] && o2 instanceof long[]) {
        return Arrays.equals((long[]) o1, (long[]) o2);
    }
    if (o1 instanceof short[] && o2 instanceof short[]) {
        return Arrays.equals((short[]) o1, (short[]) o2);
    }
    return false;
}

From source file:com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.Util.java

public static boolean compare(Object o1, Object o2) {
    if (o1 == null && o2 == null) {
        return true;
    }/*  w ww.ja  va  2s .  c o  m*/

    if (o1 == null || o2 == null) {
        return false;
    }

    return o1.equals(o2);
}

From source file:org.kiji.rest.representations.KijiRestEntityId.java

/**
 * Create KijiRestEntityId from json node.
 *
 * @param node of the RKF2-formatted, materialization unsuppressed row.
 * @param rowKeyFormat2 of the layout or null if the layout has RowKeyFormat1.
 *        If null, then long components may not be recognized.
 * @return a properly constructed KijiRestEntityId.
 * @throws IOException if KijiRestEntityId can not be properly constructed.
 *//*  w  w  w .j a va 2s  .  c  o m*/
public static KijiRestEntityId create(final JsonNode node, final RowKeyFormat2 rowKeyFormat2)
        throws IOException {
    Preconditions.checkNotNull(node);
    if (node.isArray()) {
        final Object[] components = new Object[node.size()];
        boolean wildCarded = false;
        for (int i = 0; i < node.size(); i++) {
            final Object component = getNodeValue(node.get(i));
            if (component.equals(WildcardSingleton.INSTANCE)) {
                wildCarded = true;
                components[i] = null;
            } else if (null != rowKeyFormat2
                    && ComponentType.LONG == rowKeyFormat2.getComponents().get(i).getType()) {
                components[i] = ((Number) component).longValue();
            } else {
                components[i] = component;
            }
        }
        return new KijiRestEntityId(components, wildCarded);
    } else {
        // Disallow non-arrays.
        throw new IllegalArgumentException(
                "Provide components wrapped as a JSON array or provide the row key.");
    }
}

From source file:com.simas.vc.helpers.Utils.java

/**
 * Compares 2 {@code Object} references.
 * @return false if the objects differ or either of them is {@code null}, otherwise true
 *///from w  w  w . ja va 2 s.  com
public static boolean equals(@Nullable Object obj1, @Nullable Object obj2) {
    if (obj1 == null || obj2 == null)
        return false;
    return obj1.equals(obj2);
}

From source file:com.net2plan.gui.utils.viewEditTopolTables.specificTables.AdvancedJTable_layer.java

private static TableModel createTableModel(final IVisualizationCallback networkViewer) {
    TableModel layerTableModel = new ClassAwareTableModel(new Object[1][netPlanViewTableHeader.length],
            netPlanViewTableHeader) {//from  w w w .j av a2 s .co m
        private static final long serialVersionUID = 1L;

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex >= netPlanViewTableHeader.length;
        }

        @Override
        public void setValueAt(Object newValue, int row, int column) {
            Object oldValue = getValueAt(row, column);
            /* If value doesn't change, exit from function */
            if (newValue != null && newValue.equals(oldValue))
                return;
            /* Set new value */
            super.setValueAt(newValue, row, column);
        }
    };

    return layerTableModel;
}

From source file:com.facebook.presto.operator.aggregation.AggregationTestUtils.java

public static void assertAggregation(InternalAggregationFunction function, Object expectedValue, Page page) {
    BiFunction<Object, Object, Boolean> equalAssertion;
    if (expectedValue instanceof Double && !expectedValue.equals(Double.NaN)) {
        equalAssertion = (actual, expected) -> Precision.equals((double) actual, (double) expected, 1e-10);
    } else if (expectedValue instanceof Float && !expectedValue.equals(Float.NaN)) {
        equalAssertion = (actual, expected) -> Precision.equals((float) actual, (float) expected, 1e-10f);
    } else {//from   ww  w . j a  v  a  2s  .  c om
        equalAssertion = Objects::equals;
    }

    assertAggregation(function, equalAssertion, null, page, expectedValue);
}

From source file:it.nicola_amatucci.util.Json.java

public static <T> T object_from_json(JSONObject json, Class<T> objClass) throws Exception {
    T t = null;//from ww w .j av  a2 s. c  o m
    Object o = null;

    try {
        //create new object instance
        t = objClass.newInstance();

        //object fields
        Field[] fields = objClass.getFields();

        for (Field field : fields) {
            //field name
            o = json.get(field.getName());

            if (o.equals(null))
                continue;

            //field value
            try {
                String typeName = field.getType().getSimpleName();

                if (typeName.equals("String")) {
                    o = json.getString(field.getName()); //String
                } else if (typeName.equals("boolean")) {
                    o = Integer.valueOf(json.getInt(field.getName())); //boolean
                } else if (typeName.equals("int")) {
                    o = Integer.valueOf(json.getInt(field.getName())); //int
                } else if (typeName.equals("long")) {
                    o = Long.valueOf(json.getLong(field.getName())); //long                  
                } else if (typeName.equals("double")) {
                    o = Double.valueOf(json.getDouble(field.getName())); //double
                } else if (typeName.equals("Date")) {
                    o = new SimpleDateFormat(Json.DATA_FORMAT).parse(o.toString()); //data
                } else if (field.getType().isArray()) {
                    JSONArray arrayJSON = new JSONArray(o.toString());
                    T[] arrayOfT = (T[]) null;

                    try {
                        //create object array
                        Class c = Class.forName(field.getType().getName()).getComponentType();
                        arrayOfT = (T[]) Array.newInstance(c, arrayJSON.length());

                        //parse objects                  
                        for (int i = 0; i < json.length(); i++)
                            arrayOfT[i] = (T) object_from_json(arrayJSON.getJSONObject(i), c);
                    } catch (Exception e) {
                        throw e;
                    }

                    o = arrayOfT;
                } else {
                    o = object_from_json(new JSONObject(o.toString()), field.getType()); //object
                }

            } catch (Exception e) {
                throw e;
            }

            t.getClass().getField(field.getName()).set(t, o);
        }

    } catch (Exception e) {
        throw e;
    }

    return t;
}

From source file:com.antsdb.saltedfish.util.UberUtil.java

public static boolean safeEqual(Object obj1, Object obj2) {
    if (obj1 == obj2) {
        return true;
    }//www  .  j  av a  2s. co  m
    if ((obj1 == null) || (obj2 == null)) {
        return false;
    }
    return obj1.equals(obj2);
}

From source file:com.jaspersoft.studio.components.chart.model.theme.util.PadUtil.java

public static RectangleInsets setPropertyValue(Object id, Object value, RectangleInsets ri, String preID) {
    if (ri == null)
        ri = PadUtil.RECTANGLE_INSETS;/*from  w  w w .  j  av a 2  s .  c o m*/
    if (value == null)
        value = 0.0d;
    if (id.equals(preID + PadUtil.PADDING_TOP))
        return new RectangleInsets((Double) value, ri.getLeft(), ri.getBottom(), ri.getRight());
    else if (id.equals(preID + PadUtil.PADDING_BOTTOM))
        return new RectangleInsets(ri.getTop(), ri.getLeft(), (Double) value, ri.getRight());
    else if (id.equals(preID + PadUtil.PADDING_LEFT))
        return new RectangleInsets(ri.getTop(), (Double) value, ri.getBottom(), ri.getRight());
    else if (id.equals(preID + PadUtil.PADDING_RIGHT))
        return new RectangleInsets(ri.getTop(), ri.getLeft(), ri.getBottom(), (Double) value);
    return null;
}

From source file:azkaban.utils.Utils.java

/**
 * Equivalent to Object.equals except that it handles nulls. If a and b are
 * both null, true is returned./*from   w  ww  . ja v a2  s .  c om*/
 *
 * @param a
 * @param b
 * @return
 */
public static boolean equals(Object a, Object b) {
    if (a == null || b == null) {
        return a == b;
    }

    return a.equals(b);
}