/******************************************************************************* * Copyright (c) 2012-2013 EclipseSource Muenchen GmbH and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Stephan Koehler, Eugen Neufeld, Philip Achenbach - initial API and implementation ******************************************************************************/ package org.eclipse.emf.emfstore.internal.modelmutator.intern.attribute; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Random; import org.eclipse.emf.common.util.Enumerator; import org.eclipse.emf.ecore.EEnum; import org.eclipse.emf.ecore.EEnumLiteral; /** * Class for creating random Enumerator values. * * @author Eugen Neufeld * @author Stephan Koehler * @author Philip Achenbach * * @see AttributeSetter */ public class AttributeSetterEEnum extends AttributeSetter<Enumerator> { /** * The EEnum for which the Enumerators shall be created. */ private final EEnum eEnum; /** * Creates a new AttributeSetter for Enumerator attributes. * * @param eEnum * the EEnum this attribute setter will create Enumerators for * @param random * Random object used to create attribute values */ public AttributeSetterEEnum(EEnum eEnum, Random random) { super(random); this.eEnum = eEnum; } /** * {@inheritDoc} */ @Override public Enumerator createNewAttribute() { final List<EEnumLiteral> literals = new ArrayList<EEnumLiteral>(eEnum.getELiterals()); if (literals.isEmpty()) { return null; } Collections.shuffle(literals, getRandom()); return literals.get(0).getInstance(); } /** * {@inheritDoc} */ @Override public Collection<Enumerator> createNewAttributes(int maxAmount) { final List<Enumerator> result = new ArrayList<Enumerator>(maxAmount); // add instances of all possible literals for (final EEnumLiteral literal : eEnum.getELiterals()) { result.add(literal.getInstance()); } Collections.shuffle(result, getRandom()); // remove random Enumerators until at most maxObjects are returned while (result.size() > maxAmount) { result.remove(0); } return result; } }