]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryInfo.java
0df04c43256d74a14b1c325a593598d6dcc7d75b
[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
20 public class DictionaryInfo implements Serializable {
21
22     private static final long serialVersionUID = -6850863377577700388L;
23
24     public static final class IndexInfo implements Serializable {
25         private static final long serialVersionUID = 6524751236198309438L;
26
27         static final int NUM_CSV_FIELDS = 3;
28
29         public final String shortName; // Often LangISO.
30         final int allTokenCount;
31         public final int mainTokenCount;
32
33         public IndexInfo(String shortName, int allTokenCount, int mainTokenCount) {
34             this.shortName = shortName;
35             this.allTokenCount = allTokenCount;
36             this.mainTokenCount = mainTokenCount;
37         }
38
39         void append(StringBuilder result) {
40             result.append(shortName);
41             result.append("\t").append(allTokenCount);
42             result.append("\t").append(mainTokenCount);
43         }
44
45         public IndexInfo(final String[] fields, int i) {
46             shortName = fields[i++];
47             allTokenCount = Integer.parseInt(fields[i++]);
48             mainTokenCount = Integer.parseInt(fields[i++]);
49         }
50     }
51
52     // Stuff populated from the text file.
53     public String uncompressedFilename; // used as a key throughout the program.
54     public String downloadUrl;
55     public long uncompressedBytes;
56     public long zipBytes;
57     public long creationMillis;
58     public final ArrayList<IndexInfo> indexInfos = new ArrayList<>();
59     public String dictInfo;
60
61     public DictionaryInfo() {
62         // Blank object.
63     }
64
65     public boolean isValid() {
66         return indexInfos != null && !indexInfos.isEmpty();
67     }
68
69     public StringBuilder append(final StringBuilder result) {
70         result.append(uncompressedFilename);
71         result.append("\t").append(downloadUrl);
72         result.append("\t").append(creationMillis);
73         result.append("\t").append(uncompressedBytes);
74         result.append("\t").append(zipBytes);
75         result.append("\t").append(indexInfos.size());
76         for (final IndexInfo indexInfo : indexInfos) {
77             indexInfo.append(result.append("\t"));
78         }
79         result.append("\t").append(dictInfo.replace("\n", "\\\\n"));
80         return result;
81     }
82
83     public DictionaryInfo(final String line) {
84         final String[] fields = line.split("\t");
85         int i = 0;
86         uncompressedFilename = fields[i++];
87         downloadUrl = fields[i++];
88         creationMillis = Long.parseLong(fields[i++]);
89         uncompressedBytes = Long.parseLong(fields[i++]);
90         zipBytes = Long.parseLong(fields[i++]);
91         final int size = Integer.parseInt(fields[i++]);
92         indexInfos.ensureCapacity(size);
93         for (int j = 0; j < size; ++j) {
94             indexInfos.add(new IndexInfo(fields, i));
95             i += IndexInfo.NUM_CSV_FIELDS;
96         }
97         dictInfo = fields[i++].replace("\\\\n", "\n");
98     }
99
100     @Override
101     public String toString() {
102         return uncompressedFilename;
103     }
104
105 }