Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / util / TileKey.java
bloba4f678a5704ef14d2e098bfa8e98fe5b893e3b46
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.util;
9 import gov.nasa.worldwind.geom.Angle;
11 /**
12 * @author tag
13 * @version $Id: TileKey.java 2471 2007-07-31 21:50:57Z tgaskins $
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 = Logging.getMessage("TileKey.levelIsLessThanZero");
36 Logging.logger().severe(msg);
37 throw new IllegalArgumentException(msg);
39 if (row < 0)
41 String msg = Logging.getMessage("generic.RowIndexOutOfRange", row);
42 Logging.logger().severe(msg);
43 throw new IllegalArgumentException(msg);
45 if (col < 0)
47 String msg = Logging.getMessage("generic.ColumnIndexOutOfRange", col);
48 Logging.logger().severe(msg);
49 throw new IllegalArgumentException(msg);
51 if (cacheName == null || cacheName.length() < 1)
53 String msg = Logging.getMessage("TileKey.cacheNameIsNullOrEmpty");
54 Logging.logger().severe(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 = Logging.getMessage("nullValue.AngleIsNull");
75 Logging.logger().severe(msg);
76 throw new IllegalArgumentException(msg);
78 if (level == null)
80 String msg = Logging.getMessage("nullValue.LevelIsNull");
81 Logging.logger().severe(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(Tile tile)
97 if (tile == null)
99 String msg = Logging.getMessage("nullValue.TileIsNull");
100 Logging.logger().severe(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 = Logging.getMessage("nullValue.KeyIsNull");
150 Logging.logger().severe(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 //noinspection SimplifiableIfStatement
189 if (this.row != tileKey.row)
190 return false;
192 return !(this.cacheName != null ? !this.cacheName.equals(tileKey.cacheName) : tileKey.cacheName != null);
195 @Override
196 public int hashCode()
198 return this.hash;
201 @Override
202 public String toString()
204 return this.cacheName + "/" + this.level + "/" + this.row + "/" + col;