View Javadoc
1   package net.sourceforge.rpgee.dice;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.Random;
8   import java.util.Vector;
9   
10  import net.sourceforge.rpgee.AbstractIdentified;
11  import net.sourceforge.rpgee.Describable;
12  import net.sourceforge.rpgee.dice.exceptions.InvalidParameterException;
13  import net.sourceforge.rpgee.dice.exceptions.RandomValueException;
14  import net.sourceforge.rpgee.messaging.RpgMessage;
15  import net.sourceforge.rpgee.messaging.RpgMessageProducer;
16  import net.sourceforge.rpgee.messaging.exceptions.RpgMessageConsumer;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  public abstract class AbstractRandomValueGenerator extends AbstractIdentified implements Describable, RandomValueGenerator,
22          RpgMessageProducer {
23      private static Log               log                     = LogFactory.getLog(AbstractRandomValueGenerator.class);
24  
25      private static String[]          settingNames            = { DICE_MAPPING_CLASS };
26      protected static String[]        moreSettings            = {};
27      private Map<String, Object>      settings                = new HashMap<String, Object>(5);
28      private Random                   random                  = null;
29      private List<RandomValueHandler> randomValueHandlers     = new ArrayList<RandomValueHandler>();
30      private RandomValueResultMapper  randomValueResultMapper = null;
31      private String                   description             = "This is the Abstract RandomValueGenerator";
32      private Object                   randomnessFactor;
33      private List<RpgMessageConsumer>    consumers               = new ArrayList<RpgMessageConsumer>();
34  
35      public AbstractRandomValueGenerator() {
36          super();
37      };
38  
39      public void setDescription(String description) {
40          this.description = description;
41      }
42  
43      public List<RandomValueHandler> getRandomValueHandlers() {
44          return randomValueHandlers;
45      }
46  
47      public String getRandomValuesAsString(List<RandomValue> roll) throws RandomValueException {
48  
49          StringBuffer b = new StringBuffer("[");
50          for (RandomValue v : roll) {
51              b.append(v.getValueAsString());
52          }
53          b.append("]");
54          return b.toString();
55      }
56  
57      public String getDescription() {
58          return description;
59      }
60  
61      public String getHTMLDescription() {
62          return CODE_HEADER + getDescription() + CODE_FOOTER;
63      }
64  
65      public void setRandomValueHandlers(List<RandomValueHandler> _handlers) {
66          this.randomValueHandlers = _handlers;
67      }
68  
69      public void setRandomnessFactor(Object o) {
70          this.randomnessFactor = o;
71      }
72  
73      public void generateRandomness() throws RandomValueException {
74          long i;
75          try {
76              i = Long.parseLong(this.randomnessFactor.toString());
77          } catch (Exception e) {
78              log.error("Error getting randomnesss factor", e);
79              log.warn("Setting randomness factor to current time");
80              i = System.currentTimeMillis();
81          }
82          random = new Random(i);
83      }
84  
85      public GeneratedRandomValue produceGeneratedRandomValue(String d) throws RandomValueException {
86          if (random == null)
87              generateRandomness();
88          return _internalDiceRoll(d);
89      }
90  
91      public void addRandomValueHandler(RandomValueHandler handler) {
92          this.randomValueHandlers.add(handler);
93      }
94  
95      public void setSetting(String setting, Object value) throws InvalidParameterException {
96          _internalPreSetSetting(setting, value);
97          this.settings.put(setting, value);
98          if (setting.equals(DICE_MAPPING_CLASS)) {
99              // @TODO load class by name, instantiate, and place in mapper
100         }
101         _internalPostSetSetting(setting, value);
102     }
103 
104     public Object getSetting(String key) {
105         return this.settings.get(key);
106     }
107 
108     public void setRandomValueResultMapper(RandomValueResultMapper mapper) {
109         this.randomValueResultMapper = mapper;
110     }
111 
112     public RandomValueResultMapper getRandomValueResultMapper() {
113         return randomValueResultMapper;
114     }
115 
116     protected int getIntegerFromSettings(String key) throws InvalidParameterException {
117         try {
118             Object x = this.settings.get(key);
119             if (x == null)
120                 throw new InvalidParameterException("dice.error.no-parameter-set");
121             return Integer.parseInt(x.toString());
122         } catch (NumberFormatException e) {
123             throw new InvalidParameterException(e, "dice.error.invalid-numeric-value");
124         }
125     }
126 
127     protected Random getRandom() {
128         return random;
129     }
130 
131     public String[] getSettings() {
132         String[] moreSettings = getMoreSettingsNames();
133         Vector<String> v = new Vector<String>(settingNames.length + moreSettings.length);
134         for (int i = 0; i < settingNames.length; ++i) {
135             v.add(settingNames[i]);
136         }
137         for (int i = 0; i < moreSettings.length; ++i) {
138             v.add(moreSettings[i]);
139         }
140         return v.toArray(new String[v.size()]);
141     }
142 
143     public void addMessageConsumer(RpgMessageConsumer consumer) {
144         this.consumers.add(consumer);
145     }
146 
147     public void setMessageConsumers(List<RpgMessageConsumer> _consumers) {
148         this.consumers = _consumers;
149     }
150 
151     public void sendMessage(RpgMessage message) {
152         for (RpgMessageConsumer c : this.consumers) {
153             c.consume(message);
154         }
155     }
156 
157     protected abstract String[] getMoreSettingsNames();
158 
159     public abstract void require(Object requirement) throws RandomValueException;
160 
161     public abstract GeneratedRandomValue _internalDiceRoll(String d) throws RandomValueException;
162 
163     public abstract void _internalPostSetSetting(String setting, Object value) throws InvalidParameterException;
164 
165     public abstract void _internalPreSetSetting(String setting, Object value) throws InvalidParameterException;
166 
167 }