Skip to content

Commit

Permalink
#98 aibolit.P21 (var in the middle) fixed in Directives
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 13, 2020
1 parent 1703cf2 commit 9bb599c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
22 changes: 1 addition & 21 deletions src/main/java/org/xembly/Directives.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@
)
public final class Directives implements Iterable<Directive> {

/**
* Right margin.
*/
private static final int MARGIN = 80;

/**
* List of directives.
*/
Expand Down Expand Up @@ -125,22 +120,7 @@ public Directives(final Iterable<Directive> dirs) {

@Override
public String toString() {
final StringBuilder text = new StringBuilder(0);
int width = 0;
int idx = 0;
for (final Directive dir : this.all) {
if (idx > 0 && width == 0) {
text.append('\n').append(idx).append(':');
}
final String txt = dir.toString();
text.append(txt).append(';');
width += txt.length();
if (width > Directives.MARGIN) {
width = 0;
}
++idx;
}
return text.toString().trim();
return new Print(this.all).toString();
}

@Override
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/org/xembly/Print.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2013-2020, xembly.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the xembly.org nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.xembly;

/**
* It prints directives in one line.
*
* @since 0.25
*/
final class Print {

/**
* Right margin.
*/
private static final int MARGIN = 80;

/**
* List of directives.
*/
private final Iterable<Directive> all;

/**
* Public ctor.
* @param dirs Directives
*/
Print(final Iterable<Directive> dirs) {
this.all = dirs;
}

@Override
public String toString() {
final StringBuilder text = new StringBuilder(0);
int width = 0;
int idx = 0;
for (final Directive dir : this.all) {
if (idx > 0 && width == 0) {
text.append('\n').append(idx).append(':');
}
final String txt = dir.toString();
text.append(txt).append(';');
width += txt.length();
if (width > Print.MARGIN) {
width = 0;
}
++idx;
}
return text.toString().trim();
}

}

0 comments on commit 9bb599c

Please sign in to comment.