Anyone want to critique this? I thought it'd be interesting to write one of these, so eh. Possible uses, holding information on a user in a channel (variables Ping, Flags, Username, ChannelListIndex, Product, Statstring, etc), holding bot profile information, etc.
/*
* Class: PropertyBag
* Package: Util
* Author: Joe[e2]
* Purpose: This houses our variables, and their values.
*/
package util;
public class PropertyBag {
String names[] = new String[255]; // The names of our variables
String values[] = new String[255]; // The values of our variables
int position = 0; // The position that we're inserting new variables in
/**
* @author Joe[e2]
* @param name: Variable name to return value for.
* @return: Value if variable exists, otherwise null.
*/
public String getValue(String name) {
for(int i = 0; i < 255; i++) {
if(names == name) {
return values;
}
}
return null;
}
public void setValue(String name, String value) {
for(int i = 0; i < 255; i++) {
if(names == name) {
values = value;
return;
}
}
names[position] = name;
values[position] = value;
position++;
return;
}
}
Demonstration/test class:
/*
* Class: Main
* Author: Joe[e2]
* Purpose: Create + test our PropertyBag object.
*/
import util.PropertyBag;
public class Main {
public static void main(String args[]) {
PropertyBag pbag = new PropertyBag();
pbag.setValue("Name1", "Value1");
pbag.setValue("Name2", "Value2");
pbag.setValue("Name3", "Value3");
System.out.println(pbag.getValue("Name1"));
System.out.println(pbag.getValue("Name2"));
System.out.println(pbag.getValue("Name3"));
}
}
Critique:
HashMap (http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html) TreeMap (http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeMap.html)
Why're you preallocating a fixed maximum length? Let it grow dynamically. That'll also avoid the problem that it can silently fail to store a property.