]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/cache/TileSet.java
525696e518e8566289b90cb11691930fbef9e2fc
[GpsPrune.git] / tim / prune / function / cache / TileSet.java
1 package tim.prune.function.cache;
2
3 import java.io.File;
4
5
6 /**
7  * Class to hold information about a single tile set
8  * within the overall Tile Cache.
9  */
10 public class TileSet
11 {
12         /** Summary row info for whole tileset */
13         private RowInfo _rowInfo = null;
14         /** Path relative to mapcache root */
15         private String _path = null;
16         /** Comma-separated list of configs using this tileset */
17         private String _usedBy = null;
18
19
20         /**
21          * Constructor
22          * @param inDir directory of tileset
23          * @param inPath String describing relative path from cache root
24          * @param inUsedBy String describing which configs use this Tileset
25          */
26         public TileSet(File inDir, String inPath, String inUsedBy)
27         {
28                 _path = inPath;
29                 _usedBy = inUsedBy;
30                 _rowInfo = new RowInfo();
31                 // Go through zoom directories and construct row info objects
32                 if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead())
33                 {
34                         for (File subdir : inDir.listFiles())
35                         {
36                                 if (subdir != null && subdir.exists() && subdir.isDirectory()
37                                         && subdir.canRead() && isNumeric(subdir.getName()))
38                                 {
39                                         RowInfo row = makeRowInfo(subdir);
40                                         _rowInfo.addRow(row);
41                                 }
42                         }
43                 }
44         }
45
46         /**
47          * Check if a directory name is numeric
48          * @param inName name of directory
49          * @return true if it only contains characters 0-9
50          */
51         public static boolean isNumeric(String inName)
52         {
53                 if (inName == null || inName.equals("")) return false;
54                 for (int i=0; i<inName.length(); i++)
55                 {
56                         char a = inName.charAt(i);
57                         if (a < '0' || a > '9') return false;
58                 }
59                 return true;
60         }
61
62         /**
63          * Check if a filename is numeric up until the first dot
64          * This appears to be much faster than scanning for a . with indexOf, then
65          * chopping to make a new String, and then calling isNumeric to scan through again
66          * @param inName name of file
67          * @return true if it only contains characters 0-9 before the first dot
68          */
69         public static boolean isNumericUntilDot(String inName)
70         {
71                 if (inName == null || inName.equals("") || inName.charAt(0) == '.') {
72                         return false;
73                 }
74                 try
75                 {
76                         char c = '.';
77                         int i = 0;
78                         do
79                         {
80                                 c = inName.charAt(i);
81                                 if (c == '.') return true; // found the dot, so stop
82                                 if (c < '0' || c > '9') return false; // not numeric
83                                 i++;
84                         } while (c != '\0');
85                 }
86                 catch (IndexOutOfBoundsException iobe) {}
87                 // Didn't find a dot, so can't be a valid name
88                 return false;
89         }
90
91         /**
92          * Make a RowInfo object from the given directory
93          * @param inDir directory for a single zoom level
94          * @return RowInfo object describing files and size
95          */
96         private static RowInfo makeRowInfo(File inDir)
97         {
98                 RowInfo row = new RowInfo();
99                 row.setZoom(Integer.parseInt(inDir.getName()));
100                 for (File subdir : inDir.listFiles())
101                 {
102                         if (subdir != null && subdir.exists() && subdir.isDirectory()
103                                 && subdir.canRead() && isNumeric(subdir.getName()))
104                         {
105                                 // Found a directory of images (finally!)
106                                 for (File f : subdir.listFiles())
107                                 {
108                                         if (f != null && f.exists() && f.isFile() && f.canRead())
109                                         {
110                                                 if (isNumericUntilDot(f.getName())) {
111                                                         row.addTile(f.length());
112                                                 }
113                                         }
114                                 }
115                         }
116                 }
117                 return row;
118         }
119
120         /**
121          * @return row info object
122          */
123         public RowInfo getRowInfo() {
124                 return _rowInfo;
125         }
126
127         /** @return relative path to tileset */
128         public String getPath() {
129                 return _path;
130         }
131
132         /** @return users of tileset */
133         public String getUsedBy() {
134                 return _usedBy;
135         }
136 }