Implement Properties for JavaME


/*
* BufferedReader.java
* Aug 3, 2007
*/
package com.company.dept.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
* enable readLine() method for a input stream
*
* @author
*
*/
public class BufferedReader {

private static final int CR = '/n';

private static final int LF = '/r';

private boolean error;

private Reader r;

public BufferedReader(InputStream is) {
if (is == null) {
System.out.println("BufferedReader() Null Parameter");
error = true;
} else {
r = new InputStreamReader(is);
error = false;
}
}

public void close() {
if (r != null) {
try {
r.close();
} catch (IOException e) {
// ignored
}
r = null;
}
}

/**
* read and return a string of line from the input, ignore any CR, LF
*
* @return a string of line, null for EOF or IOException
*/
public String readLine() {
if (error) {
return null;
}
StringBuffer sb = null;

try {
int ch = r.read();
if (ch == -1) {
return null;
}

sb = new StringBuffer();

// eat up CR and LF
while (ch == CR || ch == LF) {
ch = r.read();
}

while (ch != -1) {
if (ch == CR || ch == LF) {
// meet /n
break;
}
sb.append((char) ch);
ch = r.read();
}

} catch (OutOfMemoryError err) {
// handling with error
err.printStackTrace();
} catch (IOException e) {
// handling with exception
e.printStackTrace();
}

if (sb == null) {
return null;
}
return sb.toString();
}

}



/*
* Map.java
* Aug 3, 2007
*/
package com.company.dept.util;

/**interface define methods for implementation
* @author
*
*/
public interface Map {
public void clear();

/**
* return whether the map contains the given key
*
* @param key
* @return
*/
public boolean containsKey(String key);

/**
* return whether the map contains the given value
*
* @param value
* @return
*/
public boolean containsValue(String value);

/**
* retrieve value by key
*
* @param key
* @return
*/
public String get(String key);

public String[] getKeys();

public String[] getValues();

public boolean isEmpty();

/**
* add/update key/value to map
*
* @param key
* @param value
*/
public void put(String key, String value);

/**
* remove key/value by given key
*
* @param key
*/
public void remove(String key);
}





/*
* HashMap.java
* Aug 3, 2007
*/
package com.company.dept.util;

/**
* class implement interface Map
*
* @author
*
*/
public class HashMap implements Map {

private static final int MAX = 300;

private int counter = 0;

private String[] keys;

private String[] values;

public HashMap() {
clear();
}

public void clear() {
keys = new String[MAX];
values = new String[MAX];
counter = 0;
}

public boolean containsKey(String key) {
if (findKey(key) >= 0)
return true;
return false;
}

public boolean containsValue(String value) {
if (findValue(value) >= 0)
return true;
return false;
}

/**
* find the given key in the stored key array
*
* @param key
* a string of key to find
* @return an index of matching string
*/
private int findKey(String key) {
int match = -1;
for (int i = 0; i < counter; i++) {
if (keys[i].equals(key)) {
match = i;
break;
}
}
return match;
}

/**
* find the given value in stored value array
*
* @param value
* a string of value to find
* @return an index to the matching string
*/
private int findValue(String value) {
int match = -1;
for (int i = 0; i < counter; i++) {
if (values[i].equals(value)) {
match = i;
break;
}
}
return match;
}

public String get(String key) {
String os = null;

// loop keys
int match = -1;
match = findKey(key);

// return value
if (match >= 0) {
os = values[match];
}

return os;
}

public String[] getKeys() {
if (keys == null || counter == 0) {
return null;
}

String[] oss = new String[counter];
for (int i = 0; i < counter; i++) {
oss[i] = keys[i];
}
return oss;
}

public String[] getValues() {
if (values == null || counter == 0) {
return null;
}

String[] oss = new String[counter];
for (int i = 0; i < counter; i++) {
oss[i] = values[i];
}
return oss;
}

public boolean isEmpty() {
if (counter > 0) {
return false;
}
return true;
}

public void put(String key, String value) {
int match = -1;
match = findKey(key);
if (match < 0) {
// no found match, process add new
keys[counter] = key;
values[counter] = value;
counter++;
} else {
// found match, process update
values[match] = value;
}
}

public void remove(String key) {
int match = -1;
match = findKey(key);

if (match < 0) {
// no found
return;
}

// move last to matched position
keys[match] = keys[counter];
values[match] = values[counter];

// set last to null
keys[counter] = null;
values[counter] = null;

counter--;

}

}



/*
* Properties.java
* Aug 3, 2007
*/
package com.mot.lrt.util;

import java.io.InputStream;

import com.mot.lrt.io.BufferedReader;

/**
* class works as Java SE properties
*
* @author e7125c
*
*/
public class Properties {

private String curKey;

private String curValue;

private Map dish = new HashMap();

private InputStream is;

public Properties() {
// empty
}

/**
* private method, fill current key and value by given string
*
* @param s
* a string to split into curKey and curValue
*/
private void expLine(String s) {
if (s == null || s.trim().length() < 1) {
return;
}
if (s.indexOf("#") == 0) {
// comment
return;
}

int pt = s.indexOf("=");
if (pt > 0) {
curKey = s.substring(0, pt);
curValue = s.substring(pt + 1);
} else {
curKey = s;
curValue = null;
}
}

/**
* get the value by given key
*
* @param key
* a string of given key
* @return a string of the returned value, or null
*/
public String getProperty(String key) {
return dish.get(key);
}

/**
* get the value by given key, return default value if null
*
* @param key
* a string of given key
* @param def
* a string of default value
* @return a string of returned value
*/
public String getProperty(String key, String def) {
String os = dish.get(key);
if (os == null) {
return def;
}
return os;
}

/**
* load properties from given input stream
*
* @param IS
* an object of input stream
*/
public void load(InputStream IS) {
if (IS == null) {
System.out.println("Properties.load() Null InputStream");
return;
}
is = IS;
BufferedReader br = null;
try {
br = new BufferedReader(is);
String line = br.readLine();
while (line != null) {
// fill curKey and curValue with value
expLine(line);
// put current key and value to map
dish.put(curKey, curValue);
// next line
line = br.readLine();
}
} catch (OutOfMemoryError e) {
// in case ...
e.printStackTrace();
} finally {
if (br != null) {
br.close();
br = null;
}
}
}

/**
* return all property names in string array
*
* @return a string array of property names
*/
public String[] propertyNames() {
return dish.getKeys();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值