Compare two generic parameters : Generic Parameter « Generics « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Generics » Generic ParameterScreenshots 
Compare two generic parameters
       
//package com.webex.ta.hydra.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-10-20
 * Time: 15:18:50
 */
public class Comparing {
    private Comparing() {
    }

    public static <T> boolean equal(T arg1, T arg2) {
        if (arg1 == null || arg2 == null) {
            return arg1 == arg2;
        else if (arg1 instanceof Object[] && arg2 instanceof Object[]) {
            Object[] arr1 = (Object[]) arg1;
            Object[] arr2 = (Object[]) arg2;
            return Arrays.equals(arr1, arr2);
        else if (arg1 instanceof String && arg2 instanceof String) {
            return equal((Stringarg1, (Stringarg2, true);
        else {
            return arg1.equals(arg2);
        }
    }

    public static <T> boolean equal(T[] arr1, T[] arr2) {
        if (arr1 == null || arr2 == null) {
            return arr1 == arr2;
        }
        return Arrays.equals(arr1, arr2);
    }
    

    public static boolean equal(String arg1, String arg2) {
        return equal(arg1, arg2, true);
    }


    public static boolean equal(String arg1, String arg2, boolean caseSensitive) {
        if (arg1 == null || arg2 == null) {
            return arg1 == arg2;
        else {
            return caseSensitive ? arg1.equals(arg2: arg1.equalsIgnoreCase(arg2);
        }
    }

    public static boolean strEqual(String arg1, String arg2) {
        return strEqual(arg1, arg2, true);
    }

    public static boolean strEqual(String arg1, String arg2, boolean caseSensitive) {
        return equal(arg1 == null "" : arg1, arg2 == null "" : arg2, caseSensitive);
    }

    public static <T> boolean haveEqualElements(Collection<T> a, Collection<T> b) {
        if (a.size() != b.size()) {
            return false;
        }

        Set<T> aSet = new HashSet<T>(a);
        for (T t : b) {
            if (!aSet.contains(t)) {
                return false;
            }
        }
        return true;
    }

    public static <T> boolean haveEqualElements(T[] a, T[] b) {
        if (a == null || b == null) {
            return a == b;
        }

        if (a.length != b.length) {
            return false;
        }

        Set<T> aSet = new HashSet<T>(Arrays.asList(a));
        for (T t : b) {
            if (!aSet.contains(t)) {
                return false;
            }
        }
        return true;
    }

    public static int hashcode(Object obj) {
        return obj == null : obj.hashCode();
    }

    public static int hashcode(Object obj1, Object obj2) {
        return hashcode(obj1^ hashcode(obj2);
    }

    public static int compare(int name1, int name2) {
        return name1 < name2 ? -: name1 == name2 ? 1;
    }

    public static <T extends Comparable<T>> int compare(final T name1, final T name2) {
        if (name1 == nullreturn name2 == null : -1;
        if (name2 == nullreturn 1;
        return name1.compareTo(name2);
    }
}

   
    
    
    
    
    
    
  
Related examples in the same category
1.A simple generic class with two type parameters: T and V.A simple generic class with two type parameters: T and V.
2.Java generic: Hierarchy argumentJava generic: Hierarchy argument
3.Boxing Generic Example
4.Demonstrate a raw generic type. Demonstrate a raw generic type.
5.T is a type parameter that will be replaced by a real type when an object of type Gen is created.
6.Create a generic class that can compute the average of an array of numbers of any given type.
7.the type argument for T must be either Number, or a class derived from Number.
8.Demonstrate a raw type.
9.A subclass can add its own type parameters.
10.Default implementation of {@link java.lang.reflect.ParameterizedType}
11.Get the Generic definition from a class for given class with given index.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.