001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.record.compiler;
019    
020    import java.util.ArrayList;
021    
022    import org.apache.hadoop.classification.InterfaceAudience;
023    import org.apache.hadoop.classification.InterfaceStability;
024    
025    /**
026     * A wrapper around StringBuffer that automatically does indentation
027     * 
028     * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
029     */
030    @Deprecated
031    @InterfaceAudience.Public
032    @InterfaceStability.Stable
033    public class CodeBuffer {
034      
035      static private ArrayList<Character> startMarkers = new ArrayList<Character>();
036      static private ArrayList<Character> endMarkers = new ArrayList<Character>();
037      
038      static {
039        addMarkers('{', '}');
040        addMarkers('(', ')');
041      }
042      
043      static void addMarkers(char ch1, char ch2) {
044        startMarkers.add(ch1);
045        endMarkers.add(ch2);
046      }
047      
048      private int level = 0;
049      private int numSpaces = 2;
050      private boolean firstChar = true;
051      private StringBuffer sb;
052      
053      /** Creates a new instance of CodeBuffer */
054      CodeBuffer() {
055        this(2, "");
056      }
057      
058      CodeBuffer(String s) {
059        this(2, s);
060      }
061      
062      CodeBuffer(int numSpaces, String s) {
063        sb = new StringBuffer();
064        this.numSpaces = numSpaces;
065        this.append(s);
066      }
067      
068      void append(String s) {
069        int length = s.length();
070        for (int idx = 0; idx < length; idx++) {
071          char ch = s.charAt(idx);
072          append(ch);
073        }
074      }
075      
076      void append(char ch) {
077        if (endMarkers.contains(ch)) {
078          level--;
079        }
080        if (firstChar) {
081          for (int idx = 0; idx < level; idx++) {
082            for (int num = 0; num < numSpaces; num++) {
083              rawAppend(' ');
084            }
085          }
086        }
087        rawAppend(ch);
088        firstChar = false;
089        if (startMarkers.contains(ch)) {
090          level++;
091        }
092        if (ch == '\n') {
093          firstChar = true;
094        }
095      }
096    
097      private void rawAppend(char ch) {
098        sb.append(ch);
099      }
100      
101      @Override
102      public String toString() {
103        return sb.toString();
104      }
105    }