1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin; 18 19 import java.io.File; 20 import java.math.BigDecimal; 21 import java.util.Locale; 22 23 import javax.annotation.Nonnull; 24 import javax.annotation.Nullable; 25 26 public class DictionaryStats { 27 public static final int NOT_AN_ENTRY_COUNT = -1; 28 29 public final Locale mLocale; 30 public final String mDictType; 31 public final String mDictFileName; 32 public final long mDictFileSize; 33 public final int mContentVersion; 34 public final int mWordCount; 35 DictionaryStats( @onnull final Locale locale, @Nonnull final String dictType, @Nullable final String dictFileName, @Nullable final File dictFile, final int contentVersion)36 public DictionaryStats( 37 @Nonnull final Locale locale, 38 @Nonnull final String dictType, 39 @Nullable final String dictFileName, 40 @Nullable final File dictFile, 41 final int contentVersion) { 42 mLocale = locale; 43 mDictType = dictType; 44 mDictFileSize = (dictFile == null || !dictFile.exists()) ? 0 : dictFile.length(); 45 mDictFileName = dictFileName; 46 mContentVersion = contentVersion; 47 mWordCount = -1; 48 } 49 DictionaryStats( @onnull final Locale locale, @Nonnull final String dictType, final int wordCount)50 public DictionaryStats( 51 @Nonnull final Locale locale, 52 @Nonnull final String dictType, 53 final int wordCount) { 54 mLocale = locale; 55 mDictType = dictType; 56 mDictFileSize = wordCount; 57 mDictFileName = null; 58 mContentVersion = 0; 59 mWordCount = wordCount; 60 } 61 getFileSizeString()62 public String getFileSizeString() { 63 BigDecimal bytes = new BigDecimal(mDictFileSize); 64 BigDecimal kb = bytes.divide(new BigDecimal(1024), 2, BigDecimal.ROUND_HALF_UP); 65 if (kb.longValue() == 0) { 66 return bytes.toString() + " bytes"; 67 } 68 BigDecimal mb = kb.divide(new BigDecimal(1024), 2, BigDecimal.ROUND_HALF_UP); 69 if (mb.longValue() == 0) { 70 return kb.toString() + " kb"; 71 } 72 return mb.toString() + " Mb"; 73 } 74 75 @Override toString()76 public String toString() { 77 final StringBuilder builder = new StringBuilder(mDictType); 78 if (mDictType.equals(Dictionary.TYPE_MAIN)) { 79 builder.append(" ("); 80 builder.append(mContentVersion); 81 builder.append(")"); 82 } 83 builder.append(": "); 84 if (mWordCount > -1) { 85 builder.append(mWordCount); 86 builder.append(" words"); 87 } else { 88 builder.append(mDictFileName); 89 builder.append(" / "); 90 builder.append(getFileSizeString()); 91 } 92 return builder.toString(); 93 } 94 toString(final Iterable<DictionaryStats> stats)95 public static String toString(final Iterable<DictionaryStats> stats) { 96 final StringBuilder builder = new StringBuilder("LM Stats"); 97 for (DictionaryStats stat : stats) { 98 builder.append("\n "); 99 builder.append(stat.toString()); 100 } 101 return builder.toString(); 102 } 103 } 104