Source code: org/metacosm/util/RandomAbsoluteModifier.java
1 /*
2 Metacosm, an object-oriented network game framework
3 Copyright (C) 1999-2001 Metacosm Development Team
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package org.metacosm.util;
21
22 import org.metacosm.framework.random.DistributionLaw;
23
24 /**
25 * A RandomAbsoluteModifier is an AbsoluteModifier which value is not fixed or known
26 * at Influence creation time. The value is determined at Influence applying time with
27 * a DistributionLaw.
28 */
29 final public class RandomAbsoluteModifier extends AbsoluteModifier {
30
31 /**
32 * @throws IllegalArgumentException if law is null or priority not in [MIN_PRIORITY, MAX_PRIORITY]
33 */
34 public RandomAbsoluteModifier( DistributionLaw law, int priority) throws IllegalArgumentException {
35 super();
36 if ( law == null || priority < MIN_PRIORITY || priority > MAX_PRIORITY) {
37 throw new IllegalArgumentException();
38 } else {
39 this.law = law;
40 this.priority = priority;
41 }
42 }
43
44 /**
45 * @throws IllegalArgumentException if law is null
46 */
47 public RandomAbsoluteModifier( DistributionLaw law) throws IllegalArgumentException {
48 super();
49 if ( law == null) {
50 throw new IllegalArgumentException();
51 } else {
52 this.law = law;
53 this.priority = NORMAL_PRIORITY;
54 }
55 }
56
57 /**
58 * Should just be called once at Influence applying time.
59 */
60 public Object getValue() {
61 return law.nextValue();
62 }
63
64 public void load(java.io.InputStream is) throws java.io.IOException {
65 // serialization for now
66 /*
67 java.io.ObjectInputStream ois = new java.io.ObjectInputStream( is);
68 try {
69 RandomAbsoluteModifier ram = (RandomAbsoluteModifier) ois.readObject();
70 this.value = ram.value;
71 this.priority = ram.priority;
72 this.law = ram.law;
73 } catch ( ClassNotFoundException e) {
74 throw new UnsupportedOperationException();
75 }
76 */
77 }
78
79 public String toString() {
80 return new String( law + " " + priority);
81 }
82
83 private DistributionLaw law;
84 }