|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Indenter.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.debug; | |
| 36 | ||
| 37 | import edu.rice.cs.plt.text.TextUtil; | |
| 38 | import edu.rice.cs.plt.lambda.Thunk; | |
| 39 | import edu.rice.cs.plt.lambda.LazyThunk; | |
| 40 | ||
| 41 | /** | |
| 42 | * <p>Manages a string of varying size to be used for indenting. The "indent string" is some token string | |
| 43 | * repeated a variable number of times. By default, the token string is {@code " "} (two spaces), but | |
| 44 | * a different string may be provided if needed. Initially, the indentation level (the number of repeats) | |
| 45 | * is set to {@code 0}; {@link #push} and {@link #pop} are used to adjust this value.</p> | |
| 46 | * | |
| 47 | * <p>If thread safety is required, alternative {@link #atomicPop} and {@link #atomicPush} methods are | |
| 48 | * provided as well. These use locking to prevent concurrent changes to the indentation level.</p> | |
| 49 | */ | |
| 50 | public class Indenter { | |
| 51 | ||
| 52 | private final String _token; | |
| 53 | private int _level; | |
| 54 | private Thunk<String> _stringFactory; | |
| 55 | ||
| 56 | /** Create an indenter with the default token string: {@code " "} (two spaces) */ | |
| 57 | 0 | public Indenter() { this(" "); } |
| 58 | ||
| 59 | /** Create an indenter with the specified number of spaces as its token string */ | |
| 60 | 0 | public Indenter(int spaces) { this(TextUtil.repeat(' ', spaces)); } |
| 61 | ||
| 62 | /** Create an indenter with the given string as its token string */ | |
| 63 | 0 | public Indenter(String token) { |
| 64 | 0 | _token = token; |
| 65 | 0 | _level = 0; |
| 66 | 0 | _stringFactory = makeThunk(); |
| 67 | } | |
| 68 | ||
| 69 | /** Increase the indentation level */ | |
| 70 | 0 | public void push() { _level++; _stringFactory = makeThunk(); } |
| 71 | ||
| 72 | /** Decrease the indentation level */ | |
| 73 | 0 | public void pop() { _level--; _stringFactory = makeThunk(); } |
| 74 | ||
| 75 | /** | |
| 76 | * Increase the indentation level atomically. Locking prevents other {@code atomicPush()} and | |
| 77 | * {@code atomicPop()} invocations from adjusting the level simultaneously. | |
| 78 | */ | |
| 79 | 0 | public void atomicPush() { adjust(1); _stringFactory = makeThunk(); } |
| 80 | ||
| 81 | /** | |
| 82 | * Decrease the indentation level atomically. Locking prevents other {@code atomicPush()} and | |
| 83 | * {@code atomicPop()} invocations from adjusting the level simultaneously. | |
| 84 | */ | |
| 85 | 0 | public void atomicPop() { adjust(-1); _stringFactory = makeThunk(); } |
| 86 | ||
| 87 | /** Atomically adjust {@code _level}. */ | |
| 88 | 0 | private synchronized void adjust(int delta) { _level += delta; } |
| 89 | ||
| 90 | /** | |
| 91 | * Produce a string based on the token string and current indentation level. If the level is {@code <= 0}, | |
| 92 | * this is the empty string. | |
| 93 | */ | |
| 94 | 0 | public String indentString() { return _stringFactory.value(); } |
| 95 | ||
| 96 | 0 | private Thunk<String> makeThunk() { |
| 97 | 0 | return LazyThunk.make(new Thunk<String>() { |
| 98 | 0 | public String value() { |
| 99 | 0 | if (_level <= 0) { return ""; } |
| 100 | 0 | else { return TextUtil.repeat(_token, _level); } |
| 101 | } | |
| 102 | }); | |
| 103 | } | |
| 104 | ||
| 105 | } |
|
||||||||||