Skip to content

Commit

Permalink
make Time4A more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
MenoData committed Apr 20, 2020
1 parent ff314d9 commit 038669c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
36 changes: 19 additions & 17 deletions time4j-android/src/main/java/net/time4j/format/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -662,19 +662,24 @@ public final class Attributes
public static final AttributeKey<String> FORMAT_PATTERN =
PredefinedKey.valueOf("FORMAT_PATTERN", String.class);

private static final Attributes EMPTY = new Attributes.Builder().build();
private static final Attributes EMPTY = new Attributes();

//~ Instanzvariablen --------------------------------------------------

private final Map<String, Object> attributes;

//~ Konstruktoren -----------------------------------------------------

private Attributes() {
super();

this.attributes = Collections.emptyMap();
}

private Attributes(Map<String, Object> map) {
super();

this.attributes = Collections.unmodifiableMap(new HashMap<String, Object>(map));

}

//~ Methoden ----------------------------------------------------------
Expand Down Expand Up @@ -818,7 +823,7 @@ public static final class Builder {

//~ Instanzvariablen ----------------------------------------------

private final Map<String, Object> attributes = new HashMap<String, Object>();
private final Map<String, Object> attrs = new HashMap<String, Object>();

//~ Konstruktoren -------------------------------------------------

Expand Down Expand Up @@ -987,7 +992,7 @@ public Builder set(
boolean value
) {

this.attributes.put(key.name(), Boolean.valueOf(value));
this.attrs.put(key.name(), Boolean.valueOf(value));
return this;

}
Expand All @@ -998,30 +1003,27 @@ public Builder set(
* @param key attribute key
* @param value attribute value
* @return this instance for method chaining
* @throws IllegalArgumentException if an invalid pivot year is given
* @throws IllegalArgumentException if the pivot year is smaller than {@code 100}
*/
/*[deutsch]
* <p>Setzt ein Formatattribut vom {@code int}-Typ. </p>
*
* @param key attribute key
* @param value attribute value
* @return this instance for method chaining
* @throws IllegalArgumentException if an invalid pivot year is given
* @throws IllegalArgumentException if the pivot year is smaller than {@code 100}
*/
public Builder set(
AttributeKey<Integer> key,
int value
) {

if (
(key == Attributes.PIVOT_YEAR)
&& (value < 100)
) {
if ((key == Attributes.PIVOT_YEAR) && (value < 100)) {
throw new IllegalArgumentException(
"Pivot year in far past not supported: " + value);
}

this.attributes.put(key.name(), Integer.valueOf(value));
this.attrs.put(key.name(), Integer.valueOf(value));
return this;

}
Expand All @@ -1045,7 +1047,7 @@ public Builder set(
char value
) {

this.attributes.put(key.name(), Character.valueOf(value));
this.attrs.put(key.name(), Character.valueOf(value));
return this;

}
Expand Down Expand Up @@ -1078,7 +1080,7 @@ public <A extends Enum<A>> Builder set(
"Enum expected, but found: " + value);
}

this.attributes.put(key.name(), value);
this.attrs.put(key.name(), value);

Object compare = key; // stellt JDK-6 zufrieden

Expand Down Expand Up @@ -1136,7 +1138,7 @@ public <A extends Enum<A>> Builder set(
*/
public Builder setAll(Attributes attributes) {

this.attributes.putAll(attributes.attributes);
this.attrs.putAll(attributes.attributes);
return this;

}
Expand All @@ -1155,7 +1157,7 @@ public Builder setAll(Attributes attributes) {
*/
public Builder remove(AttributeKey<?> key) {

this.attributes.remove(key.name());
this.attrs.remove(key.name());
return this;

}
Expand All @@ -1173,7 +1175,7 @@ public Builder remove(AttributeKey<?> key) {
*/
public Attributes build() {

return new Attributes(this.attributes);
return new Attributes(this.attrs);

}

Expand All @@ -1186,7 +1188,7 @@ private <A> void setInternal(
throw new NullPointerException("Missing attribute value.");
}

this.attributes.put(key.name(), value);
this.attrs.put(key.name(), value);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6619,6 +6619,10 @@ public ChronoFormatter<T> build() {
*/
public ChronoFormatter<T> build(Attributes attributes) {

if (attributes == null) {
throw new NullPointerException("Missing format attributes.");
}

Map<Integer, FormatStep> m = null;

for (int index = 0, len = this.steps.size(); index < len; index++) {
Expand Down

0 comments on commit 038669c

Please sign in to comment.