001    package biweekly.io.text;
002    
003    /*
004     Copyright (c) 2013, Michael Angstadt
005     All rights reserved.
006    
007     Redistribution and use in source and binary forms, with or without
008     modification, are permitted provided that the following conditions are met: 
009    
010     1. Redistributions of source code must retain the above copyright notice, this
011     list of conditions and the following disclaimer. 
012     2. Redistributions in binary form must reproduce the above copyright notice,
013     this list of conditions and the following disclaimer in the documentation
014     and/or other materials provided with the distribution. 
015    
016     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
017     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
018     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
019     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
020     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
021     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
022     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
023     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
025     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026     */
027    
028    /**
029     * Specifies how an iCalendar object should be folded when written to a string.
030     * @author Michael Angstadt
031     */
032    public class FoldingScheme {
033            /**
034             * Folds lines according to RFC specification (75 characters not including
035             * CRLF, with 1 space as indentation).
036             */
037            public static final FoldingScheme DEFAULT = new FoldingScheme(75, " ");
038    
039            private final int lineLength;
040            private final String indent;
041    
042            /**
043             * @param lineLength the maximum number of characters that can exist on a
044             * line before needing to be folded (not including the newline)
045             * @param indent the string to use for indentation
046             * @throws IllegalArgumentException if the line length is <= 0, or if the
047             * line length is less than the length of the indentation string
048             */
049            public FoldingScheme(int lineLength, String indent) {
050                    if (lineLength <= 0) {
051                            throw new IllegalArgumentException("The line length must be greater than 0.");
052                    }
053                    if (indent.length() > lineLength) {
054                            throw new IllegalArgumentException("The line length must be greater than the length of the indentation string.");
055                    }
056                    this.lineLength = lineLength;
057                    this.indent = indent;
058            }
059    
060            /**
061             * Gets the maximum number of characters that can exist on a line before
062             * needing to be folded (not including the newline).
063             * @return the max line length
064             */
065            public int getLineLength() {
066                    return lineLength;
067            }
068    
069            /**
070             * Gets the string that is used to indent the folded line.
071             * @return the indent string
072             */
073            public String getIndent() {
074                    return indent;
075            }
076    }