jdk/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/internal/Columns.java
changeset 35377 462f93ab37f2
equal deleted inserted replaced
35361:4a652e4ca952 35377:462f93ab37f2
       
     1 /*
       
     2  * Copyright (c) 2015, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * This file is available under and governed by the GNU General Public
       
    28  * License version 2 only, as published by the Free Software Foundation.
       
    29  * However, the following notice accompanied the original version of this
       
    30  * file:
       
    31  *
       
    32  * The MIT License
       
    33  *
       
    34  * Copyright (c) 2004-2014 Paul R. Holser, Jr.
       
    35  *
       
    36  * Permission is hereby granted, free of charge, to any person obtaining
       
    37  * a copy of this software and associated documentation files (the
       
    38  * "Software"), to deal in the Software without restriction, including
       
    39  * without limitation the rights to use, copy, modify, merge, publish,
       
    40  * distribute, sublicense, and/or sell copies of the Software, and to
       
    41  * permit persons to whom the Software is furnished to do so, subject to
       
    42  * the following conditions:
       
    43  *
       
    44  * The above copyright notice and this permission notice shall be
       
    45  * included in all copies or substantial portions of the Software.
       
    46  *
       
    47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    48  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    49  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
       
    50  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
       
    51  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    52  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
       
    53  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    54  */
       
    55 
       
    56 package jdk.internal.joptsimple.internal;
       
    57 
       
    58 import java.text.BreakIterator;
       
    59 import java.util.ArrayList;
       
    60 import java.util.List;
       
    61 import java.util.Locale;
       
    62 
       
    63 import static java.text.BreakIterator.*;
       
    64 
       
    65 import static jdk.internal.joptsimple.internal.Strings.*;
       
    66 
       
    67 /**
       
    68  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
       
    69  */
       
    70 class Columns {
       
    71     private static final int INDENT_WIDTH = 2;
       
    72 
       
    73     private final int optionWidth;
       
    74     private final int descriptionWidth;
       
    75 
       
    76     Columns( int optionWidth, int descriptionWidth ) {
       
    77         this.optionWidth = optionWidth;
       
    78         this.descriptionWidth = descriptionWidth;
       
    79     }
       
    80 
       
    81     List<Row> fit( Row row ) {
       
    82         List<String> options = piecesOf( row.option, optionWidth );
       
    83         List<String> descriptions = piecesOf( row.description, descriptionWidth );
       
    84 
       
    85         List<Row> rows = new ArrayList<Row>();
       
    86         for ( int i = 0; i < Math.max( options.size(), descriptions.size() ); ++i )
       
    87             rows.add( new Row( itemOrEmpty( options, i ), itemOrEmpty( descriptions, i ) ) );
       
    88 
       
    89         return rows;
       
    90     }
       
    91 
       
    92     private static String itemOrEmpty( List<String> items, int index ) {
       
    93         return index >= items.size() ? "" : items.get( index );
       
    94     }
       
    95 
       
    96     private List<String> piecesOf( String raw, int width ) {
       
    97         List<String> pieces = new ArrayList<String>();
       
    98 
       
    99         for ( String each : raw.trim().split( LINE_SEPARATOR ) )
       
   100             pieces.addAll( piecesOfEmbeddedLine( each, width ) );
       
   101 
       
   102         return pieces;
       
   103     }
       
   104 
       
   105     private List<String> piecesOfEmbeddedLine( String line, int width ) {
       
   106         List<String> pieces = new ArrayList<String>();
       
   107 
       
   108         BreakIterator words = BreakIterator.getLineInstance( Locale.US );
       
   109         words.setText( line );
       
   110 
       
   111         StringBuilder nextPiece = new StringBuilder();
       
   112 
       
   113         int start = words.first();
       
   114         for ( int end = words.next(); end != DONE; start = end, end = words.next() )
       
   115             nextPiece = processNextWord( line, nextPiece, start, end, width, pieces );
       
   116 
       
   117         if ( nextPiece.length() > 0 )
       
   118             pieces.add( nextPiece.toString() );
       
   119 
       
   120         return pieces;
       
   121     }
       
   122 
       
   123     private StringBuilder processNextWord( String source, StringBuilder nextPiece, int start, int end, int width,
       
   124                                            List<String> pieces ) {
       
   125         StringBuilder augmented = nextPiece;
       
   126 
       
   127         String word = source.substring( start, end );
       
   128         if ( augmented.length() + word.length() > width ) {
       
   129             pieces.add( augmented.toString().replaceAll( "\\s+$", "" ) );
       
   130             augmented = new StringBuilder( repeat( ' ', INDENT_WIDTH ) ).append( word );
       
   131         }
       
   132         else
       
   133             augmented.append( word );
       
   134 
       
   135         return augmented;
       
   136     }
       
   137 }