1 10 package org.jgap.distr; 11 12 import java.io.*; 13 import java.util.*; 14 import org.apache.commons.lang.builder.*; 15 16 24 public class Culture 25 implements Serializable, Comparable { 26 27 private final static String CVS_REVISION = "$Revision: 1.14 $"; 28 29 32 private CultureMemoryCell[] m_memory; 33 34 37 private List m_memoryNames = new Vector(); 38 39 42 private int m_size; 43 44 48 private int m_width; 49 50 51 59 public Culture(final int a_size) { 60 if (a_size < 1) { 61 throw new IllegalArgumentException ("Size must be greater than zero!"); 62 } 63 m_size = a_size; 64 m_memory = new CultureMemoryCell[m_size]; 65 m_width = 1; 66 } 67 68 82 public CultureMemoryCell set(final int a_index, final double a_value, 83 final int a_historySize, final String a_name) { 84 if (a_index < 0 || a_index >= size()) { 85 throw new IllegalArgumentException ("Illegal memory index!"); 86 } 87 CultureMemoryCell cell = new CultureMemoryCell(a_name, a_historySize); 88 cell.setDouble(a_value); 89 m_memory[a_index] = cell; 90 return cell; 91 } 92 93 107 public CultureMemoryCell set(final int a_index, final Object a_value, 108 final int a_historySize, final String a_infotext) { 109 if (a_index < 0 || a_index >= size()) { 110 throw new IllegalArgumentException ("Illegal memory index!"); 111 } 112 CultureMemoryCell cell = new CultureMemoryCell(a_infotext, a_historySize); 113 cell.setValue(a_value); 114 m_memory[a_index] = cell; 115 return cell; 116 } 117 118 131 public CultureMemoryCell set(final String a_name, final Object a_value, 132 final int a_historySize) { 133 if (a_name == null || a_name.length() < 1) { 134 throw new IllegalArgumentException ("Illegal memory name!"); 135 } 136 int index = m_memoryNames.indexOf(a_name); 137 if (index < 0) { 138 m_memoryNames.add(a_name); 141 index = m_memoryNames.size() - 1; 142 } 143 CultureMemoryCell cell = new CultureMemoryCell(a_name, a_historySize); 144 cell.setValue(a_value); 145 m_memory[index] = cell; 146 return cell; 147 } 148 149 158 public CultureMemoryCell get(final int a_index) { 159 if (a_index < 0 || a_index >= size()) { 160 throw new IllegalArgumentException ("Illegal memory index!"); 161 } 162 return m_memory[a_index]; 163 } 164 165 174 public CultureMemoryCell get(final String a_name) { 175 if (a_name == null || a_name.length() < 1) { 176 throw new IllegalArgumentException ("Illegal memory name!"); 177 } 178 int index = m_memoryNames.indexOf(a_name); 179 if (index < 0) { 180 throw new IllegalArgumentException ("Memory name unknown: " + a_name); 181 } 182 return m_memory[index]; 183 } 184 185 194 public boolean contains(final String a_name) { 195 if (a_name == null || a_name.length() < 1) { 196 throw new IllegalArgumentException ("Illegal memory name!"); 197 } 198 int index = m_memoryNames.indexOf(a_name); 199 return index >=0; 200 } 201 202 208 public int size() { 209 return m_memory.length; 210 } 211 212 218 public String toString() { 219 StringBuffer result = new StringBuffer ("["); 220 int len = m_memory.length; 221 if (m_memory[0] == null) { 222 result.append(m_memory[0]); 223 } 224 else { 225 result.append(m_memory[0].toString()); 226 } 227 for (int i = 1; i < len; i++) { 228 if (m_memory[i] == null) { 229 result.append(";" + m_memory[i]); 230 } 231 else { 232 result.append(";" + m_memory[i].toString()); 233 } 234 } 235 result.append("]"); 236 return result.toString(); 237 } 238 239 245 public void clear() { 246 m_memory = new CultureMemoryCell[m_size]; 247 m_memoryNames.clear(); 248 } 249 250 256 public List getMemoryNames() { 257 return new Vector(m_memoryNames); 258 } 259 260 268 public boolean equals(Object a_other) { 269 try { 270 return compareTo(a_other) == 0; 271 } catch (ClassCastException cex) { 272 cex.printStackTrace(); 273 return false; 274 } 275 } 276 277 286 public int compareTo(Object a_other) { 287 Culture other = (Culture) a_other; 288 if (other == null) { 289 return 1; 290 } 291 return new CompareToBuilder() 294 .append(m_size, other.m_size) 295 .append(m_memory, other.m_memory) 296 .append(m_memoryNames.toArray(), other.m_memoryNames.toArray()) 297 .toComparison(); 298 } 299 300 309 public void setMatrixWidth(int a_width) { 310 int size = size(); 311 if (a_width > size) { 312 throw new IllegalArgumentException ("Width must not be greater than the" 313 + " size of the memory (" 314 + size 315 + ") !"); 316 } 317 m_width = a_width; 318 } 319 320 331 public CultureMemoryCell setMatrix(int a_x, int a_y, Object a_value) { 332 int index = a_x * m_width + a_y; 333 CultureMemoryCell cell = m_memory[index]; 334 if (cell == null) { 335 cell = new CultureMemoryCell(a_x+"_"+a_y, -1); 336 } 337 cell.setValue(a_value); 338 m_memory[index] = cell; 339 return cell; 340 } 341 342 353 public CultureMemoryCell getMatrix(int a_x, int a_y) { 354 int index = a_x * m_width + a_y; 355 return get(index); 356 } 357 } 358 | Popular Tags |