Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted groups plugin reports to use plugin data instead of builder… #60

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 90 additions & 8 deletions gcm4/src/main/java/plugins/groups/GroupsPlugin.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,113 @@
package plugins.groups;

import java.util.Optional;

import nucleus.Plugin;
import plugins.groups.datamanagers.GroupsDataManager;
import plugins.groups.reports.GroupPopulationReport;
import plugins.groups.reports.GroupPopulationReportPluginData;
import plugins.groups.reports.GroupPropertyReport;
import plugins.groups.reports.GroupPropertyReportPluginData;
import plugins.groups.support.GroupError;
import plugins.people.PeoplePluginId;
import plugins.personproperties.support.PersonPropertyError;
import plugins.stochastics.StochasticsPluginId;
import util.errors.ContractException;

/**
* A plugin providing a group data manager to the simulation.
*
*
*/
public final class GroupsPlugin {

private static class Data {
private GroupsPluginData groupsPluginData;
private GroupPopulationReportPluginData groupPopulationReportPluginData;
private GroupPropertyReportPluginData groupPropertyReportPluginData;
}

private GroupsPlugin() {
}

public static Plugin getGroupPlugin(GroupsPluginData groupsPluginData) {
public static Builder builder() {
return new Builder();
}

public static class Builder {
private Builder() {
}

private Data data = new Data();

private void validate() {
if (data.groupsPluginData == null) {
throw new ContractException(GroupError.NULL_GROUP_PLUGIN_DATA);
}
}

public Builder setGroupsPluginData(GroupsPluginData groupsPluginData) {
data.groupsPluginData = groupsPluginData;
return this;
}

return Plugin .builder()//
.setPluginId(GroupsPluginId.PLUGIN_ID)//
.addPluginData(groupsPluginData)//
.addPluginDependency(PeoplePluginId.PLUGIN_ID)//
.addPluginDependency(StochasticsPluginId.PLUGIN_ID)//
public Builder setGroupPopulationReportPluginData(GroupPopulationReportPluginData groupPopulationReportPluginData) {
data.groupPopulationReportPluginData = groupPopulationReportPluginData;
return this;
}

public Builder setGroupPropertyReportPluginData(GroupPropertyReportPluginData groupPropertyReportPluginData) {
data.groupPropertyReportPluginData = groupPropertyReportPluginData;
return this;
}

/**
* Builds the PersonPropertiesPlugin from the collected inputs
*
* @throws ContractException
* <li>{@linkplain PersonPropertyError#NULL_GROUP_PLUGIN_DATA}
* if the groupsPluginData is null</li>
*/
public Plugin getGroupsPlugin() {

try {
validate();
Plugin.Builder builder = Plugin.builder();//
builder.setPluginId(GroupsPluginId.PLUGIN_ID);//

if (data.groupPopulationReportPluginData != null) {
builder.addPluginData(data.groupPopulationReportPluginData);
}

if (data.groupPropertyReportPluginData != null) {
builder.addPluginData(data.groupPropertyReportPluginData);
}

builder.addPluginData(data.groupsPluginData);//
builder.addPluginDependency(PeoplePluginId.PLUGIN_ID);//
builder .addPluginDependency(StochasticsPluginId.PLUGIN_ID)//
.setInitializer((c) -> {
GroupsPluginData pluginData = c.getPluginData(GroupsPluginData.class).get();
c.addDataManager(new GroupsDataManager(pluginData));
})//
.build();

Optional<GroupPopulationReportPluginData> optional1 = c.getPluginData(GroupPopulationReportPluginData.class);
if (optional1.isPresent()) {
GroupPopulationReportPluginData groupPopulationReportPluginData = optional1.get();
c.addReport(new GroupPopulationReport(groupPopulationReportPluginData)::init);
}

Optional<GroupPropertyReportPluginData> optional2 = c.getPluginData(GroupPropertyReportPluginData.class);
if (optional2.isPresent()) {
GroupPropertyReportPluginData groupPropertyReportPluginData = optional2.get();
c.addReport(new GroupPropertyReport(groupPropertyReportPluginData)::init);
}

});//
return builder.build();
} finally {
data = new Data();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import plugins.reports.support.PeriodicReport;
import plugins.reports.support.ReportHeader;
import plugins.reports.support.ReportItem;
import plugins.reports.support.ReportLabel;
import plugins.reports.support.ReportPeriod;

/**
* A periodic Report that displays the number of groups having a particular
Expand All @@ -29,8 +27,8 @@
*/
public final class GroupPopulationReport extends PeriodicReport {

public GroupPopulationReport(ReportLabel reportLabel,ReportPeriod reportPeriod) {
super(reportLabel,reportPeriod);
public GroupPopulationReport(GroupPopulationReportPluginData groupPopulationReportPluginData) {
super(groupPopulationReportPluginData.getReportLabel(),groupPopulationReportPluginData.getReportPeriod());
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package plugins.groups.reports;

import net.jcip.annotations.ThreadSafe;
import nucleus.PluginData;
import nucleus.PluginDataBuilder;
import plugins.reports.support.ReportError;
import plugins.reports.support.ReportLabel;
import plugins.reports.support.ReportPeriod;
import util.errors.ContractException;

/**
* A PluginData class supporting GroupPopulationReport construction.
*/
@ThreadSafe
public final class GroupPopulationReportPluginData implements PluginData {

/*
* Data class for collecting the inputs to the report
*/
private static class Data {
private ReportLabel reportLabel;
private ReportPeriod reportPeriod;


private boolean locked;

private Data() {
}

private Data(Data data) {
reportLabel = data.reportLabel;
reportPeriod = data.reportPeriod;

locked = data.locked;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((reportLabel == null) ? 0 : reportLabel.hashCode());
result = prime * result + ((reportPeriod == null) ? 0 : reportPeriod.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Data)) {
return false;
}
Data other = (Data) obj;
if (reportLabel == null) {
if (other.reportLabel != null) {
return false;
}
} else if (!reportLabel.equals(other.reportLabel)) {
return false;
}
if (reportPeriod != other.reportPeriod) {
return false;
}
return true;
}

}

/**
* Returns a new instance of the builder class
*/
public static Builder builder() {
return new Builder(new Data());
}

/**
* Builder class for the report
*
*
*/
public final static class Builder implements PluginDataBuilder {
private Builder(Data data) {
this.data = data;
}

private void ensureDataMutability() {
if (data.locked) {
data = new Data(data);
data.locked = false;
}
}

private void ensureImmutability() {
if (!data.locked) {
data.locked = true;
}
}

private void validateData() {
if (data.reportLabel == null) {
throw new ContractException(ReportError.NULL_REPORT_LABEL);
}
if (data.reportPeriod == null) {
throw new ContractException(ReportError.NULL_REPORT_PERIOD);
}
}

private Data data;

/**
* Returns a PersonPropertyReportPluginData created from the collected
* inputs
*
* @throws ContractException
* <li>{@linkplain ReportError#NULL_REPORT_LABEL} if the
* report label is not assigned</li>
* <li>{@linkplain ReportError#NULL_REPORT_PERIOD} if the
* report period is not assigned</li>
*
*
*/
public GroupPopulationReportPluginData build() {

if (!data.locked) {
validateData();
}
ensureImmutability();
return new GroupPopulationReportPluginData(data);

}

/**
* Sets the report label
*
* @throws ContractException
* <li>{@linkplain ReportError#NULL_REPORT_LABEL} if the
* report label is null</li>
*/
public Builder setReportLabel(ReportLabel reportLabel) {
ensureDataMutability();
if (reportLabel == null) {
throw new ContractException(ReportError.NULL_REPORT_LABEL);
}
data.reportLabel = reportLabel;
return this;
}


/**
* Sets the report period id
*
* @throws ContractException
* <li>{@linkplain ReportError#NULL_REPORT_PERIOD} if the
* report period is null</li>
*/
public Builder setReportPeriod(ReportPeriod reportPeriod) {
ensureDataMutability();
if (reportPeriod == null) {
throw new ContractException(ReportError.NULL_REPORT_PERIOD);
}
data.reportPeriod = reportPeriod;
return this;
}
}

private final Data data;

private GroupPopulationReportPluginData(Data data) {
this.data = data;
}

@Override
public Builder getCloneBuilder() {
return new Builder(data);
}

@Override
public Builder getEmptyBuilder() {
return builder();
}

public ReportLabel getReportLabel() {
return data.reportLabel;
}

public ReportPeriod getReportPeriod() {
return data.reportPeriod;
}


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((data == null) ? 0 : data.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof GroupPopulationReportPluginData)) {
return false;
}
GroupPopulationReportPluginData other = (GroupPopulationReportPluginData) obj;
if (data == null) {
if (other.data != null) {
return false;
}
} else if (!data.equals(other.data)) {
return false;
}
return true;
}
}
Loading