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.config; import java.lang.reflect.InvocationTargetException; import java.util.AbstractMap.SimpleEntry; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.opentestsystem.authoring.testauth.domain.AssessmentChild; import org.opentestsystem.shared.exception.LocalizedException; import org.springframework.validation.FieldError; import com.google.common.base.Function; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; public final class TestAuthUtil { public static final String DEFAULT_VERSION = "1.0"; private TestAuthUtil() { // singleton } // ======================================================================================================================================================================== // utility public static <O extends Object, T extends Object> List<T> nullsafeListTransform( final Iterable<? extends O> iterableToFilter, final Function<O, T> transformerFunction) { if (iterableToFilter == null) { return Lists.newArrayList(); } return Lists.newArrayList( Iterables.filter(Iterables.transform(iterableToFilter, transformerFunction), Predicates.notNull())); } public static String[] paramArray(final String... param) { return param; } public static Map<String, String[]> searchParamsByAssessmentIdSmall(final String assessmentId) { return Maps.newHashMap(ImmutableMap.of("assessmentId", paramArray(assessmentId))); } public static Map<String, String[]> searchParamsByAssessmentIdLarge(final String assessmentId) { return Maps.newHashMap( ImmutableMap.of("assessmentId", paramArray(assessmentId), "pageSize", paramArray("10000"))); } public static int safeParseInt(final String val) { try { return Integer.parseInt(val); } catch (final NumberFormatException e) { return 0; } } public static int safeParseInt(final Integer intVal) { return intVal == null ? 0 : intVal; } public static final class ASSESSMENT_CHILD_COPY_XFORMER<T extends AssessmentChild> implements Function<T, T> { private final String newAssessmentId; @SuppressWarnings("rawtypes") public static ASSESSMENT_CHILD_COPY_XFORMER getInstance(final String newAssessmentId) { return new ASSESSMENT_CHILD_COPY_XFORMER(newAssessmentId); } public ASSESSMENT_CHILD_COPY_XFORMER(final String newAssessmentId) { this.newAssessmentId = newAssessmentId; } @Override public T apply(final T oldAssessmentChild) { final T newAssessmentChild = safeClone(oldAssessmentChild); newAssessmentChild.setId(null); newAssessmentChild.setAssessmentId(this.newAssessmentId); newAssessmentChild.setCopyId(oldAssessmentChild.getId()); return newAssessmentChild; } } public static final Function<AssessmentChild, SimpleEntry<String, String>> ASSESSMENT_CHILD_KEYMAP_XFORMER = new Function<AssessmentChild, SimpleEntry<String, String>>() { @Override public SimpleEntry<String, String> apply(final AssessmentChild assessmentChild) { return new SimpleEntry<String, String>(assessmentChild.getCopyId(), assessmentChild.getId()); } }; public static FieldError buildValidationFieldError(final String objectName, final String fieldName, final String messageKey, final String[] messageParams) { return new FieldError(objectName, fieldName, null, false, paramArray(messageKey), messageParams, messageKey); } @SuppressWarnings("unchecked") public static <T extends Object> T safeClone(final T objectToClone) { T returnedClone = null; try { returnedClone = (T) BeanUtils.cloneBean(objectToClone); } catch (final IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { throw new LocalizedException("unsuccessful.clone", e); } return returnedClone; } }