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 15, 2023
1 parent f6a732a commit c2bb74a
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 123 deletions.
18 changes: 9 additions & 9 deletions src/main/java/biweekly/component/VEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -1655,15 +1655,7 @@ protected void validate(List<ICalComponent> components, ICalVersion version, Lis

checkOptionalCardinality(warnings, Color.class);

Status[] validStatuses;
switch (version) {
case V1_0:
validStatuses = new Status[] { Status.tentative(), Status.confirmed(), Status.declined(), Status.needsAction(), Status.sent(), Status.delegated() };
break;
default:
validStatuses = new Status[] { Status.tentative(), Status.confirmed(), Status.cancelled() };
break;
}
Status[] validStatuses = getValidStatuses(version);
checkStatus(warnings, validStatuses);

ICalDate dateStart = getValue(getDateStart());
Expand Down Expand Up @@ -1719,6 +1711,14 @@ protected void validate(List<ICalComponent> components, ICalVersion version, Lis
}
}

private Status[] getValidStatuses(ICalVersion version) {
//@formatter:off
return (version == ICalVersion.V1_0) ?
new Status[] { Status.tentative(), Status.confirmed(), Status.declined(), Status.needsAction(), Status.sent(), Status.delegated() } :
new Status[] { Status.tentative(), Status.confirmed(), Status.cancelled() };
//@formatter:on
}

@Override
public VEvent copy() {
return new VEvent(this);
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/biweekly/component/VTodo.java
Original file line number Diff line number Diff line change
Expand Up @@ -1689,15 +1689,7 @@ protected void validate(List<ICalComponent> components, ICalVersion version, Lis

checkOptionalCardinality(warnings, Color.class);

Status[] validStatuses;
switch (version) {
case V1_0:
validStatuses = new Status[] { Status.needsAction(), Status.completed(), Status.accepted(), Status.declined(), Status.delegated(), Status.sent() };
break;
default:
validStatuses = new Status[] { Status.needsAction(), Status.completed(), Status.inProgress(), Status.cancelled() };
break;
}
Status[] validStatuses = getValidStatuses(version);
checkStatus(warnings, validStatuses);

ICalDate dateStart = getValue(getDateStart());
Expand Down Expand Up @@ -1747,6 +1739,14 @@ protected void validate(List<ICalComponent> components, ICalVersion version, Lis
}
}

private Status[] getValidStatuses(ICalVersion version) {
//@formatter:off
return (version == ICalVersion.V1_0) ?
new Status[] { Status.needsAction(), Status.completed(), Status.accepted(), Status.declined(), Status.delegated(), Status.sent() } :
new Status[] { Status.needsAction(), Status.completed(), Status.inProgress(), Status.cancelled() };
//@formatter:on
}

@Override
public VTodo copy() {
return new VTodo(this);
Expand Down
96 changes: 56 additions & 40 deletions src/main/java/biweekly/io/scribe/component/VAlarmScribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,59 +128,75 @@ private static VCalAlarmProperty create(VAlarm valarm) {
}

if (action.isAudio()) {
AudioAlarm aalarm = new AudioAlarm();
return createAudioAlarm(valarm);
}

List<Attachment> attaches = valarm.getAttachments();
if (!attaches.isEmpty()) {
Attachment attach = attaches.get(0);
if (action.isDisplay()) {
createDisplayAlarm(valarm);
}

String formatType = attach.getFormatType();
aalarm.setParameter("TYPE", formatType);
if (action.isEmail()) {
return createEmailAlarm(valarm);
}

byte[] data = attach.getData();
if (data != null) {
aalarm.setData(data);
}
if (action.isProcedure()) {
return createProcedureAlarm(valarm);
}

String uri = attach.getUri();
if (uri != null) {
String contentId = StringUtils.afterPrefixIgnoreCase(uri, "cid:");
if (contentId == null) {
aalarm.setUri(uri);
} else {
aalarm.setContentId(contentId);
}
}
return null;
}

private static AudioAlarm createAudioAlarm(VAlarm valarm) {
AudioAlarm aalarm = new AudioAlarm();

List<Attachment> attachments = valarm.getAttachments();
if (!attachments.isEmpty()) {
Attachment firstAttachment = attachments.get(0);

String formatType = firstAttachment.getFormatType();
aalarm.setParameter("TYPE", formatType);

byte[] data = firstAttachment.getData();
if (data != null) {
aalarm.setData(data);
}

return aalarm;
String uri = firstAttachment.getUri();
if (uri != null) {
String contentId = StringUtils.afterPrefixIgnoreCase(uri, "cid:");
if (contentId == null) {
aalarm.setUri(uri);
} else {
aalarm.setContentId(contentId);
}
}
}

if (action.isDisplay()) {
Description description = valarm.getDescription();
String text = ValuedProperty.getValue(description);
return new DisplayAlarm(text);
}
return aalarm;
}

if (action.isEmail()) {
List<Attendee> attendees = valarm.getAttendees();
String email = attendees.isEmpty() ? null : attendees.get(0).getEmail();
EmailAlarm malarm = new EmailAlarm(email);
private static DisplayAlarm createDisplayAlarm(VAlarm valarm) {
Description description = valarm.getDescription();
String text = ValuedProperty.getValue(description);
return new DisplayAlarm(text);
}

Description description = valarm.getDescription();
String note = ValuedProperty.getValue(description);
malarm.setNote(note);
private static EmailAlarm createEmailAlarm(VAlarm valarm) {
List<Attendee> attendees = valarm.getAttendees();
String email = attendees.isEmpty() ? null : attendees.get(0).getEmail();
EmailAlarm malarm = new EmailAlarm(email);

return malarm;
}
Description description = valarm.getDescription();
String note = ValuedProperty.getValue(description);
malarm.setNote(note);

if (action.isProcedure()) {
Description description = valarm.getDescription();
String path = ValuedProperty.getValue(description);
return new ProcedureAlarm(path);
}
return malarm;
}

return null;
private static ProcedureAlarm createProcedureAlarm(VAlarm valarm) {
Description description = valarm.getDescription();
String path = ValuedProperty.getValue(description);
return new ProcedureAlarm(path);
}

/**
Expand Down
65 changes: 36 additions & 29 deletions src/main/java/biweekly/io/scribe/component/VFreeBusyScribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import biweekly.component.VFreeBusy;
import biweekly.property.FreeBusy;
import biweekly.property.ICalProperty;
import biweekly.util.ICalDate;
import biweekly.util.Period;

/*
Expand Down Expand Up @@ -57,35 +58,7 @@ public List<ICalProperty> getProperties(VFreeBusy component) {
}

//sort FREEBUSY properties by start date (p.100)
Collections.sort(fb, new Comparator<FreeBusy>() {
public int compare(FreeBusy one, FreeBusy two) {
Date oneStart = getEarliestStartDate(one);
Date twoStart = getEarliestStartDate(two);
if (oneStart == null && twoStart == null) {
return 0;
}
if (oneStart == null) {
return 1;
}
if (twoStart == null) {
return -1;
}
return oneStart.compareTo(twoStart);
}

private Date getEarliestStartDate(FreeBusy fb) {
Date date = null;
for (Period tp : fb.getValues()) {
if (tp.getStartDate() == null) {
continue;
}
if (date == null || date.compareTo(tp.getStartDate()) > 0) {
date = tp.getStartDate();
}
}
return date;
}
});
Collections.sort(fb, new FreeBusyStartDateComparator());

//find index of first FREEBUSY instance
int index = 0;
Expand All @@ -106,6 +79,40 @@ private Date getEarliestStartDate(FreeBusy fb) {
return properties;
}

private class FreeBusyStartDateComparator implements Comparator<FreeBusy> {
@Override
public int compare(FreeBusy one, FreeBusy two) {
Date oneStart = getEarliestStartDate(one);
Date twoStart = getEarliestStartDate(two);

if (oneStart == null && twoStart == null) {
return 0;
}
if (oneStart == null) {
return 1;
}
if (twoStart == null) {
return -1;
}
return oneStart.compareTo(twoStart);
}

private Date getEarliestStartDate(FreeBusy fb) {
Date earliestDate = null;
for (Period period : fb.getValues()) {
ICalDate startDate = period.getStartDate();
if (startDate == null) {
continue;
}

if (earliestDate == null || earliestDate.compareTo(startDate) > 0) {
earliestDate = startDate;
}
}
return earliestDate;
}
}

@Override
protected VFreeBusy _newInstance() {
return new VFreeBusy();
Expand Down
45 changes: 28 additions & 17 deletions src/main/java/biweekly/io/scribe/property/ICalPropertyScribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -730,35 +730,46 @@ public String write() {
}

if (observance) {
DateTimeComponents components = date.getRawComponents();
if (components == null) {
ICalDateFormat format = extended ? ICalDateFormat.DATE_TIME_EXTENDED_WITHOUT_TZ : ICalDateFormat.DATE_TIME_BASIC_WITHOUT_TZ;
return format.format(date);
}

return components.toString(true, extended);
return writeObservance();
}

if (utc) {
ICalDateFormat format = extended ? ICalDateFormat.UTC_TIME_EXTENDED : ICalDateFormat.UTC_TIME_BASIC;
return writeUtc();
}

return date.hasTime() ? writeDateTime() : writeDate();
}

private String writeObservance() {
DateTimeComponents components = date.getRawComponents();
if (components == null) {
ICalDateFormat format = extended ? ICalDateFormat.DATE_TIME_EXTENDED_WITHOUT_TZ : ICalDateFormat.DATE_TIME_BASIC_WITHOUT_TZ;
return format.format(date);
}

return components.toString(true, extended);
}

private String writeUtc() {
ICalDateFormat format = extended ? ICalDateFormat.UTC_TIME_EXTENDED : ICalDateFormat.UTC_TIME_BASIC;
return format.format(date);
}

private String writeDateTime() {
ICalDateFormat format;
TimeZone timezone = this.timezone;
if (date.hasTime()) {
if (timezone == null) {
format = extended ? ICalDateFormat.UTC_TIME_EXTENDED : ICalDateFormat.UTC_TIME_BASIC;
} else {
format = extended ? ICalDateFormat.DATE_TIME_EXTENDED_WITHOUT_TZ : ICalDateFormat.DATE_TIME_BASIC_WITHOUT_TZ;
}
if (timezone == null) {
format = extended ? ICalDateFormat.UTC_TIME_EXTENDED : ICalDateFormat.UTC_TIME_BASIC;
} else {
format = extended ? ICalDateFormat.DATE_EXTENDED : ICalDateFormat.DATE_BASIC;
timezone = null;
format = extended ? ICalDateFormat.DATE_TIME_EXTENDED_WITHOUT_TZ : ICalDateFormat.DATE_TIME_BASIC_WITHOUT_TZ;
}

return format.format(date, timezone);
}

private String writeDate() {
ICalDateFormat format = extended ? ICalDateFormat.DATE_EXTENDED : ICalDateFormat.DATE_BASIC;
return format.format(date);
}
}

/**
Expand Down
Loading

0 comments on commit c2bb74a

Please sign in to comment.