1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.asmtools.jdis; 24 25 public class Indenter { 26 27 private int indentLength; 28 29 public Indenter(int indentLength) { 30 this.indentLength = indentLength; 31 } 32 33 public Indenter() { 34 this.indentLength = Options.BODY_INDENT; 35 } 36 /** 37 * Returns current indentation length. 38 * 39 * @return current indentation length. 40 */ 41 public int indent() { 42 return indentLength; 43 } 44 45 /** 46 * Increases indentation length. 47 * 48 * @param indentLength new indent length 49 * 50 * @throws IllegalArgumentException if indentLength is negative. 51 */ 52 public Indenter setIndent(int indentLength) { 53 if (indentLength < 0) { 54 throw new IllegalArgumentException("indent length can't be negative"); 55 } 56 this.indentLength = indentLength; 57 return this; 58 } 59 60 /** 61 * Increases indentation length. 62 * 63 * @param increase length to increase by. 64 * 65 * @throws IllegalArgumentException if increase is negative. 66 */ 67 public Indenter increaseIndent(int increase) { 68 if (increase < 0) { 69 throw new IllegalArgumentException("indent length can't be negative"); 70 } 71 setIndent(indent() + increase); 72 return this; 73 } 74 75 /** 76 * Decreases indentation length. 77 * 78 * @param decrease length to decrease by 79 * 80 * @throws IllegalArgumentException if decrease is negative, or if decrease is greater than 81 * {@link #indent() current indentation length}. 82 */ 83 public Indenter decreaseIndent(int decrease) { 84 if (decrease < 0) { 85 throw new IllegalArgumentException("decrease can't be negative"); 86 } 87 setIndent(indent() - decrease); 88 return this; 89 } 90 91 /** 92 * Creates indent string based on current indent size. 93 */ 94 public String getIndentString() { 95 StringBuilder sb = new StringBuilder(); 96 for (int i = 0; i < indent(); i++) { 97 sb.append(' '); 98 } 99 return sb.toString(); 100 } 101 }