Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.authoring.testauth.service.impl; import org.opentestsystem.authoring.testauth.domain.AffinityGroup; import org.opentestsystem.authoring.testauth.domain.AffinityGroupValue; import org.opentestsystem.authoring.testauth.domain.ItemSelectionAlgorithm; import org.opentestsystem.authoring.testauth.domain.ItemSelectionAlgorithmHelper; import com.google.common.base.Function; import com.google.common.collect.Maps; public final class AffinityGroupHelper { private AffinityGroupHelper() { // do not instantiate } protected static final class AFFINITY_GROUP_ADD_DEFAULT_SEGMENT_VALUE_TRANSFORMER implements Function<AffinityGroup, AffinityGroup> { private final String segmentId; private final ItemSelectionAlgorithm itemSelectionAlgorithm; public static AFFINITY_GROUP_ADD_DEFAULT_SEGMENT_VALUE_TRANSFORMER getInstance(final String segmentId, final ItemSelectionAlgorithm itemSelectionAlgorithm) { return new AFFINITY_GROUP_ADD_DEFAULT_SEGMENT_VALUE_TRANSFORMER(segmentId, itemSelectionAlgorithm); } private AFFINITY_GROUP_ADD_DEFAULT_SEGMENT_VALUE_TRANSFORMER(final String segmentId, final ItemSelectionAlgorithm itemSelectionAlgorithm) { this.segmentId = segmentId; this.itemSelectionAlgorithm = itemSelectionAlgorithm; } @Override public AffinityGroup apply(final AffinityGroup affinityGroup) { if (affinityGroup != null && affinityGroup.getAffinityGroupValueMap() != null) { affinityGroup.getAffinityGroupValueMap().put(this.segmentId, new AffinityGroupValue(0, 0, 0, 0)); populateDefaultItemSelectionParameters(affinityGroup, this.segmentId, this.itemSelectionAlgorithm); } return affinityGroup; } }; protected static final class AFFINITY_GROUP_UPDATE_ISA_PARAMETERS_TRANSFORMER implements Function<AffinityGroup, AffinityGroup> { private final String segmentId; private final ItemSelectionAlgorithm itemSelectionAlgorithm; public static AFFINITY_GROUP_UPDATE_ISA_PARAMETERS_TRANSFORMER getInstance(final String segmentId, final ItemSelectionAlgorithm itemSelectionAlgorithm) { return new AFFINITY_GROUP_UPDATE_ISA_PARAMETERS_TRANSFORMER(segmentId, itemSelectionAlgorithm); } private AFFINITY_GROUP_UPDATE_ISA_PARAMETERS_TRANSFORMER(final String aSegmentId, final ItemSelectionAlgorithm aItemSelectionAlgorithm) { this.segmentId = aSegmentId; this.itemSelectionAlgorithm = aItemSelectionAlgorithm; } @Override public AffinityGroup apply(final AffinityGroup affinityGroup) { if (affinityGroup != null && affinityGroup.getAffinityGroupValueMap() != null && affinityGroup.getAffinityGroupValueMap().get(this.segmentId) != null) { populateDefaultItemSelectionParameters(affinityGroup, this.segmentId, this.itemSelectionAlgorithm); } return affinityGroup; } }; protected static final class AFFINITY_GROUP_REMOVE_SEGMENT_VALUE_TRANSFORMER implements Function<AffinityGroup, AffinityGroup> { private final String segmentId; public static AFFINITY_GROUP_REMOVE_SEGMENT_VALUE_TRANSFORMER getInstance(final String segmentId) { return new AFFINITY_GROUP_REMOVE_SEGMENT_VALUE_TRANSFORMER(segmentId); } private AFFINITY_GROUP_REMOVE_SEGMENT_VALUE_TRANSFORMER(final String segmentId) { this.segmentId = segmentId; } @Override public AffinityGroup apply(final AffinityGroup affinityGroup) { affinityGroup.getAffinityGroupValueMap().remove(this.segmentId); return affinityGroup; } }; private static final void populateDefaultItemSelectionParameters(final AffinityGroup affinityGroup, final String segmentId, final ItemSelectionAlgorithm itemSelectionAlgorithm) { if (itemSelectionAlgorithm != null && itemSelectionAlgorithm.getBlueprintParameters() != null) { affinityGroup.getAffinityGroupValueMap().get(segmentId) .setItemSelectionParameters(Maps.newHashMap(Maps.transformValues( Maps.uniqueIndex(itemSelectionAlgorithm.getBlueprintParameters(), ItemSelectionAlgorithmHelper.ISA_PARAMETER_NAME_TRANSFORMER), ItemSelectionAlgorithmHelper.ISA_PARAMETER_DEFAULT_VALUE_TRANSFORMER))); } else { affinityGroup.getAffinityGroupValueMap().get(segmentId).setItemSelectionParameters(null); } } }