Guest User

lavieri

a guest
Mar 12th, 2009
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Tomaz Lavieri
  4.  */
  5. public class Point3D<T extends Number> {
  6.     public static final Point3D ZERO = valueOf(0,0,0);
  7.  
  8.     public static Point3D<Byte> valueOf(byte x, byte y, byte z) {
  9.         return new Point3D<Byte>(x, y, z);
  10.     }
  11.  
  12.     public static Point3D<Short> valueOf(short x, short y, short z) {
  13.         return new Point3D<Short>(x, y, z);
  14.     }
  15.  
  16.     public static Point3D<Integer> valueOf(int x, int y, int z) {
  17.         return new Point3D<Integer>(x, y, z);
  18.     }
  19.  
  20.     public static Point3D<Long> valueOf(long x, long y, long z) {
  21.         return new Point3D<Long>(x, y, z);
  22.     }
  23.  
  24.     public static Point3D<Float> valueOf(float x, float y, float z) {
  25.         return new Point3D<Float>(x, y, z);
  26.     }
  27.  
  28.     public static Point3D<Double> valueOf(double x, double y, double z) {
  29.         return new Point3D<Double>(x, y, z);
  30.     }
  31.    
  32.  
  33.     private final T x;
  34.     private final T y;
  35.     private final T z;
  36.  
  37.     private Point3D(T x, T y, T z) {
  38.         this.x = x;
  39.         this.y = y;
  40.         this.z = z;
  41.     }
  42.  
  43.     public T getX() {
  44.         return x;
  45.     }
  46.  
  47.     public T getY() {
  48.         return y;
  49.     }
  50.  
  51.     public T getZ() {
  52.         return z;
  53.     }
  54.  
  55.     @Override
  56.     public boolean equals(final Object other) {
  57.         if (other == null)
  58.             return false;
  59.        
  60.         return other instanceof Point3D && equals((Point3D) other);
  61.     }
  62.  
  63.     public boolean equals(final Point3D other) {
  64.         if (!this.x.equals(other.x)) {
  65.             return false;
  66.         }
  67.         if (!this.y.equals(other.y)) {
  68.             return false;
  69.         }
  70.         if (!this.z.equals(other.z)) {
  71.             return false;
  72.         }
  73.         return true;
  74.     }
  75.  
  76.     @Override
  77.     public int hashCode() {
  78.         int hash = 5;
  79.         hash = -7 * hash + x.intValue();
  80.         hash = 2 * hash + y.intValue();
  81.         hash = 3 * hash + z.intValue();
  82.         return hash;
  83.     }
  84.  
  85.     @Override
  86.     public String toString() {
  87.         return "[" + x + " , " + y + " , " + z + "]";
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment