]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryInfo.java
81c8f5d687aae2662534f0ebc000f68086292e2a
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryInfo.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary;
16
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 public class DictionaryInfo implements Serializable {
22
23     private static final long serialVersionUID = -6850863377577700388L;
24
25     public static final class IndexInfo implements Serializable {
26         private static final long serialVersionUID = 6524751236198309438L;
27
28         public static final int NUM_CSV_FIELDS = 3;
29
30         public final String shortName; // Often LangISO.
31         public final int allTokenCount;
32         public final int mainTokenCount;
33
34         public IndexInfo(String shortName, int allTokenCount, int mainTokenCount) {
35             this.shortName = shortName;
36             this.allTokenCount = allTokenCount;
37             this.mainTokenCount = mainTokenCount;
38         }
39
40         public StringBuilder append(StringBuilder result) {
41             result.append(shortName);
42             result.append("\t").append(allTokenCount);
43             result.append("\t").append(mainTokenCount);
44             return result;
45         }
46
47         public IndexInfo(final String[] fields, int i) {
48             shortName = fields[i++];
49             allTokenCount = Integer.parseInt(fields[i++]);
50             mainTokenCount = Integer.parseInt(fields[i++]);
51         }
52     }
53
54     // Stuff populated from the text file.
55     public String uncompressedFilename; // used as a key throughout the program.
56     public String downloadUrl;
57     public long uncompressedBytes;
58     public long zipBytes;
59     public long creationMillis;
60     public final ArrayList<IndexInfo> indexInfos = new ArrayList<IndexInfo>();
61     public String dictInfo;
62
63     public DictionaryInfo() {
64         // Blank object.
65     }
66
67     public boolean isValid() {
68         return indexInfos != null && !indexInfos.isEmpty();
69     }
70
71     public StringBuilder append(final StringBuilder result) {
72         result.append(uncompressedFilename);
73         result.append("\t").append(downloadUrl);
74         result.append("\t").append(creationMillis);
75         result.append("\t").append(uncompressedBytes);
76         result.append("\t").append(zipBytes);
77         result.append("\t").append(indexInfos.size());
78         for (final IndexInfo indexInfo : indexInfos) {
79             indexInfo.append(result.append("\t"));
80         }
81         result.append("\t").append(dictInfo.replace("\n", "\\\\n"));
82         return result;
83     }
84
85     public DictionaryInfo(final String line) {
86         final String[] fields = line.split("\t");
87         int i = 0;
88         uncompressedFilename = fields[i++];
89         downloadUrl = fields[i++];
90         creationMillis = Long.parseLong(fields[i++]);
91         uncompressedBytes = Long.parseLong(fields[i++]);
92         zipBytes = Long.parseLong(fields[i++]);
93         final int size = Integer.parseInt(fields[i++]);
94         indexInfos.ensureCapacity(size);
95         for (int j = 0; j < size; ++j) {
96             indexInfos.add(new IndexInfo(fields, i));
97             i += IndexInfo.NUM_CSV_FIELDS;
98         }
99         dictInfo = fields[i++].replace("\\\\n", "\n");
100     }
101
102     @Override
103     public String toString() {
104         return uncompressedFilename;
105     }
106
107 }