Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / TileKey.java
blob6983c4fcae2be645d4897fedc89e211bb1a3fd3c
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind;
9 import gov.nasa.worldwind.geom.*;
11 /**
12 * @author tag
13 * @version $Id: TileKey.java 511 2007-01-17 07:26:02Z ericdalgliesh $
15 public class TileKey implements Comparable<TileKey>
17 private final int level;
18 private final int row;
19 private final int col;
20 private final String cacheName;
21 private final int hash;
23 /**
24 * @param level
25 * @param row
26 * @param col
27 * @param cacheName
28 * @throws IllegalArgumentException if <code>level</code>, <code>row</code> or <code>column</code> is negative or if
29 * <code>cacheName</code> is null or empty
31 public TileKey(int level, int row, int col, String cacheName)
33 if (level < 0)
35 String msg = WorldWind.retrieveErrMsg("TileKey.levelIsLessThanZero");
36 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
37 throw new IllegalArgumentException(msg);
39 if (row < 0)
41 String msg = WorldWind.retrieveErrMsg("generic.rowIndexOutOfRange");
42 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
43 throw new IllegalArgumentException(msg);
45 if (col < 0)
47 String msg = WorldWind.retrieveErrMsg("generic.columnIndexOutOfRange");
48 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
49 throw new IllegalArgumentException(msg);
51 if (cacheName == null || cacheName.length() < 1)
53 String msg = WorldWind.retrieveErrMsg("TileKey.cacheNameIsNullOrEmpty");
54 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
55 throw new IllegalArgumentException(msg);
57 this.level = level;
58 this.row = row;
59 this.col = col;
60 this.cacheName = cacheName;
61 this.hash = this.computeHash();
64 /**
65 * @param latitude
66 * @param longitude
67 * @param level
68 * @throws IllegalArgumentException if any parameter is null
70 public TileKey(Angle latitude, Angle longitude, Level level)
72 if (latitude == null || longitude == null)
74 String msg = WorldWind.retrieveErrMsg("nullValue.AngleIsNull");
75 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
76 throw new IllegalArgumentException(msg);
78 if (level == null)
80 String msg = WorldWind.retrieveErrMsg("nullValue.LevelIsNull");
81 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
82 throw new IllegalArgumentException(msg);
84 this.level = level.getLevelNumber();
85 this.row = Tile.computeRow(level.getTileDelta().getLatitude(), latitude);
86 this.col = Tile.computeColumn(level.getTileDelta().getLongitude(), longitude);
87 this.cacheName = level.getCacheName();
88 this.hash = this.computeHash();
91 /**
92 * @param tile
93 * @throws IllegalArgumentException if <code>tile</code> is null
95 public TileKey(gov.nasa.worldwind.Tile tile)
97 if (tile == null)
99 String msg = WorldWind.retrieveErrMsg("nullValue.TileIsNull");
100 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
101 throw new IllegalArgumentException(msg);
103 this.level = tile.getLevelNumber();
104 this.row = tile.getRow();
105 this.col = tile.getColumn();
106 this.cacheName = tile.getCacheName();
107 this.hash = this.computeHash();
110 public int getLevelNumber()
112 return level;
115 public int getRow()
117 return row;
120 public int getColumn()
122 return col;
125 public String getCacheName()
127 return cacheName;
130 private int computeHash()
132 int result;
133 result = this.level;
134 result = 29 * result + this.row;
135 result = 29 * result + this.col;
136 result = 29 * result + (this.cacheName != null ? this.cacheName.hashCode() : 0);
137 return result;
141 * @param key
142 * @return
143 * @throws IllegalArgumentException if <code>key</code> is null
145 public final int compareTo(TileKey key)
147 if (key == null)
149 String msg = WorldWind.retrieveErrMsg("nullValue.KeyIsNull");
150 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
151 throw new IllegalArgumentException(msg);
154 // No need to compare Sectors because they are redundant with row and column
155 if (key.level == this.level && key.row == this.row && key.col == this.col)
156 return 0;
158 if (this.level < key.level) // Lower-res levels compare lower than higher-res
159 return -1;
160 if (this.level > key.level)
161 return 1;
163 if (this.row < key.row)
164 return -1;
165 if (this.row > key.row)
166 return 1;
168 if (this.col < key.col)
169 return -1;
171 return 1; // tile.col must be > this.col because equality was tested above
174 @Override
175 public boolean equals(Object o)
177 if (this == o)
178 return true;
179 if (o == null || getClass() != o.getClass())
180 return false;
182 final TileKey tileKey = (TileKey) o;
184 if (this.col != tileKey.col)
185 return false;
186 if (this.level != tileKey.level)
187 return false;
188 if (this.row != tileKey.row)
189 return false;
191 return !(this.cacheName != null ? !this.cacheName.equals(tileKey.cacheName) : tileKey.cacheName != null);
194 @Override
195 public int hashCode()
197 return this.hash;
200 @Override
201 public String toString()
203 return this.cacheName + "/" + this.level + "/" + this.row + "/" + col;