Simple STL Pair Implementation : Generic Class « 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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Generics » Generic Class 




Simple STL Pair Implementation
       
//package org.hh.jga.util;

import java.io.Serializable;

/**
 * Simple STL Pair Implementation
 
 @author Hong Hong
 *
 @param <E1> the type of first element
 @param <E2> the type of second element
 */
public class Pair<E1, E2> implements Serializable {
  private static final long serialVersionUID = -312111942211427434L;
  public E1 first;
  public E2 second;
  
  /**
   * This is a comparable pair
   
   @author Hong Hong
   *
   @param <E1> the type of first element
   @param <E2> the type of second element
   */
  public static class CpPair<E1 extends Comparable<E1>, E2 extends Comparable<E2>> extends Pair<E1, E2> implements Comparable<CpPair<E1, E2>> {
    private static final long serialVersionUID = -6710234564965065727L;

    public int compareTo(CpPair<E1, E2> o) {
      int res = first != null ? first.compareTo(o.first(o.first != null ? -0);
      if(res == 0)
        res = second != null ? second.compareTo(o.second(o.second != null ? -0);
      return res;
    }
    public CpPair(E1 first, E2 second) {
      this.first = first;
      this.second = second;
    }
    public CpPair() {
    }
    public <E extends Comparable<E>> CpPair<E, CpPair<E1, E2>> frontAppend(E e) {
      return make_pair(e, this);
    }
  }
  public static <E1, E2> Pair<E1, E2> make_pair(E1 e1, E2 e2) {
    return new Pair<E1, E2>(e1, e2);
  }
  public static <E1 extends Comparable<E1>, E2 extends Comparable<E2>> CpPair<E1, E2> make_pair(E1 e1, E2 e2) {
    return new CpPair<E1, E2>(e1, e2);
  }

  public static <E1, E2, E3> Pair<E1, Pair<E2, E3>> make_chain(E1 e1, E2 e2, E3 e3) {
    return new Pair<E1, Pair<E2, E3>>(e1, new Pair<E2, E3>(e2, e3));
  }
  public static <E1 extends Comparable<E1>, E2 extends Comparable<E2>, E3 extends Comparable<E3>> 
  CpPair<E1, CpPair<E2, E3>> make_chain(E1 e1, E2 e2, E3 e3) {
    return make_pair(e1, make_pair(e2, e3));
  }
  
  public <E> Pair<E, Pair<E1, E2>> frontAppend(E e) {
    return make_pair(e, this);
  }
  
  
  public Pair(E1 first, E2 second) {
    this.first = first;
    this.second = second;
  }
  public Pair() {
  }
  
  public int hashCode() {
    return (first == null : first.hashCode())
      (second == null : second.hashCode());
  }
  
  @SuppressWarnings("unchecked")
  public boolean equals(Object other) {
    if(other.getClass().equals(getClass())) {
      Pair<E1, E2> o = (Pair<E1, E2>other;
      if(first == o.first || first != null && first.equals(o.first)) {
        if(second == o.second || second != null && second.equals(o.second))
          return true;
      }
    }
    return false;
  }
  
  public String toString() {
    return "Pair(" + first.toString() ", " + second.toString() ")";
  }
}

   
    
    
    
    
    
    
  














Related examples in the same category
1.A simple generic class. A simple generic class.
2.Demonstrate the non generic classDemonstrate the non generic class
3.Stats attempts (unsuccessfully) to create a generic class
4.A simple generic class hierarchy.A simple generic class hierarchy.
5.A nongeneric class can be the superclass of a generic subclass.A nongeneric class can be the superclass of a generic subclass.
6.Use the instanceof operator with a generic class hierarchy. Use the instanceof operator with a generic class hierarchy.
7.Java hierarchy generic classJava hierarchy generic class
8.Custom Generic Object TesterCustom Generic Object Tester
9.Pair of template arguments
10.Generic pair structure
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.