asmuts 02/01/14 22:45:49
Added: src/java/org/apache/stratum/jcs/utils/data
PropertyGroups.java Queue.java QueueV.java
Log:
some basic data utilities
some may be borrowed, not sure where from, pulled into project a long time ago
Revision Changes Path
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/utils/data/PropertyGroups.java
Index: PropertyGroups.java
===================================================================
package org.apache.stratum.jcs.utils.data;
import java.util.*;
/////////////////////////////////////////////////
/**
* represent a file from the classpath, such as
* C:\JRun\jsm-default\classes\tst.properties
* which we load via
* load("\tst.properties")
* as a group of Properties in a hashtable; thus
* alpha_zip=1111
* beta_zip=2222
* gamma_zip=3333
* alpha_zap=uggle
* wurple=wing
* beta_zap=wuggle
* zurple=zing
*
* becomes a PropertyGroups with
* Enumeration propertyKeys()=[alpha,beta,gamma]
* Enumeration simpleKeys()=[wurple,zurple]
* Properties getProperties("alpha") = {zip=1111,zap=uggle}
* String getProperty("wurple")=wing.
* String getProperty("alpha","bibble")=bibble
*
* It is an error to define a key both as a group name and a property:
* alpha=stringval
* would be an error; it would conflict with alpha_zip or alpha_zap.
* it is not an error to ask for a property whose name is a group name,
* but the answer is null.
*
*/
public class PropertyGroups extends Hashtable {
private Properties props=null;
private String fileName=null;
int simpleKeys;
int compoundKeys;
public PropertyGroups(){
props=new Properties();
simpleKeys=0;
compoundKeys=0;
}
public PropertyGroups(String name)throws Exception{
this();
load(name);
}
///////////////////////////////////////
public void load(String name)throws Exception{fileName=name; load();}
////////////////////////////////////////////
public void load()throws Exception{
java.io.InputStream is=getClass().getResourceAsStream(fileName);
if(null==is) throw
new Exception("PropertyGroups.load: can't get resource "+fileName);
props.load(is);
is.close();
Enumeration keys=props.keys();
while(keys.hasMoreElements()){
String key=(String)keys.nextElement();
int sloc;
if(0>(sloc=key.indexOf('_'))){
simpleKeys++;
put(key,props.get(key));
}
else{
String key1=key.substring(0,sloc);
String key2=key.substring(1+sloc);
Properties subprops=(Properties)get(key1);
if(null==subprops){
compoundKeys++;
put(key1,subprops=new Properties());
}
subprops.put(key2,props.get(key));
}
}
}
public String getProperty(String key){
Object ob=get(key);
if(ob instanceof String)return (String)ob;
return null;
}
public String getProperty(String key,String dflt){
if(null==key)return dflt;
String p=getProperty(key);
return p==null?dflt:p;
}
public Properties getProperties(String key){
if(null==key){
return null;
}
Object ob=get(key);
if(ob instanceof Properties)return (Properties)ob;
return null;
}
//////////////////////////////////////////////////////
public Enumeration propertyKeys(){return new PropertyKeysEnum();}
////////////////////////////////////////////////////////
public Enumeration simpleKeys(){return new SimpleKeysEnum();}
//////////////////////////////////////////////
private void keyVal(StringBuffer sB,Object key){
String k=(String)key;
sB.append(k); sB.append("=");
sB.append(get(key).toString());
}
///////////////////////////////////////////
public synchronized String toString(){
StringBuffer sB=new StringBuffer();
Enumeration pk=propertyKeys();
if(pk.hasMoreElements())keyVal(sB,pk.nextElement());
while(pk.hasMoreElements()){
sB.append(", ");
keyVal(sB,pk.nextElement());
}
Enumeration sk=simpleKeys();
if(sk.hasMoreElements())keyVal(sB,sk.nextElement());
while(sk.hasMoreElements()){
sB.append(", ");
keyVal(sB,sk.nextElement());
}
return sB.toString();
}
///////////////////////////////////////////////
class PropertyKeysEnum implements Enumeration{
int howMany; Enumeration baseEnum;
/////////////////////////////////
public PropertyKeysEnum(){
howMany=compoundKeys;
baseEnum=keys();
}
////////////////////////////////////
public boolean hasMoreElements(){return howMany>0;}
/////////////////////////////////
public Object nextElement(){
Object ob;
while(baseEnum.hasMoreElements()){
Object k=baseEnum.nextElement();
Object v=get(k);
if(v instanceof Properties){
howMany--;
return k;
}
}
howMany=0;
return null;
}
} // end of PropertyKeysEnum inner class
/////////////////////////////////////////////////
class SimpleKeysEnum implements Enumeration{
int howMany; Enumeration baseEnum;
public SimpleKeysEnum(){
howMany=simpleKeys;
baseEnum=keys();
}
public boolean hasMoreElements(){return howMany>0;}
public Object nextElement(){
Object ob;
while(baseEnum.hasMoreElements()){
Object k=baseEnum.nextElement();
Object v=get(k);
if(v instanceof String){
howMany--;
return k;
}
}
howMany=0;
return null;
}
} // end of SimpleKeysEnum inner class
} // end PropertyGroups class
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/utils/data/Queue.java
Index: Queue.java
===================================================================
package org.apache.stratum.jcs.utils.data;
import java.util.Vector;
public class Queue {
private Vector v;
public Queue() {
v = new Vector();
}
public Queue(int size) {
v = new Vector(size);
}
public synchronized Object dequeue() {
if(empty()) return null;
else {
Object o = v.firstElement();
v.removeElementAt(0);
return o;
}
}
public void enqueue(Object o) {
v.addElement(o);
}
public int size() {
return v.size();
}
public boolean empty() {
return v.size() < 1;
}
}
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/utils/data/QueueV.java
Index: QueueV.java
===================================================================
package org.apache.stratum.jcs.utils.data;
import java.util.*;
public class QueueV extends Vector {
public boolean isEmpty(){
return size()<=0;
}
public void append(Object ob){
addElement(ob);
}
public Object next() throws Exception{
if( isEmpty() ){
throw new Exception("no Queue.next() on empty queue");
}
// too slow
Object ob=firstElement();
removeElementAt(0);
return ob;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>