1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.policies.createtable; 26 27 import java.util.ArrayList ; 28 import java.util.Random ; 29 30 38 public class CreateTableRandom extends CreateTableRule 39 { 40 private Random random = new Random (); 41 42 45 public CreateTableRandom() 46 { 47 super(CreateTablePolicy.RANDOM); 48 } 49 50 55 public CreateTableRandom(ArrayList backendList) 56 { 57 super(CreateTablePolicy.RANDOM, backendList); 58 } 59 60 63 public ArrayList getBackends(ArrayList backends) throws CreateTableException 64 { 65 if (nbOfNodes == 0) 66 return null; 67 68 ArrayList clonedList = super.getBackends(backends); 69 70 int clonedSize = clonedList.size(); 71 72 if (nbOfNodes == clonedSize) 73 return clonedList; 74 else if (nbOfNodes > clonedSize) 75 throw new CreateTableException( 76 "Asking for more backends (" 77 + nbOfNodes 78 + ") than available (" 79 + clonedSize 80 + ")"); 81 82 ArrayList result = new ArrayList (nbOfNodes); 83 84 for (int i = 0; i < nbOfNodes; i++) 85 result.add(clonedList.remove(random.nextInt(clonedSize - i))); 86 87 return result; 88 } 89 90 93 public String getInformation() 94 { 95 String s; 96 if (tableName == null) 97 s = "Default rule create table on "; 98 else 99 s = "Rule for table " + tableName + " create table on "; 100 101 return s + nbOfNodes + " node(s) randomly from " + backendList; 102 } 103 } 104 | Popular Tags |