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:Main.java

public static Object containsClassAttributeInCollection(Collection in, String fieldName, Object value)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    Iterator<Object> it = in.iterator();
    boolean isBoolean = (value instanceof Boolean || value == boolean.class);
    String methodName = (isBoolean) ? "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) //$NON-NLS-1$
            : "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); //$NON-NLS-1$

    while (it.hasNext()) {
        Object obj = it.next();//from   w w w .j av a2s  .  co m
        Method m = obj.getClass().getMethod(methodName, new Class[] {});
        Object value2 = m.invoke(obj, null);
        if (value != null && value2 != null && value.equals(value2))
            return obj;
    }
    return null;
}

From source file:com.phoenixst.plexus.examples.RandomGraphFactory.java

/**
 *  Creates a random graph according to the Watts-Strogatz model.
 *  The number <code>d</code> here is half of <code>K</code> in
 *  the standard literature./*from  w  w  w  . jav a 2s  .  c  o m*/
 *
 *  <P>Start with a circulant graph.  Arrange the nodes in a
 *  circle, starting at 0 and increasing, in order, clockwise.
 *  Begin with node 0 and the edge which connects it to its
 *  nearest clockwise neighbor, which is node 1.  With probability
 *  <code>prob</code>, reconnect this edge from node 0 to a
 *  uniformly randomly selected node, with duplicate and self edges
 *  forbidden.  Repeat this process for each node, moving
 *  clockwise around the circle.  Now, repeat the entire cycle,
 *  but instead choose edges which connect nodes to their
 *  second-nearest clockwise neighbor.  And so on, until every one
 *  of the original edges has been considered.
 */
public static Graph createWattsStrogatz(int n, int d, double prob) {
    if (prob < 0.0 || prob > 1.0) {
        throw new IllegalArgumentException("Probability must be between 0.0 and 1.0, inclusive.");
    }
    Graph graph = new DefaultGraph(new CirculantGraph(n, d));
    Random random = new Random();
    for (int dist = 1; dist <= d; dist++) {
        for (int nodeIndex = 0; nodeIndex < n; nodeIndex++) {
            if (random.nextDouble() < prob) {
                Object tail = new Integer(nodeIndex);
                Object head = new Integer((nodeIndex + dist) % n);
                Predicate edgePred = EdgePredicateFactory.createEqualsNodes(tail, head,
                        GraphUtils.ANY_DIRECTION_MASK);
                graph.removeEdge(graph.getEdge(edgePred));
                while (true) {
                    head = new Integer(random.nextInt(n));
                    Predicate traverserPred = TraverserPredicateFactory.createEqualsNode(head,
                            GraphUtils.ANY_DIRECTION_MASK);
                    if (!tail.equals(head) && graph.getIncidentEdge(tail, traverserPred) == null) {
                        graph.addEdge(null, tail, head, true);
                        break;
                    }
                }
            }
        }
    }
    return graph;
}

From source file:jp.xet.baseunits.tests.SerializationTester.java

/**
 * ????????//from  ww w. j  av  a2  s  .c  om
 * 
 * @param serializable 
 * @throws AssertionError ????
 */
public static void assertCanBeSerialized(Object serializable) {
    if (Serializable.class.isInstance(serializable) == false) {
        fail("Object doesn't implement java.io.Serializable interface: " + serializable.getClass());
    }

    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    ByteArrayInputStream byteArrayIn = null;
    try {
        out = new ObjectOutputStream(byteArrayOut);
        out.writeObject(serializable);

        byteArrayIn = new ByteArrayInputStream(byteArrayOut.toByteArray());
        in = new ObjectInputStream(byteArrayIn);
        Object deserialized = in.readObject();
        if (serializable.equals(deserialized) == false) {
            fail("Reconstituted object is expected to be equal to serialized");
        }
    } catch (IOException e) {
        fail(e.getMessage());
    } catch (ClassNotFoundException e) {
        fail(e.getMessage());
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:MultiMap.java

public static Object[] removeFromArray(Object[] array, Object item) {
    if (item == null || array == null)
        return array;
    for (int i = array.length; i-- > 0;) {
        if (item.equals(array[i])) {
            Class c = array == null ? item.getClass() : array.getClass().getComponentType();
            Object[] na = (Object[]) Array.newInstance(c, Array.getLength(array) - 1);
            if (i > 0)
                System.arraycopy(array, 0, na, 0, i);
            if (i + 1 < array.length)
                System.arraycopy(array, i + 1, na, i, array.length - (i + 1));
            return na;
        }//from  ww w.  j  a va 2  s.  c o  m
    }
    return array;
}

From source file:fr.openwide.nuxeo.utils.document.DocumentUtils.java

/**
 * Returns the properties that differ between two versions of a document.
 * - If a schema is missing from a model, all of its properties will be returned
 * - Only basic types and string arrays as supported. More complex properties will always be considered modified. 
 * /*from  w w  w .j  av  a2 s  .  c  om*/
 * @param m1
 * @param m2
 * @return A list of xpaths
 * @throws ClientException
 */
public static List<String> getDifferingProperties(DocumentModel m1, DocumentModel m2) throws ClientException {
    List<String> differingProperties = new ArrayList<String>();

    Set<String> schemas = new HashSet<String>();
    schemas.addAll(Arrays.asList(m1.getSchemas()));
    schemas.addAll(Arrays.asList(m2.getSchemas()));

    for (String schema : schemas) {
        if (m1.hasSchema(schema)) {
            if (m2.hasSchema(schema)) {
                Map<String, Object> p1 = m1.getProperties(schema);
                Map<String, Object> p2 = m2.getProperties(schema);
                for (Entry<String, Object> property : p1.entrySet()) {
                    boolean differs = false;
                    Object value1 = property.getValue();
                    Object value2 = p2.get(property.getKey());
                    if (value1 == null) {
                        differs = value2 != null;
                    } else {
                        if (value1 instanceof String[]) {
                            differs = !Arrays.equals((String[]) value1, (String[]) value2);
                        } else {
                            differs = !value1.equals(value2);
                        }
                    }
                    if (differs) {
                        differingProperties.add(property.getKey());
                    }
                }
            } else {
                differingProperties.addAll(m1.getProperties(schema).keySet());
            }
        } else {
            differingProperties.addAll(m2.getProperties(schema).keySet());
        }
    }

    return differingProperties;
}

From source file:com.xhsoft.framework.common.utils.ClassUtil.java

/**
 * @param a/*from   ww  w .  j  ava2s  .  co m*/
 * @param b
 * @return
 */
public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

From source file:MultiMap.java

public static boolean contains(Object list, Object item) {
    if (list == null)
        return false;

    if (list instanceof List)
        return ((List) list).contains(item);

    return list.equals(item);
}

From source file:com.silverpeas.util.CollectionUtil.java

/**
 * Extracting a property from elements that each has an other property with a given value
 * @param class property class to extract
 * @param collection element collection/*from w  w w. j a  v a 2  s .  c o  m*/
 * @param propertyNameToExtract name property to extract
 * @param propertyNameToCompare name value to compare
 * @param givenValueToCompare value to compare
 * @param nullValueExtracted null value extracted
 * @return a bean property collection
 */
@SuppressWarnings({ "unchecked" })
public static <T> Collection<T> extractFrom(final Class<T> aClass, final Collection<?> collection,
        final String propertyNameToExtract, final String propertyNameToCompare,
        final Object givenValueToCompare, final boolean nullValueExtracted) {
    Set<T> result = null;
    if (collection != null) {
        result = new HashSet<T>(collection.size());
        Object valueToExtract;
        Object valueToCompare;
        for (final Object element : collection) {
            valueToCompare = getPropertyAsObject(element, propertyNameToCompare);
            if ((givenValueToCompare == null && valueToCompare == null)
                    || (givenValueToCompare != null && givenValueToCompare.equals(valueToCompare))) {
                valueToExtract = getPropertyAsObject(element, propertyNameToExtract);
                if (valueToExtract != null || nullValueExtracted) {
                    result.add((T) valueToExtract);
                }
            }
        }
    }
    return result;
}

From source file:adalid.commons.util.ObjUtils.java

public static Boolean eq(Object x, Object y) {
    Integer i = compare(x, y);//from  w  ww.  ja v a  2s  .  c  o  m
    return (i != null) ? i == 0 : x != null && y != null && x.equals(y);
}

From source file:adalid.commons.util.ObjUtils.java

public static Boolean neq(Object x, Object y) {
    Integer i = compare(x, y);/*from w  ww  .jav a2 s . com*/
    return (i != null) ? i != 0 : x != null && y != null && !x.equals(y);
}