Skip to content

Commit

Permalink
backport: fine-tuning of or-logic in printing
Browse files Browse the repository at this point in the history
see issue #661
  • Loading branch information
MenoData committed Jul 24, 2017
1 parent 9c9761a commit 16e2711
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -856,16 +856,17 @@ Set<ElementPosition> print(
}

RuntimeException re = null;
int printed = -1;

try {
step.print(formattable, buf, attributes, positions, quickPath);
printed = step.print(formattable, buf, attributes, positions, quickPath);
} catch (ChronoException ex) {
re = ex;
} catch (IllegalArgumentException ex) {
re = ex;
}

if (re != null) {
if (printed == -1) {
// Fehlerfall: nächsten oder-Block suchen
int section = step.getSection();
int last = index;
Expand Down Expand Up @@ -897,6 +898,8 @@ Set<ElementPosition> print(
positionStack.push(ep);
}
index = last;
} else if (re == null) {
throw new IllegalArgumentException("Not formattable: " + formattable);
} else {
throw new IllegalArgumentException("Not formattable: " + formattable, re);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private CustomizedProcessor(
//~ Methoden ----------------------------------------------------------

@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand All @@ -136,7 +136,7 @@ public void print(
if (this.passThroughZDT && (formattable instanceof ZonalDateTime) && (positions == null)) {
ChronoFormatter<?> cf = (ChronoFormatter<?>) this.printer;
cf.print(formattable, buffer, attributes, false);
return;
return Integer.MAX_VALUE; // unknown number of printed characters
}

V value = formattable.get(this.element);
Expand Down Expand Up @@ -174,6 +174,7 @@ public void print(
}

buffer.append(collector);
return collector.length();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private DecimalProcessor(
//~ Methoden ----------------------------------------------------------

@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand Down Expand Up @@ -217,6 +217,8 @@ public void print(
positions.add(new ElementPosition(this.element, start, start + printed));
}

return printed;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ interface FormatProcessor<V> {
* @param attributes control attributes including sectional attributes
* @param positions positions of elements in text (optional)
* @param quickPath hint for using quick path
* @return count of printed characters or {@code Integer.MAX_VALUE} if unknown or {@code -1} if not successful
* @throws IllegalArgumentException if the object is not formattable
* @throws ChronoException if the object does not contain the element in question
* @throws IOException if writing into buffer fails
*/
void print( // TODO: return count of printed characters???
int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand Down
27 changes: 17 additions & 10 deletions i18n/src/main/java/net/time4j/format/expert/FormatStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ private FormatStep(
* @param attributes non-sectional control attributes
* @param positions positions of elements in text (optional)
* @param quickPath hint for using quick path
* @return count of printed characters or {@code Integer.MAX_VALUE} if unknown or {@code -1} if not successful
* @throws IllegalArgumentException if the object is not formattable
* @throws ChronoException if the object does not contain the element in question
* @throws IOException if writing into buffer fails
* @since 3.15/4.12
*/
void print(
int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand All @@ -142,7 +143,7 @@ void print(
) throws IOException {

if (!this.isPrinting(formattable)) {
return;
return 0;
}

AttributeQuery aq = (quickPath ? this.fullAttrs : this.getQuery(attributes));
Expand All @@ -151,15 +152,13 @@ void print(
(this.padLeft == 0)
&& (this.padRight == 0)
) {
this.processor.print(
return this.processor.print(
formattable,
buffer,
aq,
positions,
quickPath
);

return;
}

StringBuilder collector;
Expand All @@ -174,11 +173,12 @@ void print(
collector = new StringBuilder();
}

if (
(buffer instanceof CharSequence)
&& (positions != null)
) {
offset = ((CharSequence) buffer).length();
if ((buffer instanceof CharSequence) && (positions != null)) {
offset = (
((collector == buffer)
&& ((this.processor instanceof CustomizedProcessor) || (this.processor instanceof StyleProcessor)))
? 0
: ((CharSequence) buffer).length());
posBuf = new LinkedHashSet<ElementPosition>();
}

Expand All @@ -202,20 +202,24 @@ void print(
throw new IllegalArgumentException(this.padExceeded());
}

int leftPadding = 0;

while (printed < this.padLeft) {
if (start == -1) {
buffer.append(padChar);
} else {
collector.insert(start, padChar);
}
printed++;
leftPadding++;
}

if (start == -1) {
buffer.append(collector);
}

if (offset != -1) {
offset += leftPadding;
for (ElementPosition ep : posBuf) {
positions.add(
new ElementPosition(
Expand All @@ -233,6 +237,7 @@ void print(
while (len < this.padRight) {
buffer.append(padChar);
len++;
printed++;
}
}
} else { // padRight > 0
Expand Down Expand Up @@ -260,6 +265,8 @@ void print(
}
}

return printed;

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2016 Meno Hochschild, <http://www.menodata.de/>
* Copyright © 2013-2017 Meno Hochschild, <http://www.menodata.de/>
* -----------------------------------------------------------------------
* This file (FractionProcessor.java) is part of project Time4J.
*
Expand Down Expand Up @@ -136,7 +136,7 @@ private FractionProcessor(
//~ Methoden ----------------------------------------------------------

@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand Down Expand Up @@ -230,6 +230,8 @@ public void print(
new ElementPosition(this.element, start + 1, start + printed));
}

return printed;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2016 Meno Hochschild, <http://www.menodata.de/>
* Copyright © 2013-2017 Meno Hochschild, <http://www.menodata.de/>
* -----------------------------------------------------------------------
* This file (IgnorableWhitespaceProcessor.java) is part of project Time4J.
*
Expand Down Expand Up @@ -45,7 +45,7 @@ enum IgnorableWhitespaceProcessor
//~ Methoden ----------------------------------------------------------

@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand All @@ -54,6 +54,7 @@ public void print(
) throws IOException {

buffer.append(' ');
return 1;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2016 Meno Hochschild, <http://www.menodata.de/>
* Copyright © 2013-2017 Meno Hochschild, <http://www.menodata.de/>
* -----------------------------------------------------------------------
* This file (LiteralProcessor.java) is part of project Time4J.
*
Expand Down Expand Up @@ -166,7 +166,7 @@ private LiteralProcessor(
//~ Methoden ----------------------------------------------------------

@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand All @@ -177,10 +177,13 @@ public void print(
if (this.attribute != null) {
char literal = attributes.get(this.attribute, null).charValue();
buffer.append(literal);
return 1;
} else if (this.multi == null) {
buffer.append(this.single);
return 1;
} else {
buffer.append(this.multi);
return this.multi.length();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private LocalizedGMTProcessor(
//~ Methoden ----------------------------------------------------------

@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand Down Expand Up @@ -268,6 +268,8 @@ public void print(
start + printed));
}

return printed;

}

@Override
Expand Down
14 changes: 8 additions & 6 deletions i18n/src/main/java/net/time4j/format/expert/LookupProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private LookupProcessor(
//~ Methoden ----------------------------------------------------------

@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand All @@ -126,13 +126,13 @@ public void print(
if (buffer instanceof CharSequence) {
CharSequence cs = (CharSequence) buffer;
int offset = cs.length();
this.print(formattable, buffer);

int printed = this.print(formattable, buffer);
if (positions != null) {
positions.add(new ElementPosition(this.element, offset, cs.length()));
}
return printed;
} else {
this.print(formattable, buffer);
return this.print(formattable, buffer);
}

}
Expand Down Expand Up @@ -285,13 +285,15 @@ public FormatProcessor<V> quickPath(

}

private void print(
private int print(
ChronoDisplay formattable,
Appendable buffer
) throws IOException {

V value = formattable.get(this.element);
buffer.append(this.getString(value));
String text = this.getString(value);
buffer.append(text);
return text.length();

}

Expand Down
10 changes: 5 additions & 5 deletions i18n/src/main/java/net/time4j/format/expert/NumberProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private NumberProcessor(

@SuppressWarnings("unchecked")
@Override
public void print(
public int print(
ChronoDisplay formattable,
Appendable buffer,
AttributeQuery attributes,
Expand Down Expand Up @@ -193,8 +193,7 @@ public void print(
int v = formattable.getInt((ChronoElement<Integer>) this.element);
if (v < 0) {
if (v == Integer.MIN_VALUE) {
throw new IllegalArgumentException(
"Format context \"" + formattable + "\" without element: " + this.element);
return -1;
} else {
throw new IllegalArgumentException(
"Negative value not allowed according to sign policy.");
Expand Down Expand Up @@ -245,8 +244,7 @@ public void print(
if (type == Integer.class) {
int v = formattable.getInt((ChronoElement<Integer>) this.element);
if (v == Integer.MIN_VALUE) {
throw new IllegalArgumentException(
"Format context \"" + formattable + "\" without element: " + this.element);
return -1;
}
negative = (v < 0);
x = Math.abs(v);
Expand Down Expand Up @@ -381,6 +379,8 @@ public void print(
positions.add(new ElementPosition(this.element, start, start + printed));
}

return printed;

}

@Override
Expand Down
Loading

0 comments on commit 16e2711

Please sign in to comment.