MATSIM
Coord.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CoordI.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.api.core.v01;
22 
23 import java.io.Serializable;
24 
35 public final class Coord implements Serializable {
36 
37  private static final long serialVersionUID = 1L;
38 
39  private double x;
40  private double y;
41  private double z = Double.NEGATIVE_INFINITY;
42 
52  public Coord() {
53  }
54 
55  public Coord(final double x, final double y) {
56  this.x = x;
57  this.y = y;
58  this.z = Double.NEGATIVE_INFINITY;
59  }
60 
61  public Coord( double[] coord ) {
62  this() ;
63  switch ( coord.length ) {
64  case 3:
65  z = coord[2] ;
66  case 2:
67  x = coord[0] ; y = coord[1] ;
68  break ;
69  default:
70  throw new RuntimeException( "double[] of wrong length; cannot be interpreted as coordinate ") ;
71  }
72  }
73 
74 
75  public Coord(final double x, final double y, final double z){
76  if(z == Double.NEGATIVE_INFINITY){
77  throw new IllegalArgumentException("Double.NEGATIVE_INFINITY is an invalid elevation. " +
78  "If you want to ignore elevation, use Coord(x, y) constructor instead.");
79  }
80  this.x = x;
81  this.y = y;
82  this.z = z;
83  }
84 
85 
86  public double getX() {
87  return this.x;
88  }
89 
90  public double getY() {
91  return this.y;
92  }
93 
94  public double getZ() throws IllegalStateException {
95  if ( !hasZ() ){
96  throw new IllegalStateException("Requesting elevation (z) without having first set it.");
97  }
98  return this.z;
99  }
100 
101  public boolean hasZ() {
102  return this.z != Double.NEGATIVE_INFINITY;
103  }
104 
105 
106  @Override
107  public boolean equals(final Object other) {
108  if (!(other instanceof Coord)) {
109  return false;
110  }
111  Coord o = (Coord)other;
112 
113  if( !hasZ() ){
114  /* this object is a 2D coordinate. */
115 
116  if ( o.hasZ() ) return false;
117 
118  /* both are 2D coordinates. */
119  return (this.x == o.getX()) && (this.y == o.getY());
120  }
121  else {
122  /* this object is 3D coordinate. */
123 
124  if ( !o.hasZ() ) return false;
125 
126  /* both objects are 3D coordinates. */
127  return
128  (this.x == o.getX()) &&
129  (this.y == o.getY()) &&
130  (this.z == o.getZ());
131  }
132  }
133 
134 
135  @Override
136  public int hashCode() {
137  // Implementation based on chapter 3 of Joshua Bloch's "Effective Java"
138  long xbits = Double.doubleToLongBits(this.x);
139  long ybits = Double.doubleToLongBits(this.y);
140  long zbits = Double.doubleToLongBits(this.z);
141  int result = (int) (xbits ^ (xbits >>> 32));
142  result = 31 * result + (int) (ybits ^ (ybits >>> 32));
143  result = 31 * result + (int) (zbits ^ (zbits >>> 32));
144  return result;
145  }
146 
147 
148  @Override
149  public final String toString() {
150  if( !hasZ() ){
151  return "[x=" + this.x + " | y=" + this.y + "]";
152  } else{
153  return "[x=" + this.x + " | y=" + this.y + " | z=" + this.z + "]";
154  }
155  }
156 }
Coord(double[] coord)
Definition: Coord.java:61
Coord(final double x, final double y)
Definition: Coord.java:55
final String toString()
Definition: Coord.java:149
Coord(final double x, final double y, final double z)
Definition: Coord.java:75
boolean equals(final Object other)
Definition: Coord.java:107
static final long serialVersionUID
Definition: Coord.java:37