/* * Created by Andrey Cherkashin (acherkashin) * http://acherkashin.me * * License * Copyright (c) 2015 Andrey Cherkashin * The project released under the MIT license: http://opensource.org/licenses/MIT */ package ragefist.core.distribution; import com.juniform.JUniformObject; import java.util.Random; import javax.naming.directory.InvalidAttributesException; /** * * @author acherkashin */ public abstract class SelectionPolicy { // ---------------------------------------------------------------------- // // IMPLEMENTATIONS // ---------------------------------------------------------------------- // private static class SelectionPolicyRandom extends SelectionPolicy { private static final Random _rand = new Random(); public SelectionPolicyRandom(JUniformObject configObject) throws InvalidAttributesException { // no attributes required } @Override public int select(int max) { return _rand.nextInt(max); } } // ---------------------------------------------------------------------- // // STATIC // ---------------------------------------------------------------------- // public static SelectionPolicy newInstance(JUniformObject configObject) throws InvalidAttributesException { if (configObject == null) { throw new InvalidAttributesException("EnvironmentSelectionPolicy is null"); } String policyType = configObject.getProperty("Type").toStringDefault(""); if (policyType == null) { throw new InvalidAttributesException("EnvironmentSelectionPolicy.Type is null"); } switch(policyType) { default: throw new InvalidAttributesException("Unknown environment selection policy type: "+policyType); case "random": return new SelectionPolicyRandom(configObject); } } // ---------------------------------------------------------------------- // // PUBLIC // ---------------------------------------------------------------------- // /** * Selects an Environment/Server * @param max Maximum number of environments/servers * @return Target id */ public abstract int select(int max); }