1 /*
2  * Copyright (C) 2012 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.keyboard.tools;
18 
19 import java.io.PrintStream;
20 
21 public class ArrayInitializerFormatter {
22     private final PrintStream mOut;
23     private final int mMaxWidth;
24     private final String mIndent;
25     // String resource names array; indexed by {@link #CurrentIndex} and
26     // {@link #mStartIndexOfBuffer}.
27     private final String[] mResourceNames;
28 
29     private int mCurrentIndex = 0;
30     private String mLastElement;
31     private final StringBuilder mBuffer = new StringBuilder();
32     private int mBufferedLen;
33     private int mStartIndexOfBuffer = Integer.MIN_VALUE;
34 
ArrayInitializerFormatter(final PrintStream out, final int width, final String indent, final String[] resourceNames)35     public ArrayInitializerFormatter(final PrintStream out, final int width, final String indent,
36             final String[] resourceNames) {
37         mOut = out;
38         mMaxWidth = width - indent.length();
39         mIndent = indent;
40         mResourceNames = resourceNames;
41     }
42 
getCurrentIndex()43     public int getCurrentIndex() {
44         return mCurrentIndex;
45     }
46 
flush()47     public void flush() {
48         if (mBuffer.length() == 0) {
49             return;
50         }
51         final int lastIndex = mCurrentIndex - 1;
52         if (mStartIndexOfBuffer == lastIndex) {
53             mOut.format("%s/* %s */ %s\n",
54                     mIndent, mResourceNames[mStartIndexOfBuffer], mBuffer);
55         } else if (mStartIndexOfBuffer == lastIndex - 1) {
56             final String startElement = mBuffer.toString()
57                     .substring(0, mBuffer.length() - mLastElement.length())
58                     .trim();
59             mOut.format("%s/* %s */ %s\n"
60                     + "%s/* %s */ %s\n",
61                     mIndent, mResourceNames[mStartIndexOfBuffer], startElement,
62                     mIndent, mResourceNames[lastIndex], mLastElement);
63         } else {
64             mOut.format("%s/* %s ~ */\n"
65                     + "%s%s\n"
66                     + "%s/* ~ %s */\n",
67                     mIndent, mResourceNames[mStartIndexOfBuffer],
68                     mIndent, mBuffer,
69                     mIndent, mResourceNames[lastIndex]);
70         }
71         mBuffer.setLength(0);
72         mBufferedLen = 0;
73     }
74 
outCommentLines(final String lines)75     public void outCommentLines(final String lines) {
76         flush();
77         mOut.print(lines);
78         mLastElement = null;
79     }
80 
outElement(final String element)81     public void outElement(final String element) {
82         if (!element.equals(mLastElement)) {
83             flush();
84             mStartIndexOfBuffer = mCurrentIndex;
85         }
86         final int nextLen = mBufferedLen + " ".length() + element.length();
87         if (mBufferedLen != 0 && nextLen < mMaxWidth) {
88             // Element can fit in the current line.
89             mBuffer.append(' ');
90             mBuffer.append(element);
91             mBufferedLen = nextLen;
92         } else {
93             // Element should be on the next line.
94             if (mBufferedLen != 0) {
95                 mBuffer.append('\n');
96                 mBuffer.append(mIndent);
97             }
98             mBuffer.append(element);
99             mBufferedLen = element.length();
100         }
101         mCurrentIndex++;
102         mLastElement = element;
103     }
104 }
105