Skip to content

Commit

Permalink
Reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed Nov 5, 2023
1 parent bed7131 commit 3394da0
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 236 deletions.
72 changes: 42 additions & 30 deletions src/main/java/biweekly/io/json/JCalRawWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,64 +230,76 @@ public void writeProperty(String propertyName, ICalParameters parameters, ICalDa

//write value
for (JsonValue jsonValue : value.getValues()) {
writeValue(jsonValue);
writeJsonValue(jsonValue);
}

generator.writeEndArray();

generator.setCurrentValue(null);
}

private void writeValue(JsonValue jsonValue) throws IOException {
private void writeJsonValue(JsonValue jsonValue) throws IOException {
if (jsonValue.isNull()) {
generator.writeNull();
return;
}

Object val = jsonValue.getValue();
if (val != null) {
if (val instanceof Byte) {
generator.writeNumber((Byte) val);
} else if (val instanceof Short) {
generator.writeNumber((Short) val);
} else if (val instanceof Integer) {
generator.writeNumber((Integer) val);
} else if (val instanceof Long) {
generator.writeNumber((Long) val);
} else if (val instanceof Float) {
generator.writeNumber((Float) val);
} else if (val instanceof Double) {
generator.writeNumber((Double) val);
} else if (val instanceof Boolean) {
generator.writeBoolean((Boolean) val);
} else {
generator.writeString(val.toString());
}
writeValue(val);
return;
}

List<JsonValue> array = jsonValue.getArray();
if (array != null) {
generator.writeStartArray();
for (JsonValue element : array) {
writeValue(element);
}
generator.writeEndArray();
writeArray(array);
return;
}

Map<String, JsonValue> object = jsonValue.getObject();
if (object != null) {
generator.writeStartObject();
for (Map.Entry<String, JsonValue> entry : object.entrySet()) {
generator.writeFieldName(entry.getKey());
writeValue(entry.getValue());
}
generator.writeEndObject();
writeObject(object);
return;
}
}

private void writeValue(Object val) throws IOException {
if (val instanceof Byte) {
generator.writeNumber((Byte) val);
} else if (val instanceof Short) {
generator.writeNumber((Short) val);
} else if (val instanceof Integer) {
generator.writeNumber((Integer) val);
} else if (val instanceof Long) {
generator.writeNumber((Long) val);
} else if (val instanceof Float) {
generator.writeNumber((Float) val);
} else if (val instanceof Double) {
generator.writeNumber((Double) val);
} else if (val instanceof Boolean) {
generator.writeBoolean((Boolean) val);
} else {
generator.writeString(val.toString());
}
}

private void writeArray(List<JsonValue> array) throws IOException {
generator.writeStartArray();
for (JsonValue element : array) {
writeJsonValue(element);
}
generator.writeEndArray();
}

private void writeObject(Map<String, JsonValue> object) throws IOException {
generator.writeStartObject();
for (Map.Entry<String, JsonValue> entry : object.entrySet()) {
generator.writeFieldName(entry.getKey());
writeJsonValue(entry.getValue());
}
generator.writeEndObject();
}

/**
* Flushes the JSON stream.
* @throws IOException if there's a problem flushing the stream
Expand Down
80 changes: 50 additions & 30 deletions src/main/java/biweekly/io/scribe/property/FreeBusyScribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,40 +127,20 @@ protected FreeBusy _parseXml(XCalElement element, ICalParameters parameters, Par

FreeBusy property = new FreeBusy();
for (XCalElement periodElement : periodElements) {
String startStr = periodElement.first("start");
if (startStr == null) {
throw new CannotParseException(9);
}
ICalDate start = parseStart(periodElement);

ICalDate start;
try {
start = date(startStr).parse();
} catch (IllegalArgumentException e) {
throw new CannotParseException(10, startStr);
}

String endStr = periodElement.first("end");
if (endStr != null) {
try {
ICalDate end = date(endStr).parse();
property.getValues().add(new Period(start, end));
context.addDate(start, property, parameters);
context.addDate(end, property, parameters);
} catch (IllegalArgumentException e) {
throw new CannotParseException(11, endStr);
}
ICalDate end = parseEnd(periodElement);
if (end != null) {
property.getValues().add(new Period(start, end));
context.addDate(start, property, parameters);
context.addDate(end, property, parameters);
continue;
}

String durationStr = periodElement.first("duration");
if (durationStr != null) {
try {
Duration duration = Duration.parse(durationStr);
property.getValues().add(new Period(start, duration));
context.addDate(start, property, parameters);
} catch (IllegalArgumentException e) {
throw new CannotParseException(12, durationStr);
}
Duration duration = parseDuration(periodElement);
if (duration != null) {
property.getValues().add(new Period(start, duration));
context.addDate(start, property, parameters);
continue;
}

Expand All @@ -170,6 +150,46 @@ protected FreeBusy _parseXml(XCalElement element, ICalParameters parameters, Par
return property;
}

private ICalDate parseStart(XCalElement periodElement) {
String startStr = periodElement.first("start");
if (startStr == null) {
//start is required
throw new CannotParseException(9);
}

try {
return date(startStr).parse();
} catch (IllegalArgumentException e) {
throw new CannotParseException(10, startStr);
}
}

private ICalDate parseEnd(XCalElement periodElement) {
String endStr = periodElement.first("end");
if (endStr == null) {
return null;
}

try {
return date(endStr).parse();
} catch (IllegalArgumentException e) {
throw new CannotParseException(11, endStr);
}
}

private Duration parseDuration(XCalElement periodElement) {
String durationStr = periodElement.first("duration");
if (durationStr == null) {
return null;
}

try {
return Duration.parse(durationStr);
} catch (IllegalArgumentException e) {
throw new CannotParseException(12, durationStr);
}
}

@Override
protected JCalValue _writeJson(FreeBusy property, WriteContext context) {
List<Period> values = property.getValues();
Expand Down
Loading

0 comments on commit 3394da0

Please sign in to comment.